Click here to Skip to main content
15,899,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class named TerminalConnectionParameters which contains a few differently typed fields, like Hostname, IPAddress, UserName, Password, etc.

I want to used this class type as a property of a Dummy class, like :
C#
public class Dummy
{
    private TerminalConnectionParameters _parameters;
    public TerminalConnectionParameters Parameters
    {
      get
      {
        // do some fancy calculation here
        return _parameters;
      }
      set
      {
        _parameters = value;
        // do some fancy calculation here
      }
    }
}


This ensures that I can get or set the whole Parameters class of a Dummy object.
So far so good, but what, if I want to use this property like :

C#
objectDummy.Parameters.Hostname= "dummyRouter";


For sure, this assignment statement will not set the value of the dummyObject's internal _parameters field. But what then ? And what is the correct way to implenment such property writer without having to create a property accessor of each field of the TerminalConnectionParameters uniquely ?
I am aware of the trivial way as below, but is there no better solution ?
C#
TerminalConnectionParameters  temp = objectDummy.Parameters;
temp.HostName = "dummyRouter";
objectDummy.Parameters = temp;


And the reason behind this question is that I want to use PropertyGrid to deal with the internal _parameters object of a Form.

What I have tried:

As outlined in the example above.
Posted
Updated 20-Oct-16 9:46am

Actually, your first assignment statement is good providing your _parameters member has been initialised - you can either do that in the declaration
C#
private TerminalConnectionParameters _parameters = new TerminalConnectionParameters();

or in the getter
C#
private TerminalConnectionParameters _parameters = null;
    public TerminalConnectionParameters Parameters
    {
      get
      {
        if (_parameters - null)
            _parameters = new TerminalConnectionParameters();
        // do some fancy calculation here
        return _parameters;

Now, once you are ensured of initialisation, all you need to do is decorate the Parameters property with the following attribute, and the designer will magically take care of the rest:
C#
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TerminalConnectionParameters Parameters
{
 
Share this answer
 
You could serialize the parameters in the dummy object, and deserialize it in the host object.
 
Share this answer
 
Thank you Midi_Mick, that was it. Somehow I felt it must be simple...:-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900