Solved

Structure type constant value

Posted on 2004-08-25
7
399 Views
Last Modified: 2011-10-03
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);
}
0
Comment
Question by:awu25
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 11899507
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.


0
 

Author Comment

by:awu25
ID: 11899539
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
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 11900184
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:awu25
ID: 11900295
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"
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 11900402
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
0
 

Author Comment

by:awu25
ID: 11900443
The original VB code is:

Public Shared Struct_StrAs BLCommData.Struct_Str

Dim a as string
a = Struct_Str.Str1

and it works, why?
0
 

Accepted Solution

by:
Stiow earned 500 total points
ID: 12041841
because you have created a refernce to it already.  

Public Shared Struct_StrAs BLCommData.Struct_Str
brings the contents of the BLCommData.Struct_Str structure into the local Struct_Str variable.  If you changed the line to

Public Shared Struct_ABC As BLCommData.Struct_Str

then the code

Dim a as string
a = Struct_Str.Str1

would cease to work.

It's worth noting that VB is not a strict on the compilation of it's code, which is why you see the descrepancy betweent VB and c#.

Stuart.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now