Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
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);
    }
}

        }


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)
Posted
Updated 16-Feb-18 19:30pm
v3
Comments
Afzaal Ahmad Zeeshan 16-Feb-18 3:01am    
That dates back to the days of C/C++ style programming of passing objects by reference (or pointers to their memory locations). The only reason to do that is to enable multiple returns from a method; 1 for the method success, 2 for the return value.

I don't recommend this approach as this doesn't follow a high-level programming approach. If there are problems, throw an error. This value-based conditional style is much more C style.
david salaman 16-Feb-18 3:41am    
thnx)) your answer is good enough)))but i have no other choice bcz there`s a fingerprint device and i have to deal with methods which it provides. manual of function are in the link file:///C:/Users/jack/Desktop/baha/Biometric%20Device%20SDK-%20Programmers%20Guide.1517824896.pdf

1 solution

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:
Quote:
from interface:

C#
[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:
objZkeeper
variable contains when it enters FetchDeviceInfo and look at the source code for that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900