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;
}
}
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.
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.
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.
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.
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.
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;
}
The struct is public, but the members are not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With