Click here to Skip to main content
15,398,357 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Team, in C# don't know how to initialize below SiteExternalId Class with some dummy values, help me

public class SiteExternalId
{
    public SiteExternalIdItem[] Item { get; set; }
}

public class SiteExternalIdItem
{
    public ExternalTypeCd ExternalIdTypeCd { get; set; }

    public string SiteNumber { get; set; }
}

public class ExternalTypeCd
{
    public string ExternalIdTypeName { get; set; }

    public string SiteCode { get; set; }
}


What I have tried:

I'm trying this way, but not success

SiteExternalId = new SiteExternalId
{
    Item = new SiteExternalIdItem[]
    {
        new ExternalTypeCd {ExternalIdTypeName = "A", SiteCode = "X"}
    }
}


If possible could you give me initialization in a single line. Thanks
Posted
Updated 25-Jun-22 6:28am

1 solution

Look at your definitions: Item is an array of SiteExternalIdItem objects, but your code is trying to fill it with ExternalTypeCd objects.
You need something like this:
C#
SiteExternalId = new SiteExternalId
{
    Item = new SiteExternalIdItem[]
    {
        new SiteExternalIdItem
        {
            ExternalTypeCd = new ExternalTypeCd {ExternalIdTypeName = "A", SiteCode = "X"},
            SiteNumber = "666"
        }
    }
}
   

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