Link to home
Start Free TrialLog in
Avatar of awu25
awu25

asked on

Structure type constant value

I am trying to rewrite a structure type from VB to C#.

This is the original VB code and is working fine.
   
Public Structure Struct_Str
        'CM Package
        Const Str1 As String = "First String"
        Const Str2 As String = "Second String"
End Structure

But this C# code gives me an "inaccessible due to its protection level" error.

public struct Struct_Str
{
      const string Str1 ="First String";
      const string Str2 ="Second String";
}
      
public void test()
{
      Struct_Str a;
      Response.Write(a.CM_FirstLoad);
}
Avatar of arif_eqbal
arif_eqbal

Microsoft Documentation says

"You can also define constants and properties in the structure, but you must declare at least one nonshared variable or event."

There is no instance variable in your structure that could be one of the reasons. But then you should get a different Error message for that, not the one you are getting anyway add one variable to the structure abd see.


Avatar of awu25

ASKER

Actually the original code is supposed to be
Public Structure Struct_Str
        'CM Package
        Const Str1 As String = "First String"
        Const Str2 As String = "Second String"
        Dim h As Boolean
End Structure

I changed my C# code to be

public struct Struct_Str
{
     const string Str1 ="First String";
     const string Str2 ="Second String";
     bool h;
}
     
public void test()
{
     Struct_Str a;
     string b = Struct_Str.CM_FirstLoad;
}

but it's still giving me the same error
Add the keyword PUBLIC to the structure definition

public struct Struct_Str
{
     public const string Str1 ="First String";
     public const string Str2 ="Second String";
     bool h;
}

then use it

public void test()
{
     Struct_Str a;
     string b = Struct_Str.Str1;
}

No need to even declare the variable a if you do not intend to use the structure member h. For accessing Str1 & Str2 you'll have to use Structure name only as it won't be accessible through the instance of structure.
Avatar of awu25

ASKER

It works now but when i asked the question I simplified the quesiton. The structure type is actually created in a different class library project.

I have the structure type in the class library project:
public struct Struct_Str
{
     public const string Str1 ="First String";
     public const string Str2 ="Second String";
     bool h;
}

I have another project that reference to this class with a method:

protected BLCommData.Struct_Str Struct_Str;
public ModuleBasic()
{
      string a = BLCommData.Struct_Str.Str1; //<-------------------This works
                a = Struct_Str.Str1;                             //<-------------this doesn't work
}

the erros I got is: "Cannot be accessed with an instance reference;qualify it with a type name instead"
See, the error says, you can not access a shared member (a const member in this case) through an instance variable, and that's true. For a structure all scoping rules for a class are also valid. Just like we do not access a shared class member from from a class instance we do not access it in case of structure also.

Now when you declare
protected BLCommData.Struct_Str Struct_Str;
Struct_Str becomes an instance variable, so accessing a const member (Str1) using this instance variable is wrong.

So You'll have to use fully qualified (Class) name BLCommData.Struct_Str.Str1
Avatar of awu25

ASKER

The original VB code is:

Public Shared Struct_StrAs BLCommData.Struct_Str

Dim a as string
a = Struct_Str.Str1

and it works, why?
ASKER CERTIFIED SOLUTION
Avatar of Stiow
Stiow

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial