Hi Arturo,
Let's say I have this class.
public class oompa_loompa: halfling
{
public var automobile;
}
public oompa_loompa first_oompa_loompa = new oompa_loompa(); // Fixed by
nick .
.
first_oompa_loompa.find()
Is it possible to call first_oompa_loompa.find() and have the halfling
class, instantiate an automobile object and set first_oompa_loompa.var
to reference that new instance?
Use a constructor:
public class oompa_loompa: halfling
{
public var automobile;
public oompa_loompa()
{
automobile = new var();
}
}
Now, when you create the oompa_loompa object, the constructor will create
the automobile object, and 'first_oompa_loompa' will behave just like a
halfling with an extra public property of 'automobile'. All this before you
even call 'find'.
On the other hand, if 'find' is supposed to find the automobile object, then
you will want to take a look at the Template Method pattern.
public interface ITransportation
{
public void LoadFromDatabase();
}
public class halfling
{
public abstract ITransportation ReturnTransport();
public void find()
{
ITransportation txp = this.ReturnTransport();
txp.LoadFromDatabase();
// do some interesting things here...
}
}
public class oompa_loompa : halfling
{
public ITransportation automobile;
public oompa_looma() // constructor
{
automobile = new PersonalTransport(4,4); // create a new object of
type PersonalTransport with four doors and four wheels.
}
public ITransportation ReturnTransport()
{
return automobile;
}
}
Use
http://search.msn.com to find out more about the "template method design
pattern"
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--