Solved

Structure type constant value

Posted on 2004-08-25
7
420 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
listing all functions in JavaScript 19 218
Help Required 3 110
backup program with robocopy 6 47
Vector, list Questions R 6 32
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.

821 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