473,406 Members | 2,369 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,406 software developers and data experts.

Inheriting Consts

Jon
Hello all,

Am I missing something quite simple as I can't seem to see how to
inherit consts from my base class.

Can this be done?

Thanks,

Jon
www.nantwichonline.com

Oct 18 '07 #1
5 1494
On Oct 18, 2:41 pm, Jon <JonMYa...@gmail.comwrote:
Am I missing something quite simple as I can't seem to see how to
inherit consts from my base class.

Can this be done?
What exactly do you mean? You should be able to refer to constants (so
long as they're accessible) from a derived class. For example:

using System;

class Base
{
public const int TestConstant = 10;
}

class Test : Base
{
static void Main()
{
Console.WriteLine (TestConstant);
}
}

Is your constant private by any chance? (This is the default if you
don't specify an access level.)

Jon

Oct 18 '07 #2
"Jon" <Jo*******@gmail.comwrote:
Am I missing something quite simple as I can't seem
to see how to inherit consts from my base class.
You can't override the value of a constant in a derived class, because
constants are static members.

Eq.
Oct 18 '07 #3
Paul,

then how do you solve in C# cases like (I do not draw the inheritence
lines, I hope it is obvious):

Class Vehicle with static constant: NumberOfWheels

Class Auto: NumberOfWheels = 4; Class Bycicle: NumberOfWheels = 2;
Class Amphibian: NumberOfWheels =0;

There should be only one NumberOfWheels in each class (because it is
common for all instances), but they should have different values.

Thank you,
Janos

Oct 18 '07 #4
On Oct 18, 4:02 pm, Janos <jkazs...@intARTS.comwrote:
then how do you solve in C# cases like (I do not draw the inheritence
lines, I hope it is obvious):

Class Vehicle with static constant: NumberOfWheels

Class Auto: NumberOfWheels = 4; Class Bycicle: NumberOfWheels = 2;
Class Amphibian: NumberOfWheels =0;

There should be only one NumberOfWheels in each class (because it is
common for all instances), but they should have different values.
The number of wheels should be related to an *instance*, with Vehicle
having a protected constructor taking the number of wheels. Auto would
always pass 4, Bicycle would always pass 2 etc.

It has to be instance data otherwise you can't ask an arbitrary
vehicle how many wheels it has.

An alternative if there are many similar values is to have flyweight
"VehicleKind" with Auto, Bicycle etc instead of using polymorphism -
each vehicle would have a VehicleKind which in turn would know how
many wheels it had etc.

Jon

Oct 18 '07 #5
"Janos" <jk******@intARTS.comwrote:
then how do you solve in C# cases like [...]
Class Vehicle with static constant: NumberOfWheels
Class Auto: NumberOfWheels = 4;
Class Bycicle: NumberOfWheels = 2;
abstract class Vehicle
{
public abstract int NumberOfWheels { get; }
}

class Auto : Vehicle
{
private const int WHEELS = 4;
public override int NumberOfWheels { get { return WHEELS; } }
}

Eq.
Oct 18 '07 #6

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

Similar topics

1
by: richardlane | last post by:
Hi, basic question here - I'm struggling with the transfer from asp to asp.net a bit, especially in seeing the 'bigger picture' of how things are best structured. I have a website made up...
15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
4
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor...
1
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
7
by: Christopher | last post by:
Where is the proper place to assign the value to a public static const variable that is a member of a class? My compiler is angry with me, but I don't understand why. It doesn't mind any other...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.