Good brain teaser (real life problem)

  • Thread starter Thread starter arthernan
  • Start date Start date
A

arthernan

Let's say I have this class.

public class oompa_loompa: halfling
{
public var automobile;
}

public first_oompa_loompa oompa_loompa = new oompa_loompa();
 
Apart from the fact that you got your declaration backward (I think
that you meant to say:

public oompa_loompa first_oompa_loompa = new oompa_loompa();

), if I may take the liberty of rephrasing your question:

"Can a base class method change the value of one of the fields of its
derived class?"

So long as the base class has a reference to an instance of the derived
class, I don't see why not. However, from the base class's point of
view, the derived class object is just another object instance, like
any other object in the system. Unless you use Reflection, that is.

So, you could use Reflection to ask the question, "Do I have a field
called automobile?" and alter that field if you did.

Or, you could pass a reference to the derived class to its own base
class method and have the base class treat the derived class argument
like any other object (and so be able to manipulate any public fields,
properties, etc.)

However, both of these would be gross abuses of inheritance. Why do you
want to do something like this?
 
This is using the deprecated oompa_loompa API. oompa_loompa.find was
superceeded in the IOompaLoompa interface from the October CTP for CF2005
project codenamed "Lindt"

Try implementing IOompaLoompa.DoompetyDoo(string _message);

If this doesn't resolve your problem a good workaround will be to replace
your everlasting gum and turn the sound on the teevee right down.

HTH....

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Let's say I have this class.

public class oompa_loompa: halfling
{
public var automobile;
}

public first_oompa_loompa oompa_loompa = new oompa_loompa();

.
.
.

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?

I have thought of a couple of alternatives but there ought to be an
easier way.

Arturo Hernandez



All the variable names on this posting have been changed to protect the
identity of the real variables.


PS: If anyone says MVP's don't have a sense of humour I'll be round there to
sort you out.
 
Bob said:
This is using the deprecated oompa_loompa API. oompa_loompa.find was
superceeded in the IOompaLoompa interface from the October CTP for CF2005
project codenamed "Lindt"

Try implementing IOompaLoompa.DoompetyDoo(string _message);

If this doesn't resolve your problem a good workaround will be to replace
your everlasting gum and turn the sound on the teevee right down.

You know, it gets very frustrating when so-called MVP's give invalid
information. As all of us *true* geeks know... they are everlasting
GOBSTOPPERS. Geez.

:)

Matt
 
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.
--
 
Back
Top