Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I want to create a baseclass called vehicle. It has an int property MinWheels. Each deriving class has a defined number if minimum wheels (e.g. car=4, motorcycle=2, truck=6).

 

The derived vehicles are to be created through a dialog. The user is to select the type of vehicle first than a pop-up will ask for the number of wheels the user wants the specified vehicle to have (the pop-up will check for the minimum number of wheels for that type).

 

The selected number of wheels is passed to the constructor so the MinWheels proerty needs to be checked before an instance has been created.

I thought to make the MinWheels property static so the dialog can retrieve it through the classname but this way the derived classes can't override the MinWheels property.

 

Another way would be to make MinWheels an object property. Then I'd have to create an instance without initializing the wheels retrieving MinWheels and call a method to init the wheels.

 

I know I'm somehow missing something here cause there's an elegant solution to this ... but I don't get it :o

 

plz help this lost soul ...

Debug me...
  • Administrators
Posted

You should be able to declare a property as both static and virtual, should work then

public class Class1
{

	public virtual static int MaxWheels
	{
	get {return 1;}
	}

}
public class Class2 : Class1
{
	public static override int MaxWheels
	{
		get
		{
			return base.MaxWheels;
		}
	}

}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted

Could have sworn it compiled here but I get the same error :confused: even stranger is that the intellisense generated that code for me :confused:

I suppose you are probably best exposing the number of wheels and the minimum number of wheels as properties and do the creation in two stages like you suggest.

Alternatively the constructor could throw an error (possibly bad) or simply set the required minimum if the number specified was too low - possibly raise an event to indicate this has happened.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

You can use the new keyword to do it.

 

public class Class1
{
	public static int MaxWheels
	{
	get {return 1;}
	}
}
public class Class2 : Class1
{
	public static new int MaxWheels
	{
		get
		{
			return 4;
		}
	}

}

 

I'm not sure why you would want to do this. vechicleBase.MinimumWheels is meaningless without knowing what type of vechicle it is.

I would just have a static field in the derived classes.

 

public class baseVechicle
{

public virtual int MinimumWheels
{
	get
	{
		return 0;
	}
}
}


public class Car: baseVechicle
{
public static int minWheels = 4;
public Car (int wheels)
{
	
	if(wheels < minWheels)
	{
		//do error stuff
	}
}
public override int MinimumWheels
{
	get
	{
		return minWheels;
	}
}

}

public class UserClass
{
public void main()
{
	baseVechicle myVechicle;

	int test = Car.minWheels;
	myVechicle = new Car(5);
	int anotherTest = myVechicle.MinimumWheels;
}
}

Edited by John_0025
Posted

class MotorCycle:Vehicle{};
class Automobile:Vehicle{};
class Truck:Vehicle{};

abstract class Vehicle
{
public static int NumWheels(Type aVehicleType)
{
switch (aVehicleType)
{
 case MotorCycle: return 2; 
 case Automobile: return 4; 
 case Truck: return 6; 
 default: throw new Exception( string.Format("{0} is not a vehicle", aVehicleType.Name)); 
}
}

public int NumWheels
{
get
{
 return NumWheels(this.GetType());
}
}
}

 

call it via:

int mcWheelCount = Vehicle.NumWheels(MotorCycle);

 

or

 

MotorCycle mc = new MotorCycle();

mcWheelCount = mc.NumWheels;

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Ouch!!! that doesnt work!!!

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Ouch!!! that doesnt work!!!

hold on. . . let me think about this. . .

 

Its going to be something along these lines. . .

 

this is the kind of stuff I like

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

ok this . . .

class MotorCycle:Vehicle{};
class Automobile:Vehicle{};
class Truck:Vehicle{};
abstract class Vehicle
{
  public static int NumWheels(Type aVehicleType)
  {
  if (aVehicleType == typeof(MotorCycle))
	 return 2; 
  if (aVehicleType == typeof(Automobile))
	 return 4; 
  if (aVehicleType == typeof(Truck))
	 return 6; 
  throw new Exception( string.Format("{0} is not a vehicle", aVehicleType.Name)); 
  }
  public int NumWheels()
  {	  
  return NumWheels(this.GetType());
  }
} 

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

oh yeah . . .

calling it would be -

 

call it via:

int mcWheelCount = Vehicle.NumWheels(typeof(MotorCycle));

 

or

 

MotorCycle mc = new MotorCycle();

mcWheelCount = mc.NumWheels();

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

In OOP you need to be careful about the way you structure your code. Simple having code that works isn't always correct.

 

The value for the number of wheels should be a property of the derived classes not the base 'vehicle' class. It may seem quite trivial but it means you would have to update the base class every time you derived a new class from it and this isn't quite right for OOP. Imagine if you needed to edit and rebuild 'System.Windows.Forms.Form' class everytime you made a new form.

 

I guess in this project it won't matter at all but it's good to start good habits :)

Posted

I agree with you that the implementation is a bit clunky. . .

 

but I believe that NumWheels needs to be part of the base class in this instance. Now perhaps this should simply be an enumeration to minimize code writing.

 

 

public Class Vehicle

{

Enum MaxWheelCount{ UniCycle = 1, Motorcycle = 2, Car = 4, Truck = 6};

// Additional Methods and properties

}

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...