472,145 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Access Modifier in structs - help!

Hi there,

I had seen examples for classes, but i had no idea how to implement the same
thing in struct. I am quite mix up!

Which one is correct?

Scenario:
WForm.cs - the one that calls FileA.cs to access the structures
FileA.cs - contains all structures

1)

public struct TestA
{
public byte a;
public byte b;

public struct TestB
{
public byte c;
public byte d;
}
}

yes/no - but i think this is not recommended, i read it somewhere.. not
secure or something

2)

public struct TestA
{
internal byte a;
internal byte b;

public struct TestB
{
internal byte c;
internal byte d;
}
}

yes/no?

3)

public struct TestA
{
internal byte a;
internal byte b;

internal struct TestB
{
internal byte c;
internal byte d;
}
}

yes/no

4)

internal struct TestA
{
public byte a;
public byte b;

public struct TestB
{
public byte c;
public byte d;
}
}

yes/no

5)

internal struct TestA
{
public byte a;
public byte b;

internal struct TestB
{
public byte c;
public byte d;
}
}

yes/no

6)

public struct TestA
{
private byte a;
private byte b;

public struct TestB
{
private byte c;
private byte d;
}
}

yes/no - but how could this is possible? Is there a way to get out the
private values

7)

public struct TestA
{
private byte a;
private byte b;

private struct TestB
{
private byte c;
private byte d;
}
}

yes/no

8)

private struct TestA
{
public byte a;
public byte b;

private struct TestB
{
public byte c;
public byte d;
}
}

yes/no

Any idea please? Can anyone clear my doubts? Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
3 1766
OK the key thing is are WForm.cs and FileA.cs going to be built into the same assembly or into two separate ones?

Same assembly:
1) best avoided as you have no control over what values are put into the byte member - no validation. But other than that it will work fine.
2) this will be exactly the same as 1) except code in other assemblies will not be able touch the byte members
3) this will be the same as 2) except code in other assemblies will not be able to touch TestB
4) same as 1) except code in other assemblies will not be able to touch TestA
5) same as 1) except code in other assemblies will not be able to touch TestA or TestB
6) TestA and TestB will be accessible to code in other assemblies but the byte members can only be touched by code in their declaring struct. This is normally dealt with by having public properties to access the fields. This gives the benefit of being able to validate values written to the byte members
7) You cannot make a type (class or struct) private - only public and internal are allowed
8) same as 7)

You missed out the option of:
internal struct TestA
{
private byte a;
private byte b;
}

internal struct TestB
{
private byte c;
private byte d;
}
This gives maximum protection - only code in the assembly can get to the structs and you control access to the state (you would need to add public properties to access the state).

Different assemblies:
1) best avoided as you have no control over what values are put into the byte member - no validation. But other than that it will work fine.
2) this will be exactly the same as 1) except code in other assemblies will not be able touch the byte members so you will have to provide public properties
3) this will not work as the form will not be able to get to TestB
4) this will not work as the form will not be able to get to TestA
5) this will not work as the form will not be able to get to TestA or TestB
6) TestA and TestB will be accessible to code in other assemblies so it will work fine. But the byte members can only be touched by code in their declaring struct. This is normally dealt with by having public properties to access the fields. This gives the benefit of being able to validate values written to the byte members
7) You cannot make a type (class or struct) private - only public and internal are allowed
8) same as 7)
?
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi there,

I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up!

Which one is correct?

Scenario:
WForm.cs - the one that calls FileA.cs to access the structures FileA.cs - contains all structures

1)

public struct TestA
{
public byte a;
public byte b;

public struct TestB
{
public byte c;
public byte d;
}
}

yes/no - but i think this is not recommended, i read it somewhere.. not secure or something

2)

public struct TestA
{
internal byte a;
internal byte b;

public struct TestB
{
internal byte c;
internal byte d;
}
}

yes/no?

3)

public struct TestA
{
internal byte a;
internal byte b;

internal struct TestB
{
internal byte c;
internal byte d;
}
}

yes/no

4)

internal struct TestA
{
public byte a;
public byte b;

public struct TestB
{
public byte c;
public byte d;
}
}

yes/no

5)

internal struct TestA
{
public byte a;
public byte b;

internal struct TestB
{
public byte c;
public byte d;
}
}

yes/no

6)

public struct TestA
{
private byte a;
private byte b;

public struct TestB
{
private byte c;
private byte d;
}
}

yes/no - but how could this is possible? Is there a way to get out the private values

7)

public struct TestA
{
private byte a;
private byte b;

private struct TestB
{
private byte c;
private byte d;
}
}

yes/no

8)

private struct TestA
{
public byte a;
public byte b;

private struct TestB
{
public byte c;
public byte d;
}
}

yes/no

Any idea please? Can anyone clear my doubts? Thanks.
--
Regards,
Chua Wen Ching :)

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #2
Hi Richard,

Thanks for the tips. It will be useful for me.

But i had 1 question,

can i break

from:
-----
struct A
{
struct B
{

}
}
to:
---
struct A
{

}
struct B
{

}

// if this is the case, how does struct A and B relates to each other?

Thanks!

:)

"Richard Blewett [DevelopMentor]" wrote:
OK the key thing is are WForm.cs and FileA.cs going to be built into the same assembly or into two separate ones?

Same assembly:
1) best avoided as you have no control over what values are put into the byte member - no validation. But other than that it will work fine.
2) this will be exactly the same as 1) except code in other assemblies will not be able touch the byte members
3) this will be the same as 2) except code in other assemblies will not be able to touch TestB
4) same as 1) except code in other assemblies will not be able to touch TestA
5) same as 1) except code in other assemblies will not be able to touch TestA or TestB
6) TestA and TestB will be accessible to code in other assemblies but the byte members can only be touched by code in their declaring struct. This is normally dealt with by having public properties to access the fields. This gives the benefit of being able to validate values written to the byte members
7) You cannot make a type (class or struct) private - only public and internal are allowed
8) same as 7)

You missed out the option of:
internal struct TestA
{
private byte a;
private byte b;
}

internal struct TestB
{
private byte c;
private byte d;
}
This gives maximum protection - only code in the assembly can get to the structs and you control access to the state (you would need to add public properties to access the state).

Different assemblies:
1) best avoided as you have no control over what values are put into the byte member - no validation. But other than that it will work fine.
2) this will be exactly the same as 1) except code in other assemblies will not be able touch the byte members so you will have to provide public properties
3) this will not work as the form will not be able to get to TestB
4) this will not work as the form will not be able to get to TestA
5) this will not work as the form will not be able to get to TestA or TestB
6) TestA and TestB will be accessible to code in other assemblies so it will work fine. But the byte members can only be touched by code in their declaring struct. This is normally dealt with by having public properties to access the fields. This gives the benefit of being able to validate values written to the byte members
7) You cannot make a type (class or struct) private - only public and internal are allowed
8) same as 7)
?
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi there,

I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up!

Which one is correct?

Scenario:
WForm.cs - the one that calls FileA.cs to access the structures FileA.cs - contains all structures

1)

public struct TestA
{
public byte a;
public byte b;

public struct TestB
{
public byte c;
public byte d;
}
}

yes/no - but i think this is not recommended, i read it somewhere.. not secure or something

2)

public struct TestA
{
internal byte a;
internal byte b;

public struct TestB
{
internal byte c;
internal byte d;
}
}

yes/no?

3)

public struct TestA
{
internal byte a;
internal byte b;

internal struct TestB
{
internal byte c;
internal byte d;
}
}

yes/no

4)

internal struct TestA
{
public byte a;
public byte b;

public struct TestB
{
public byte c;
public byte d;
}
}

yes/no

5)

internal struct TestA
{
public byte a;
public byte b;

internal struct TestB
{
public byte c;
public byte d;
}
}

yes/no

6)

public struct TestA
{
private byte a;
private byte b;

public struct TestB
{
private byte c;
private byte d;
}
}

yes/no - but how could this is possible? Is there a way to get out the private values

7)

public struct TestA
{
private byte a;
private byte b;

private struct TestB
{
private byte c;
private byte d;
}
}

yes/no

8)

private struct TestA
{
public byte a;
public byte b;

private struct TestB
{
public byte c;
public byte d;
}
}

yes/no

Any idea please? Can anyone clear my doubts? Thanks.
--
Regards,
Chua Wen Ching :)

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #3
Well sure you can change from one to the other but the meaning of the code changes:

struct A
{
struct B
{
}
}

In this case you are saying that B is part of the implementation, or fundamentally associated with A - in such a way that B makes no sense without A.

struct A
{
}
struct B
{
}

In this case (assuming they are declared in the same assembly) then you are saying these structs are related by being part of the same component or deployment and may be related on that basis, but no more than that.

I'm assuming the lack of access modifiers is not significant ( if it were, in the first example B would not be visible outside of A)

Regards

Richard Blewett - DevelopMentor
Http://staff.develop.com/richardb/weblog

?
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi Richard,

Thanks for the tips. It will be useful for me.

But i had 1 question,

can i break

from:
-----
struct A
{
struct B
{

}
}
to:
---
struct A
{

}
struct B
{

}

// if this is the case, how does struct A and B relates to each other?

Thanks!

:)
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dan Disney | last post: by
3 posts views Thread by N Khan via .NET 247 | last post: by
1 post views Thread by Jacob N. Rohde | last post: by
1 post views Thread by Ashkan Daie | last post: by
3 posts views Thread by Mr Newbie | last post: by
5 posts views Thread by wpmccormick | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.