Common tasks in base classes, how?
-
ok next q ... how do you access inputs into a constructor within the base class? my base class constructor: protected int commonID; // this is common to all object public BaseClass{ console.write commonID; // or something that accesses id } my sub-class constrictor: public myClass(int id) { commonID = id; } but when I instantiate "myClass", before it sets commonID, it goes to the BaseClass constructor. But commonID is null cause it hasnt been set. So is there a way for me to get "id" into the baseclass? or get commonID set? M
-
ok next q ... how do you access inputs into a constructor within the base class? my base class constructor: protected int commonID; // this is common to all object public BaseClass{ console.write commonID; // or something that accesses id } my sub-class constrictor: public myClass(int id) { commonID = id; } but when I instantiate "myClass", before it sets commonID, it goes to the BaseClass constructor. But commonID is null cause it hasnt been set. So is there a way for me to get "id" into the baseclass? or get commonID set? M
Here are your two constructors rewritten:
public BaseClass(int id)
{
commonID = id;
console.Write(commonID);
}public myClass (int id) : base(id)
{
}
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog
-
ok next q ... how do you access inputs into a constructor within the base class? my base class constructor: protected int commonID; // this is common to all object public BaseClass{ console.write commonID; // or something that accesses id } my sub-class constrictor: public myClass(int id) { commonID = id; } but when I instantiate "myClass", before it sets commonID, it goes to the BaseClass constructor. But commonID is null cause it hasnt been set. So is there a way for me to get "id" into the baseclass? or get commonID set? M
base class: protected int commonID; // this is common to all object public BaseClass{ } public void DoOtherThing { console.write commonID; // or something that accesses id } /////////////////////////////////////////////// sub-class: public myClass(int id) { commonID = id; DoOtherThing(); } Was it a cat I saw?
-
Here are your two constructors rewritten:
public BaseClass(int id)
{
commonID = id;
console.Write(commonID);
}public myClass (int id) : base(id)
{
}
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog
dude, that was perfect, it worked. thanks! Moazzam
-
base class: protected int commonID; // this is common to all object public BaseClass{ } public void DoOtherThing { console.write commonID; // or something that accesses id } /////////////////////////////////////////////// sub-class: public myClass(int id) { commonID = id; DoOtherThing(); } Was it a cat I saw?
But with this code the purpose of having the base class is somewhat lost as your method requires the subclasses to specifically call DoOtherThing() rather than properly inherite the behaviour of the base constructor.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog