Malfunction Posted August 21, 2004 Posted August 21, 2004 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 ... Quote Debug me...
Administrators PlausiblyDamp Posted August 21, 2004 Administrators Posted August 21, 2004 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; } } } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Malfunction Posted August 21, 2004 Author Posted August 21, 2004 The compiler tells me that I can't do that. "A static member can't be declared as override, abstract or virtual." Quote Debug me...
Administrators PlausiblyDamp Posted August 21, 2004 Administrators Posted August 21, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
John_0025 Posted August 23, 2004 Posted August 23, 2004 (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 August 23, 2004 by John_0025 Quote
Joe Mamma Posted August 23, 2004 Posted August 23, 2004 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; Quote 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.
Joe Mamma Posted August 23, 2004 Posted August 23, 2004 Ouch!!! that doesnt work!!! Quote 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.
Joe Mamma Posted August 23, 2004 Posted August 23, 2004 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 Quote 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.
Joe Mamma Posted August 23, 2004 Posted August 23, 2004 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()); } } Quote 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.
Joe Mamma Posted August 23, 2004 Posted August 23, 2004 oh yeah . . . calling it would be - call it via: int mcWheelCount = Vehicle.NumWheels(typeof(MotorCycle)); or MotorCycle mc = new MotorCycle(); mcWheelCount = mc.NumWheels(); Quote 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.
John_0025 Posted August 27, 2004 Posted August 27, 2004 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 :) Quote
Joe Mamma Posted August 31, 2004 Posted August 31, 2004 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 } Quote 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.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.