How to override Field
-
Dear Sir and Madam, It is possible to override method, but how to override field. Thank You very much. public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? }
-
Dear Sir and Madam, It is possible to override method, but how to override field. Thank You very much. public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? }
I really don't think you can override a field, never did it anyway... I can't see why you would override a field. Overriding a field isn't really OOP i guess ... But if you really want something that is close to the thing you want, you can use properties: Instead of this: public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? } use this: public class BaseClass { private int myInt = 0; protected virtual int i { get { return myInt;} } } public class SuperClass : BaseClass { private int otherInt = 0; protected override int i { get { return otherInt; } } } I didn't implement the set, it is just to show you the picture -- modified at 5:09 Friday 11th August, 2006 -- modified at 5:09 Friday 11th August, 2006
Students in Belgium still come in handy, don't they ?
-
I really don't think you can override a field, never did it anyway... I can't see why you would override a field. Overriding a field isn't really OOP i guess ... But if you really want something that is close to the thing you want, you can use properties: Instead of this: public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? } use this: public class BaseClass { private int myInt = 0; protected virtual int i { get { return myInt;} } } public class SuperClass : BaseClass { private int otherInt = 0; protected override int i { get { return otherInt; } } } I didn't implement the set, it is just to show you the picture -- modified at 5:09 Friday 11th August, 2006 -- modified at 5:09 Friday 11th August, 2006
Students in Belgium still come in handy, don't they ?
-
Lol sir, I am just some noob student in a country no1 ever heared of :p (Belgium)
Don't you also love the code?