473,574 Members | 7,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1892
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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3536
by: Dan Disney | last post by:
I have read conflicting information on the access scope of VB.Net class declarations when no access modifier is specified. MSDN library documentation states "Classes that do not specify an access modifier are declared as Friend by default" However, another source, VB.Net Class Design Handbook by Wrox Press states that no a "no class access...
3
1790
by: N Khan via .NET 247 | last post by:
Hi, I know the usage of access modifier ?public? in DLL assembliesbut I am a bit puzzled that if let say I have a public class inan exe assembly then what purpose access modifier ?public? willserver. Related to the same thing is it possible that we canexpose exe assembly like DLL assembly. Cheers ----------------------- Posted by a user...
1
1727
by: Jacob N. Rohde | last post by:
Hi guys, I was under the assumption that the default type access modifier for classes/structs is internal. But the MCAD Study Guide (from MS even) says public?!?! (The Dr. GUI .NET tutorial from MS's site says internal!!) So which source from MS do I believe? It is internal is it not? Thanks
1
1857
by: Ashkan Daie | last post by:
Hi All, Is there an internal access modifier in managed vc++ like vc#? Thanks, Ashkan
1
5588
by: Mark Kamoski | last post by:
Hi-- Please help. What is the default access modifier value for a Sub or a Function when one has not explicitly been set. In MSDN, in "Access Types", it states... "If no access modifier has been specified, the default access type depends
3
1928
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the page. 2.) The identifier must be delcared with a minimum access of 'Protected' in order to work. If the above is indeed correct, then I have...
5
9168
by: dost | last post by:
can u tell me the diffrence between...... private public and protected .........in........these.......which one...is access modifier....and which one..is specifier....... and....the diffrence between...modifier and specifier in c++ thanks
9
2154
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service) Hide.dll methods and data I want to remain hidden I have a DLL, Hide.dll, that contains methods that I want to handle for
5
2009
by: wpmccormick | last post by:
What is the cleanest way to gain access to object methods and properties across classes and files in the same namespace? Example: A form object frmForm in file frmForm.cs creates obj1 defined in file obj1.cs, which in turn creates obj2 and obj3 defined in obj2.cs and obj3.cs, respectively. What is the canonical way for frmForm to access...
0
7817
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8081
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8258
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8119
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1082
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.