Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to access static field of a public struct

Tags:

c#

I am trying to access a static field of the struct which I already defined as public. However, I still got "inaccessible due to its protection level" Can anyone help me?

public class Program
{
    public struct AT_CMD
    {
        static int x = 7;
       static  byte[] cmd_mode = new byte[3] { 0x2B, 0x2B, 0x2B };
       static  byte[] end_device_assoc = new byte[4] { 0x41, 0x54, 0x41, 0x31 };
      //should be 0 for end device, default is 0
       static  byte[] data_rate = new byte[4] { 0x41, 0x54, 0x42, 0x44 }; //3 for 9600, 5 for 38400
       static  byte[] channel = new byte[4] { 0x41, 0x54, 0x43, 0x48 }; //0x0B-0x1A
       static  byte[] Dest_addr_high = new byte[4] { 0x41, 0x54, 0x44, 0x48 }; //0 FOR 16bit
       static  byte[] Dest_addr_low = new byte[4] { 0x41, 0x54, 0x44, 0x4C };
       static  byte[] my_addr = new byte[4] { 0x41, 0x54, 0x4D, 0x59 }; // 0-0xFFFF
       static  byte[] carriage_return = new byte[1] { 0x0D };
   }  

    static void Main()
    {
        int y = AT_CMD.x;
    }
}
like image 691
fiftyplus Avatar asked Jan 24 '12 20:01

fiftyplus


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why is C programming language important?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.

Why is C called a mid level programming language?

C has the features of both assembly level languages i.e low-level languages and higher level languages. So that's why C is generally called as a middle-level Language. The user uses C language for writing an operating system and generates menu driven customer billing system.


3 Answers

Change it to:

  public struct AT_CMD
  {
        public static int x = 7;

        // ...
  }  

Just making a struct public doesn't affect its members, the members of the struct will be private by default unless you specify otherwise.

UPDATE: Addressing the confusion on default access, internal is the default access for the struct or class itself if declared at the namespace level (not nested in another class/struct):

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

Whereas, the fields in a class or struct always default to private:

The access level for class members and struct members, including nested classes and structs, is private by default.

From the MSDN.

like image 101
James Michael Hare Avatar answered Sep 25 '22 23:09

James Michael Hare


By default fields are private. Add the most limiting modifier that will still allow you to work with the structure.

public struct AT_CMD
{
    public static int x = 7;
}  

static void Main()
{
    int y = AT_CMD.x;
}
like image 26
oleksii Avatar answered Sep 25 '22 23:09

oleksii


The struct is public, but the members are not.

like image 41
Almo Avatar answered Sep 26 '22 23:09

Almo