c# - Accessing class fields using a variable set to that class -
i have situation in have class called myclasses , 2 sub classes called subone , subtwo.
class myclasses { class subone { public const sofieldone = "blahblah"; public const sofieldtwo = "blahblahblah"; } class subtwo { public const stfieldone = "blahblah"; } } i want able set variable either class subone or subtwo based on argument passed 1 of methods. want able access members within subone or subtwo using general variable.
for example if argument "usesubone" want set variable subtouse so...
subtouse = myclasses.subone; i should able access sofieldone typing following...
subtouse.sofieldone how go doing this?
polymorphism not apply constants. here's how code instead:
private class myclasses { private class subone { public virtual string sofieldone { { return "blahblah"; } } } private class subtwo : subone { public override string sofieldone { { return "something else"; } } } } now can create new variable this:
subone somevariable = new subtwo(); console.writeline(somevariable.sofieldone); // prints "something else"
Comments
Post a Comment