protection error

  • Thread starter Thread starter Rustam Asgarov
  • Start date Start date
R

Rustam Asgarov

I have a structure like this :
public struct index_struct
{
static char [] IP = new char [16] ;
int port;
static char [] FileN = new char [50];
int ID;
int Bnum;
long m_DataSize;
int Version;
}
then i have an array of this struct:

public index_struct [] indecies ;

then in one of the methods of same class in whic previous are defined i
try to do the following:

int id = indecies[j].ID;
but get an error : "'trial_v1.ListenForBroadcast.index_struct.ID' is
inaccessible due to its protection level
"

Can someone explain the situation?

thanks..
 
You're a C++ programmer at heart, aren't you? :-)

In C#, the default member accessibility for structs is 'private', not
'public'. You need to put the 'public' keyword in front of all those member
variables to make them accessible from outside the struct.

On a side note, that structure looks rather large to be a C# struct; I
suspect you'd be much better off making it a class. In C# only use structs
when you *need* a value-type.

- Lee
 
Lee said:
You're a C++ programmer at heart, aren't you? :-) yes...

In C#, the default member accessibility for structs is 'private', not
'public'. You need to put the 'public' keyword in front of all those member
variables to make them accessible from outside the struct.
thanks for the solution...
On a side note, that structure looks rather large to be a C# struct; I
suspect you'd be much better off making it a class. In C# only use structs
when you *need* a value-type.

i tried to make it class , but now i got another problem.
i my code i assign size of my struct (now class) to some variable :
int i = sizeof(index_struct);

and now it gives error like :
ListenForBroadcast.cs(124): Cannot take the address or size of a
variable of a managed type ('trial_v1.ListenForBroadcast.index_struct')

- Lee

I have a structure like this :
public struct index_struct
{
static char [] IP = new char [16] ;
int port;
static char [] FileN = new char [50];
int ID;
int Bnum;
long m_DataSize;
int Version;
}
then i have an array of this struct:

public index_struct [] indecies ;

then in one of the methods of same class in whic previous are defined i
try to do the following:

int id = indecies[j].ID;
but get an error : "'trial_v1.ListenForBroadcast.index_struct.ID' is
inaccessible due to its protection level
"

Can someone explain the situation?

thanks..
 
i tried to make it class , but now i got another problem.
i my code i assign size of my struct (now class) to some variable :
int i = sizeof(index_struct);

and now it gives error like :
ListenForBroadcast.cs(124): Cannot take the address or size of a
variable of a managed type ('trial_v1.ListenForBroadcast.index_struct')

Why do you need the size of the struct?

- Lee
 
bacause i am doing something like this:
indexsize=(n_bytes_received - 4*sizeof(int))/sizeof(index_struct);
indecies = new index_struct [indexsize] ;
 
Rustam Asgarov said:
bacause i am doing something like this:
indexsize=(n_bytes_received - 4*sizeof(int))/sizeof(index_struct);
indecies = new index_struct [indexsize] ;


Hmmm. Well I'm going to leave you to it; although I suspect serialization
might cover what you're trying to do - your C# looks so much like C++ that I
suspect you're not getting the most out of the CLR.

- Lee
 
Back
Top