473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using internal modifier on set accessor ..

Hello!

With C# 2.0 coming up, I was wondering what your thoughts are regarding the
introduction of access modifiers on set accessors.

Personally, I like the first example, because of its simplicity. The second
seems rather dangerous and the third one - well, quite verbose.

I'm trying to communicate best practices to fellow developers in-house - and
figured some of you already had experience from past projects / languages
with such features.

Your thoughts?

// 1 - using C# 2.0 access modifer on member
public class CmsObject
{
private Guid cmsObjectID;

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
internal set { this.cmsObjectID = value; }
}
}

vs.

// 2 - using an internal access modifier on member
public class CmsObject
{
internal Guid cmsObjectID;

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
}
}
vs.

// 3 using a method to set member
public class CmsObject
{
private Guid cmsObjectID;

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
}

internal void SetCmsObjectID(Guid cmsObjectID)
{
this.cmsObjectID = cmsObjectID;
}
}

Thanks in advance.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Nov 16 '05 #1
2 7691
Hi,

the reason for encapsulating a member field into a property in the first
place is usually because set and get are associated with some kind of
behaviour.

Leaving the field accessible directly to bypass encapsulation is
therefor, in my opionion, breaking good OO design and are breaking
encapsulation.

Also if you are using propertys, there's really no need to use separate
set or get methods to write and read from the data. All access to that
particular member should be in a single place.

So I would say that your code snippet 1 is the one that makes the most
sense in a true OO environment.

Additionally I would like to add that if you have a field encapsulated
into a property, you should make it a rule to always access that field
throught a property even inside your class. Why? Well as I said earlier,
you encapsulate a field for a reson.

--
Patrik Lwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net

Anders Borum [.NET/C# MCP] wrote:
Hello!

With C# 2.0 coming up, I was wondering what your thoughts are regarding the
introduction of access modifiers on set accessors.

Personally, I like the first example, because of its simplicity. The second
seems rather dangerous and the third one - well, quite verbose.

I'm trying to communicate best practices to fellow developers in-house - and
figured some of you already had experience from past projects / languages
with such features.

Your thoughts?

// 1 - using C# 2.0 access modifer on member
public class CmsObject
{
private Guid cmsObjectID;

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
internal set { this.cmsObjectID = value; }
}
}

vs.

// 2 - using an internal access modifier on member
public class CmsObject
{
internal Guid cmsObjectID;

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
}
}
vs.

// 3 using a method to set member
public class CmsObject
{
private Guid cmsObjectID;

public Guid CmsObjectID
{
get { return this.cmsObjectID; }
}

internal void SetCmsObjectID(Guid cmsObjectID)
{
this.cmsObjectID = cmsObjectID;
}
}

Thanks in advance.

Nov 16 '05 #2
Hello Patrik

First of all, thanks for sharing your thoughts.
the reason for encapsulating a member field into a property in the first
place is usually because set and get are associated with some kind of
behaviour.
Well, I have never been a fan of exposing member fields directly without
wrapping them in a set of accessors. There are a few cases where I use
private configuration classes etc. that are used with serialization, but I
would never do it with a public exposed interface.
Leaving the field accessible directly to bypass encapsulation is
therefor, in my opionion, breaking good OO design and are breaking
encapsulation.
I agree.
So I would say that your code snippet 1 is the one that makes the most
sense in a true OO environment.
Glad to hear that, I was also under the impression that
Additionally I would like to add that if you have a field encapsulated
into a property, you should make it a rule to always access that field
throught a property even inside your class. Why? Well as I said earlier,
you encapsulate a field for a reson.


Well, some times it can be useful to access the field directly without going
through the public property.

With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: TS | last post by:
what would be the scenario that you would want to declare a variable of a class in that fashion instead of a public property accessor?
0
by: Subu | last post by:
Hi, I have declared a class with Internal modifier , trying to instantiate the same class in same project in a webform. its not showing the class in the editor. Please help me out
2
by: Kolozs, Áron | last post by:
Hi everybody, The C# compiler reports a Compiler Error CS0052 in the following situation: I declared a type marked as "internal": namespace MyNamespace { internal class MyInteralClass
6
by: Billy | last post by:
I have created a typed dataset by dragging on a sql table on to the designer of the dataset and from there I can change the modifier of the tableadpater to 'internal' from 'public' thru the...
4
by: aaryan | last post by:
hi all, again i need a clarification. the access modifier protected means that class members are accessible from its own class and its derived class only. internal means that the class member is...
6
by: Andrus | last post by:
class Class1 { public void foo() {} } Does not cause any error or warning at compile time. Why ? public access modifier should not allowed in foo() since it is member of
13
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes...
1
by: Richard Coltrane | last post by:
Hi there, The docs say that default access modifier is internal. My own experience has been that it is internal. And yet almost every website Ive seen on the web tries to tell me its public?? Am...
9
by: dylan.miller | last post by:
I'm having trouble understanding the internal access modifier. There are many classes in my assembly that should not be accessible outside of the assembly. I've used the internal access modifier...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.