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

use of 'base.'

I have some base class fields that I am updating. I don't need to use the
'base' modifier to qualify the reference to my fields but does its use add
to readability? What do most people do in C#?

base.isValid = true;
or
isValid = true;

Both work. Which is better?

Thanks.

--

Andrew Robinson

May 16 '06 #1
12 1383
"Andrew Robinson" <ne****@nospam.nospam> wrote:
I have some base class fields that I am updating. I don't need to use the
'base' modifier to qualify the reference to my fields but does its use add
to readability? What do most people do in C#?

base.isValid = true;
or
isValid = true;

Both work. Which is better?


The 'base' qualifier is for accessing the base class's property or
method when overriding a virtual property or method. Since fields are,
by convention and for good reasons, usually private, 'base.' would not
be able to access them in a descendant class.

-- Barry
May 16 '06 #2
Would it be poor design to declare those fields as protected or should I be
using properties from my inherited class?

For example, I am creating a series of classes that handle shipping
transactions with different shippers. My program interacts with these
objects in the same way but internally, the shipping is handled very
differently from processor to processor. I created a common base class so
that at least the external properties of each 'shipper' are the same.

Make sense?

--

Andrew Robinson
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:bh********************************@4ax.com...
"Andrew Robinson" <ne****@nospam.nospam> wrote:
I have some base class fields that I am updating. I don't need to use the
'base' modifier to qualify the reference to my fields but does its use
add
to readability? What do most people do in C#?

base.isValid = true;
or
isValid = true;

Both work. Which is better?


The 'base' qualifier is for accessing the base class's property or
method when overriding a virtual property or method. Since fields are,
by convention and for good reasons, usually private, 'base.' would not
be able to access them in a descendant class.

-- Barry

May 16 '06 #3
of course. this is the way to reuse code and make application support
much easier

I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 16 '06 #4
Hi,

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
Would it be poor design to declare those fields as protected or should I
be using properties from my inherited class?

For example, I am creating a series of classes that handle shipping
transactions with different shippers. My program interacts with these
objects in the same way but internally, the shipping is handled very
differently from processor to processor. I created a common base class so
that at least the external properties of each 'shipper' are the same.

Make sense?


100%, not only that but that is the way of doing it.
May 16 '06 #5
"Andrew Robinson" <ne****@nospam.nospam> wrote:
Would it be poor design to declare those fields as protected or should I be
using properties from my inherited class?


Use properties. There's no cost to using them, only upside. The little
getter and setter functions get inlined at runtime if they're trivial.

-- Barry
May 16 '06 #6
Make your fields private and create protected properties for them. This
allows you to read the base class in isolation and make guarantees
about how the fields are manipulated both by the public and by child
classes. You can add checks into your setters to maintain guarantees in
the base class and know for sure that no other class--neither the
public nor a descendant--can break those guarantees.

If you allow direct field access, even by descendent classes, then you
cannot make any guarantees at all about the base class's state.

May 16 '06 #7
Barry,

I think that it might be better to stick with accessing protected fields in
the inherited classes since many of the properties are read/get only when
viewed from the outside. Or, I could use a public get and protected set?

--

Andrew Robinson

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:a5********************************@4ax.com...
"Andrew Robinson" <ne****@nospam.nospam> wrote:
Would it be poor design to declare those fields as protected or should I
be
using properties from my inherited class?


Use properties. There's no cost to using them, only upside. The little
getter and setter functions get inlined at runtime if they're trivial.

-- Barry

May 16 '06 #8
Thanks Bruce. Clarity at last. Makes complete sense.

--

Andrew Robinson
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Make your fields private and create protected properties for them. This
allows you to read the base class in isolation and make guarantees
about how the fields are manipulated both by the public and by child
classes. You can add checks into your setters to maintain guarantees in
the base class and know for sure that no other class--neither the
public nor a descendant--can break those guarantees.

If you allow direct field access, even by descendent classes, then you
cannot make any guarantees at all about the base class's state.

May 16 '06 #9
If you are using .NET 2.0 then you can have different permissions on
the get and the set.

In .NET 1.1 I solved this problem by creating a get-only public
property and a protected method to set the value, something like this:

public bool SomethingOrOther
{
get { return this._somethingOrOther; }
}

protected void SetSomethingOrOther(bool something)
{
this._somethingOrOther = something;
}

May 16 '06 #10
I would recommend using 'this.', because if you use 'base.' but override the
method or function in the current context ('this' class) your overrides will
be ignored.

On the other hand, use 'base.' if you're referencing something that you want
to be sure won't call back to 'this.', such as when appending functionality
to an overridden method or property.
--
Jon Davis

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:e4**************@TK2MSFTNGP03.phx.gbl...
I have some base class fields that I am updating. I don't need to use the
'base' modifier to qualify the reference to my fields but does its use add
to readability? What do most people do in C#?

base.isValid = true;
or
isValid = true;

Both work. Which is better?

Thanks.

--

Andrew Robinson

May 16 '06 #11
Barry Kelly <ba***********@gmail.com> wrote:
Use properties. There's no cost to using them, only upside.


I think there is a cost to them -- extra code and readability. Indeed,
if I were to use only the simple getter/setter properties for an
internal class, my instinct would be to declare them just as public
fields.

That way, if the complexity of your app later merits the change to
properties, you can change them to properties and recompile. (well, if
you did anything like "obj.field += 5" or passing obj.field by
reference then it would give errors to make you change them. But that
kind of error seems salutory to me.)

--
Lucian
May 17 '06 #12
Two things to note here:

1) This doesn't work for DLLs released to outside audiences with a
published contract. It works only for internal apps where you have the
luxury of recompiling both the class and all classes that use it. I
have that luxury, and maybe the OP does, but not everyone does.

2) See my earlier post about being able to read a base class and get
the warm fuzzy feeling that you know what's going on. If you expose
public fields then any client class can change them to anything, at any
time, and you have no control. I find that if I supply simply getters
and setters to the "outside world" it's usually out of laziness. I
later find that I have to go back and put some sort of validation in
the setters, in order to guarantee things about the state of my class.

May 17 '06 #13

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

Similar topics

52
by: Dick Moores | last post by:
I need to figure out how to compute pi to base 12, to as many digits as possible. I found this reference, <http://mathworld.wolfram.com/Base.html>, but I really don't understand it well enough....
2
by: Stephan Br?nnimann | last post by:
Dear all thanks to Scott Meyers (ME C++, item 33) we know assignment operators should be protected in the base class. Is there a common pattern (similar to virtual construction via `clone()')...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
7
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: //...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
1
by: miben | last post by:
I want to create a new inherited class givin its base. For example: class Base { public: void operator =(const Base &base) { // copy base items } }; class Inherits: public Base {
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.