Solved

Structure type constant value

Posted on 2004-08-25
7
388 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
EvenOdd challenge 10 83
scores100 challenge 3 82
Sorting in Excel with Group Headers if the Exist 2 68
Path of Workbook 3 45
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now