Home > Software engineering >  c# - How to Populate a complex property
c# - How to Populate a complex property

Time:01-29

I have the following classes :

public class a
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    private object details { get; set; }
    public object Details
    {
        get
        {
            if (Type == "One")
                details = new DotOne();
            if (Type == "Two")
                details = new DotTwo();
            return details;
        }
        set { details = value; }
    }
}
public class DotOne
{
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
}
public class DotTwo
{
    public string d { get; set; }
    public string e { get; set; }
}

However, although I can populate DotOne and Two separately and then add it to the main a Like :

var newDotOne = New DotOne()
{
    a = "Test One",
    b = "Test Two",
    c = "Test Three"
}
var NewA = new a()
{
    Id = "1234",
    Name = "Adam",
    Type = "One",
    Details = newDotOne
}

But I can only populate it if I know the choice, and I can't access it any other way Therefore It will be greatly appreciated if this could be fixed, either by changing the classes or a way to access "Details"

Edited

As per @NetMage 's suggestion and to clearify the need, further, here is what i need to achieve:

var NewA = new a()
{
    Id = "1234",
    Name = "Adam",
    Type = "One",
    Details = 
    {
        a = "Test One",
        b = "Test Two",
        c = "Test Three"
    }
}

or :

a.Details.b = "New Test";

CodePudding user response:

You could use a generic type to achieve this easily. Refactor your class as so:

public class a<TypeOfDetails>
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public TypeOfDetails Details { get; set; }
}
public class DotOne
{
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
}
public class DotTwo
{
    public string d { get; set; }
    public string e { get; set; }
}

Then, you can instantiate your a class with the appropriate details type like so:

var newDotOne = New DotOne()
{
    a = "Test One",
    b = "Test Two",
    c = "Test Three"
}
var NewA = new a<DotOne>()
{
    Id = "1234",
    Name = "Adam",
    Type = "One",
    Details = newDotOne
}

CodePudding user response:

    private string type;
    public string Type
    {
        get { return type; }
        set
        {
            if (value == "One")
                Details = new DotOne();
            if (value == "Two")
                Details = new DotTwo();
            type = value;
        }
    }
    
  
    private object details;
    public object Details { get { return details; } set { details = value;} }  

i will post all the code . please call the func createobj to test it.

void Createobj()
{
     var obj = new a() { Type = "One" };
    obj.Details = new DotOne { a = "a", b = "b", c = "c" };
    obj.Type = "Two";
    obj.Details = new DotTwo { d = "d" , e = "e" };

    var x = obj.Details as DotTwo; 
    x.d = "dddd";
    obj.Details = x;
    
}

public class a
{
    private string type;
    public string Type
    {
        get { return type; }
        set
        {
            if (value == "One")
                Details = new DotOne();
            if (value == "Two")
                Details = new DotTwo();
            type = value;
        }
    }


    private object details;
    public object Details { get { return details; } set { details = value; } }


}
public class DotOne
{
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
}
public class DotTwo
{
    public string d { get; set; }
    public string e { get; set; }
}

check here

check img

  •  Tags:  
  • Related