Search code examples
c#sap-erpsap-dotnet-connector

Adding variable watch shows field but compiling doesnt like the field?


I have to pull some data from an ERP system (SAP) in C#. Without going into details about SAP (I'm sure you wouldn't want to know about it).

I'm calling some Remote Function from within C# and its working but I'm having a strange issue. One of the functions I have to call in SAP from C# is called BAPI_USER_GET_DETAIL.

So I do this in C#:

        s.Bapi_User_Get_Detail("No","10217502", out address, out alias, out companyName,
            out defaults, out islocked, out lastmodified, out logonData, out ref_user, out snc, out uclass, 
            ref activeGroups, ref addcomrem, ref addfax, ref addpag, ref addprt, ref addrfc,
            ref addrml, ref addsmtp, ref addssf, ref addtel, ref addtlx, ref addttx, ref adduri, 
            ref addx400, ref extidhead, ref extidpart, ref groups, ref parameter, ref parameter1,
            ref profiles, ref return0, ref systems, ref uclasssys); 

        Console.WriteLine(companyName._Company);
        Console.ReadLine();

SAP requires all these fields so that is quite normal code. I need to pull one of the out arguments within this function. Namely the argument company that is after I execute this procedure I should have a value inside of this company field and the watch shows that right here:

enter image description here

The issue I'm having is notice that _Company has the value I need but I cannot do companyName._Company (visual studio doesnt like this). However if I do it in my watch as shown above it displays it correctly. It gives me the company name. So how is it that the watch shows the value correctly. I must be doing something simple in C# incorrectly.

Edit

Err looks like it is some sort of error due to protection level in SAP..I wonder how I can expose this...

Solution

  • From what I can see the most likely explanation is that _Company is a private field. That is why it is not accessible directly from code. The Watch window uses reflection (or similar technique) to get all members including private and protected.

    Edit:
    If SAP made it private it is probably for a good reason. But if you want to pick the value out anyway you can try something like this

    companyName.GetType().GetField("_Company", BindingFlags.NonPublic).GetValue(companyName);