drewwyatt
drewwyatt

Reputation: 6027

How can a child object access the properties of its parent?

I have two classes: Message and Emailer with the following (example) properties:

How can I set this up so that the Emailer instance assigned to Message can access the properties of method? e.g.:

public class Message
{
    public string Name;
    public string EmailAddress;
    public string Text;
    public Emailer Email = new Emailer();

    public Message(string name, string emailAddress, string text)
    {
        this.Name = name;
        this.EmailAddress = emailAddress;
        this.Text = text;        
    }
}

public class Emailer
{
    public void Send()
    {
        // Send email using Message Properties
    }
}

Message myMessage = new Message('Joe Blow', '[email protected]', 'Boom Boom Pow');
myMessage.Email.Send();

Upvotes: 0

Views: 63

Answers (5)

mason
mason

Reputation: 32693

While the other answers are correct and you wouldn't want to have a Message that owns an Emailer, here's a way that you could do that. I'm providing this simply so you'll understand how a child could know about its parent.

public class Message
{
    public string Name;
    public string EmailAddress;
    public string Text;
    public Emailer Email = new Emailer();

    public Message(string name, string emailAddress, string text, Emailer emailer)
    {
        this.Name = name;
        this.EmailAddress = emailAddress;
        this.Text = text;
        this.Email= emailer;        
    }
}

Then, to create the Message, you'd do this from inside the Emailer:

Message mymessage=new Message("John Doe","[email protected]", "Hello, world!", this);

I'm not advocating that you do this, just demonstrating how it could be done for educational purposes.

Upvotes: 0

Habib
Habib

Reputation: 223207

There is no parent-child relationship / inheritance between Message and Emailer , It is more like composition, where Message contains object of Emailer. You can't access Message properties in Emailer.

Your method Send in Emailer should receive an object of type Message and then send email accordingly.

public static class Emailer
{
    public static void Send(Message message)
    {
    }
}

Your class Emailer looks more like a utility class responsible for sending messages. You can declare that as static and then use it like:

Message myMessage = new Message('Joe Blow', '[email protected]', 'Boom Boom Pow');
Emailer.Send(myMessage);

Upvotes: 1

Håkan Fahlstedt
Håkan Fahlstedt

Reputation: 2095

Send the Message object as a parameter in the Emailer constructor:

public Emailer Email = new Emailer(this);

Then you can have acces to the parent:

public class Emailer
{
    private Message _message;

    public Emailer(Message mesage)
{
    _message = message;
}

    public void Send()
    {
        // Send email using Message Properties
        // use _message
    }
}

Upvotes: 0

Servy
Servy

Reputation: 203814

In this case you're parent;child relationship is backwards. The message shouldn't need to know anything about the emailer that sends it. The Send method of Emailer should be given a Message as a parameter:

public class Message
{
    public string Name;
    public string EmailAddress;
    public string Text;

    public Message(string name, string emailAddress, string text)
    {
        this.Name = name;
        this.EmailAddress = emailAddress;
        this.Text = text;        
    }
}

public class Emailer
{
    public void Send(Message message)
    {
        // Send email using Message Properties
    }
}

Upvotes: 0

SLaks
SLaks

Reputation: 887275

Your hierarchy is wrong.

A Message should not own an Emailer. Instead, an Emailer should accept a Message in its Send() method.
To put it differently, an Emailer instance should not be tied to a single Message; instead, it should be able to send as many emails as you want.


The actual answer to your question is, you can't.

Instead, you can pass the object as a constructor parameter.

Upvotes: 1

Related Questions