Solved

Structure type constant value

Posted on 2004-08-25
7
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VbScript to countdown to New Year's Day 6 94
What are programming codes and how do they work? 7 119
Cygwin - GNU GPL License 1 58
Eclipse with various Java releases 7 91
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is an explanation of a simple data model to help parse a JSON feed
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 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