Update an enum in a child class
I have a class Gastanker that inherts from Olietanker, I need to update the Lading enum in Olietanker for Gastanker class, can someone help me with this?
34 Replies
What do you mean by "update the Lading enum"?
hey there, I have an enum Lading in the base class it has these items : Olie, Benzeen, Desel, Nafta while in the child class the Lading enum should contain LPG, LNG, amoniak
Enums do not change values depending on where they're used, and in .net enums are basically named
int
values (starting at 0
by default)yeah you should have another enum entirely
Or one enum that holds all of the allowed values
meh
at this point use a static interface
or real classes
makes sense, thing is the inheritance has to stay there and the child class can't use the enum values from the base class
is there a way to perhaps, hide the enum from that child class?
this is a school project all about inheritance
iirc if the field/property is re-declared in a inherited class with a
new
modifier (and the new enum type) it should workright I thought so aswell, however this results in a conflict between types in the constructor
Argument 7: cannot convert from 'BL.models.Gastanker.Lading' to 'BL.models.Olietanker.Lading'
you can just pass a default value
how so?
field will not be used, so 🤷
I don't understand what you mean by default value
any value, from just
(Lading)0
or the first one of the enumlike this?
I would probably use a string to represent that value instead of an enum
you think so?
Yeah, or merge all the values of the different
Lading
enumsteacher just said it could be any of these values so I thought of enums
sure, but how do I make sure that the base class only uses the last 4 and the child class the first 3
You could check the range in the constructors but that gets weird for the base class constructors
I've never seen this type of enum usage where each derived class has its own publicly exposed set of enum properties. I would just use something like a string in this case
there is really no way of ovveriding these enums
I'll do that for now then, thank you all @br4kejet @dont @ded
Having your oil tanker class derived from the gas tanker class seems a bit odd though, would it not be better to make them both extend Containerschip?
they both use volume tho while Containerschip doesn't
Or create a class between Containerschip and the oil/gas tanker classes that has those properties like lengths, voluime, etc.
like
BaseTanker
intresting
Then the oil and gas tankers derive from that class, and then you could do what you're doing now where the oil and gas tankers have their own enum things
and BaseTanker inherits from Containerschip?
Yeah
alright I'll go for this one then, thanks man
The thing about OOP is sometimes you have to redefine the same properties for different classes because extending a base class isn't really possible, and if you ever run into that, you might have to use some sort of composition class that contains those properties that can be shared across different types. You might not run into it, but it's an annoying thing to deal with when using complicated inheritance 😛
exactly, also the problem is that you can only inherit one concrete class