IS“out”作为参数传递的类型数据是否等于返回类型? [英] IS "out" type data passed as parameter equal to return type?

查看:154
本文介绍了IS“out”作为参数传递的类型数据是否等于返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近阅读了关于Out / ref的关键字,我需要知道下面的方法(FetchDeviceInfo)如何返回设备的序列号,尽管变量returnValue是string.Empty。并且在GetSerialNumber(machineNumber,out returnValue)方法体内什么都没有?



recently read about Out/ref keywords and i need to know how does the below method(FetchDeviceInfo) return serial number of a device, although the variable "returnValue" is string.Empty. and inside GetSerialNumber(machineNumber, out returnValue) method body there`s nothing?

public string FetchDeviceInfo(ZkemClient objZkeeper, int machineNumber)
        {
            
            string returnValue = string.Empty;
            
            objZkeeper.GetSerialNumber(machineNumber, out returnValue);
           

            return returnValue.ToString;}

/* 
realized method from class:
public bool GetSerialNumber(int dwMachineNumber, out string dwSerialNumber)
        { return objCZKEM.GetSerialNumber(dwMachineNumber, out dwSerialNumber); }

from interface:
[DispId(29)]
        bool GetSerialNumber(int dwMachineNumber, out string dwSerialNumber);

from manual: 
  2.23 GetSerialNumber
[Function]
Get the product information or serial number
[Protocol]
BOOLGetSerialNumber (
long dwMachineNumber,
 BSTR FAR* lpszSerialNumber);
[Paramters]
dwMachineNumber
The Machine Number of operating device
lpszSerialNumber
The returned string
[Return]
TRUE if success, FALSE else. 

 
*/

============================
class SomeClass
{
      static void Main()
      {
      ZkemClient objZkeeper = new ZkemClient();
      objZkeeper.Connect_Net("192.168.1.25", 4370);
      
      DeviceManipulator manipulator = new DeviceManipulator();
      string deviceInfo = manipulator.FetchDeviceInfo(objZkeeper, 1);
      }
}
//--------------
public class DeviceManipulator
{
       public string FetchDeviceInfo(ZkemClient objZkeeper, int machineNumber)
       {
              returnValue = string.Empty;
              objZkeeper.GetSerialNumber(machineNumber, out returnValue);
              return returnValue.ToString;}
        }

//------------------
using zkemkeeper;

public class ZkemClient : IZKEM
{
      Action<object, string> RaiseDeviceEvent;
      public ZkemClient(Action<object, string> RaiseDeviceEvent)
      {
      this.RaiseDeviceEvent = RaiseDeviceEvent;
      }

      CZKEM objCZKEM = new CZKEM();

      public bool GetSerialNumber(int dwMachineNumber, out string dwSerialNumber)
      {
      return objCZKEM.GetSerialNumber(dwMachineNumber, out dwSerialNumber); 
      }

}

//---------------
namespace zkemkeeper
{
    [CoClass(typeof(CZKEMClass))]
    [Guid("102F4206-E43D-4FC9-BAB0-331CFFE4D25B")]
    public interface CZKEM : IZKEM, _IZKEMEvents_Event
    {
    }
}

//-------------------

namespace zkemkeeper
{
    [ComConversionLoss]
    [Guid("102F4206-E43D-4FC9-BAB0-331CFFE4D25B")]
    [TypeLibType(4160)]
    public interface IZKEM
    {

     [DispId(29)]
     bool GetSerialNumber(int dwMachineNumber, out string dwSerialNumber);
    }
}

        }





我尝试过:



观看了几个关于ref / out的教程。得到这些是通过引用传递。在将它们作为方法参数传递时,在调用方法之后,我们声明的变量的值将被更改。不像价值传递。 (其中方法将使用我们声明的变量的副本)



What I have tried:

watched several tutorials about ref/out. Got that these are for passing by reference. while passing them as method parameter and after calling method the value of our declared variable would be changed. unlike passed by value. (where method will work with the copy of our declared variable)

推荐答案

简单的答案是 GetSerialNumber ,但是你还没找到合适的代码。你假设方法的主体是空的:
The simple answer is that there is something in the body of GetSerialNumber, but you haven't found the right code yet. Your assumption that the body of the method is empty from this:
引用:

来自界面:




[DispId(29)]
        bool GetSerialNumber(int dwMachineNumber, out string dwSerialNumber);

错误:接口无法声明任何代码,它只是定义了一个方法签名, 必须 由包含其定义中的接口的每个类实现。您需要使用调试器来确切地找出

is wrong: an Interface cannot declare any code, it just defines a method signature that must be implemented by every class that includes the interface in it's definition. You need to use the debugger to find out exactly what class the instance contained in your

Quote中包含的实例类:
Quote:

objZkeeper

objZkeeper

变量包含在进入 FetchDeviceInfo 并查看源代码。

variable contains when it enters FetchDeviceInfo and look at the source code for that.


这篇关于IS“out”作为参数传递的类型数据是否等于返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆