C
C#•9mo ago
TheUnquiet

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
TheUnquiet
TheUnquietOP•9mo ago
public class Gastanker : Olietanker
{
public enum Lading
{
LPG,
LNG,
amoniak
}

public Gastanker(double lengte, double breedte, int tonnage, string naam, int aantalContainers, double volume, Lading lading) : base(lengte, breedte, tonnage, naam, aantalContainers, volume, lading)
{
}
}
public class Olietanker : Containerschip
{
public double Volume { get; set; }
public Lading _lading { get; set; }
public enum Lading
{
Olie,
Benzeen,
Desel,
Nafta
}
public Olietanker(double lengte, double breedte, int tonnage, string naam, int aantalContainers, double volume, Lading lading) : base(lengte, breedte, tonnage, naam, aantalContainers)
{
Volume = volume;
_lading = lading;
}
}
public class Gastanker : Olietanker
{
public enum Lading
{
LPG,
LNG,
amoniak
}

public Gastanker(double lengte, double breedte, int tonnage, string naam, int aantalContainers, double volume, Lading lading) : base(lengte, breedte, tonnage, naam, aantalContainers, volume, lading)
{
}
}
public class Olietanker : Containerschip
{
public double Volume { get; set; }
public Lading _lading { get; set; }
public enum Lading
{
Olie,
Benzeen,
Desel,
Nafta
}
public Olietanker(double lengte, double breedte, int tonnage, string naam, int aantalContainers, double volume, Lading lading) : base(lengte, breedte, tonnage, naam, aantalContainers)
{
Volume = volume;
_lading = lading;
}
}
Keswiik
Keswiik•9mo ago
What do you mean by "update the Lading enum"?
TheUnquiet
TheUnquietOP•9mo ago
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
Keswiik
Keswiik•9mo ago
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)
Omnissiah
Omnissiah•9mo ago
yeah you should have another enum entirely
Keswiik
Keswiik•9mo ago
Or one enum that holds all of the allowed values
Omnissiah
Omnissiah•9mo ago
meh at this point use a static interface or real classes
TheUnquiet
TheUnquietOP•9mo ago
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
Omnissiah
Omnissiah•9mo ago
iirc if the field/property is re-declared in a inherited class with a new modifier (and the new enum type) it should work
TheUnquiet
TheUnquietOP•9mo ago
right 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'
Omnissiah
Omnissiah•9mo ago
you can just pass a default value
TheUnquiet
TheUnquietOP•9mo ago
how so?
Omnissiah
Omnissiah•9mo ago
field will not be used, so 🤷
TheUnquiet
TheUnquietOP•9mo ago
I don't understand what you mean by default value
Omnissiah
Omnissiah•9mo ago
any value, from just (Lading)0 or the first one of the enum
TheUnquiet
TheUnquietOP•9mo ago
like this?
public Gastanker(double lengte, double breedte, int tonnage, string naam, int aantalContainers, double volume, Lading lading = 0) : base(lengte, breedte, tonnage, naam, aantalContainers, volume, lading)
{
}
public Gastanker(double lengte, double breedte, int tonnage, string naam, int aantalContainers, double volume, Lading lading = 0) : base(lengte, breedte, tonnage, naam, aantalContainers, volume, lading)
{
}
br4kejet
br4kejet•9mo ago
I would probably use a string to represent that value instead of an enum
TheUnquiet
TheUnquietOP•9mo ago
you think so?
br4kejet
br4kejet•9mo ago
Yeah, or merge all the values of the different Lading enums
TheUnquiet
TheUnquietOP•9mo ago
teacher just said it could be any of these values so I thought of enums
br4kejet
br4kejet•9mo ago
public enum Lading
{
LPG,
LNG,
amoniak,
Olie,
Benzeen,
Desel,
Nafta
}
public enum Lading
{
LPG,
LNG,
amoniak,
Olie,
Benzeen,
Desel,
Nafta
}
TheUnquiet
TheUnquietOP•9mo ago
sure, but how do I make sure that the base class only uses the last 4 and the child class the first 3
br4kejet
br4kejet•9mo ago
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
TheUnquiet
TheUnquietOP•9mo ago
there is really no way of ovveriding these enums I'll do that for now then, thank you all @br4kejet @dont @ded
br4kejet
br4kejet•9mo ago
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?
TheUnquiet
TheUnquietOP•9mo ago
they both use volume tho while Containerschip doesn't
br4kejet
br4kejet•9mo ago
Or create a class between Containerschip and the oil/gas tanker classes that has those properties like lengths, voluime, etc. like BaseTanker
TheUnquiet
TheUnquietOP•9mo ago
intresting
br4kejet
br4kejet•9mo ago
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
TheUnquiet
TheUnquietOP•9mo ago
and BaseTanker inherits from Containerschip?
br4kejet
br4kejet•9mo ago
Yeah
TheUnquiet
TheUnquietOP•9mo ago
alright I'll go for this one then, thanks man
br4kejet
br4kejet•9mo ago
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 😛
Omnissiah
Omnissiah•9mo ago
exactly, also the problem is that you can only inherit one concrete class
Want results from more Discord servers?
Add your server