473,386 Members | 1,790 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,386 software developers and data experts.

Understanding protected and its use with collections

Hi,

I am a newbie to C# and I have a simple question for you. I have the
following two classes which are used to present information in Excel.

public class Environment : ICloneable, IComparable
{
readonly string name;
readonly int id; // the id of this env
with respect to the env sql table

public int LeftmostCell; // The column number in
Excel

...
}

public class Environments : IEnumerable, ICloneable
{
TestEnvironment[] testEnvironments;

void EstablishExcelLocations()
{
...
}

The function EstablishExcelLocations sets the values of LeftmostCell.
The problem I have is that I have to make LeftmostCell public and it
can now be set from anywhere.
When I derive a class, I often use protected to ensure that only the
derived class can access a certain property. But in this case I'm not
deriving, but it still feels to me that Environments comes from
Environment. Do you understand what I mean? It feels like Environments
should be able to access "protected" members in Environment.

Is there a better way of implementing the above if I don't want to
expose LeftmostCell?

Thanks,

Barry
Aug 20 '08 #1
4 1030
On 20 Aug., 11:06, Magnus.Morab...@gmail.com wrote:
Hi,

I am a newbie to C# and I have a simple question for you. I have the
following two classes which are used to present information in Excel.

public class Environment : ICloneable, IComparable
{
readonly string name;
readonly int id; // the id of this env
with respect to the env sql table

public int LeftmostCell; // The column number in
Excel

...
}

public class Environments : IEnumerable, ICloneable
{
TestEnvironment[] testEnvironments;

void EstablishExcelLocations()
{
...
}

The function EstablishExcelLocations sets the values of LeftmostCell.
The problem I have is that I have to make LeftmostCell public and it
can now be set from anywhere.
When I derive a class, I often use protected to ensure that only the
derived class can access a certain property. But in this case I'm not
deriving, but it still feels to me that Environments comes from
Environment. Do you understand what I mean? It feels like Environments
should be able to access "protected" members in Environment.

Is there a better way of implementing the above if I don't want to
expose LeftmostCell?

Thanks,

Barry
If both classes reside within the same assembly, you could use the
"internal" modifier to allow Environments to access LeftmostCell.

hth,
Kevin Wienhold
Aug 20 '08 #2
Well, you can make it "internal" to limit access to just the calling
assembly, and simply don't change it from your other code... maybe that is
enough? By the way, I would recommend exposing this as a property (not a
field); if you are using C# 3 (VS2008 etc) then this is trivial:

internal int LeftmostCell {get;set;}

In C# 2 (VS2005 etc) you need more code:

private int leftmostCell;
internal int LeftmostCell {
get {return leftmostCell;}
set {leftmostCell = value;}
}

Marc
Aug 20 '08 #3
On 20 Aug, 11:12, "Marc Gravell" <marc.grav...@gmail.comwrote:
Well, you can make it "internal" to limit access to just the calling
assembly, and simply don't change it from your other code... maybe that is
enough? By the way, I would recommend exposing this as a property (not a
field); if you are using C# 3 (VS2008 etc) then this is trivial:

internal int LeftmostCell {get;set;}

In C# 2 (VS2005 etc) you need more code:

private int leftmostCell;
internal int LeftmostCell {
* get {return leftmostCell;}
* set {leftmostCell = value;}

}

Marc
Thanks for the replies! By exposing as a property, am I simply
informing the user of this class as to how I intend them to use this
property?

I usually do the following -

public const string HasNoResults_English = "Has No Results"; // get,
but not set. Initialised prior to Constructor

public readonly string Name; // get, but not set. Initialised in
Constructor

public int Result // get, but not set. Value
updated after Constructor
{
get { return resultCount; }
}

public int LeftmostCell; // both get and set

Its feels tidier to me, but if its not good practice then I can stop.

Thanks,

Barry.
Aug 20 '08 #4
Public fields are indeed not /generally/ considered good practice
(especially if writeable), but ultimately it is your code. Using a property
simply separates the API from the implementation; other benefits:
* you can inject validation / notification
* you can change the implementation without impacting callers
* it'll work with data-binding (fields don't without some serious hacks)
* it'll work with interfaces
* it can be "virtual" for inheritance purposes
* if you have a mutable struct [please don't do it!] then it completely
changes the behavior - better to do that from the outset
* you can get clearer instrumentation / profiling markers (i.e. you can look
at the call-frequency of the accessors)

etc

There are no real downsides; the JIT will typically inline simple pass-thru
properties, so there is no performance penalty.

Marc
Aug 20 '08 #5

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

Similar topics

12
by: Jozef | last post by:
Hello, Does anyone here know for sure, when you do a For Each loop on a forms controls collection, does Access cycle through them in Alphabetical order or control ID or ???. Any ideas? ...
29
by: Brett | last post by:
I'd like to better understand how the following code works. I've posted questions below. namespace Something.Something1 { using System; public delegate void Test1(); public delegate void...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
20
by: BB | last post by:
Hello all, I am trying to override OnPaint in a custom textbox control (so I can drawstring a caption, etc.). In the code below, I get the "painting the form" message as expected, but not the...
0
by: webmaster | last post by:
Pardon my being a total C# noob. I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit sample application and replicate a part of the code. At a high level, I have a very...
1
by: Lee | last post by:
Hi all, been playing with some code overriding WndProc to get information about mouse events. So far I've tried to capture when the left mouse button is pressed using the code below. Sometimes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
2
Dormilich
by: Dormilich | last post by:
Hi, I'm testing my classes for a web page and I stumble upon an error I don't have a clue what it means: Error: Fatal error: Can't use method return value in write context in "output.php" on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.