Initialize a Class with Array of another complex type

rajeshnrh

New member
Joined
Jun 5, 2019
Messages
4
Programming Experience
3-5
Team, I'm learning C#, don;'t know how to initialize below Class with some dummy values, help me
C#:
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; }
    }
 
Last edited by a moderator:
You didn't say whether you wanted to do this initialization declaratively or imperatively. Below is how to do it imperatively:

You just need to work your way backwards through the heirarchy. Instantiate the lowest level objects and hold references to them. Instantiate the next level up and hold references to these as well, but put them in an array. Assign references from the lowest level objects into the properties of the next higher ones. Lastly instantiate the top level object. Assign the reference to the array to the property of the top level object that needs the array.
 
You didn't say whether you wanted to do this initialization declaratively or imperatively. Below is how to do it imperatively:

You just need to work your way backwards through the heirarchy. Instantiate the lowest level objects and hold references to them. Instantiate the next level up and hold references to these as well, but put them in an array. Assign references from the lowest level objects into the properties of the next higher ones. Lastly instantiate the top level object. Assign the reference to the array to the property of the top level object that needs the array.
 
Thanks Skydiver for your response.
Actually I'm trying this way, but not success
C#:
SiteExternalId = new SiteExternalId
{
Item = new SiteExternalIdItem[]
{
new ExternalTypeCd {ExternalIdTypeName = "A", SiteCode = "X"}
}
},
If possible could you give me initialization in a single line. Thanks
 
Last edited by a moderator:
So you are trying to do it declaratively. Post the error message that you are getting. C#'s error messages are typically a lot more helpful than C/C++ error messages. Part of learning to become a C# programmer is learning how to understand the error messages.

We could just give you the answer, but does serve you long term.
 
So you are trying to do it declaratively. Post the error message that you are getting. C#'s error messages are typically a lot more helpful than C/C++ error messages. Part of learning to become a C# programmer is learning how to understand the error messages.

We could just give you the answer, but does serve you long term.
Sir, it would be fine if you provide an example of initialize below objects, so I can learn from you
C#:
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; }
    }
 
Last edited by a moderator:
In modern versions of C# you can skip mentioning the class name after new if the compiler can work out what the type is (e.g. by looking at the name of the property you're using on the left side of =, then looking at the type of that named property:

C#:
            SiteExternalId seid = new()
            {
                Item = new[]
                {
                    new SiteExternalIdItem() { ExternalIdTypeCd = new() { ExternalIdTypeName = "Hello", SiteCode = "World" }, SiteNumber = "Foo" },  //this first one has the type name after the new, to indicate the type for the other elements in the array. It could also go before the [] above
                    new() { ExternalIdTypeCd = new() { ExternalIdTypeName = "Goodbye", SiteCode = "World" }, SiteNumber = "Bar" }
                }
            };

There are so many ways to skin this cat. You could even write JSON (or.. this is how you might deser a file/string of JSON to these objects):

C#:
        var seid = JsonConvert.DeserializeObject<SiteExternalId>(@"{ ""Item"": [
          {""ExternalIdTypeCd"": { ""ExternalIdTypeName"": ""Hello"", ""SiteCode"": ""World"" }, ""SiteNumber"": ""Foo"" },
          {""ExternalIdTypeCd"": { ""ExternalIdTypeName"": ""Goodbye"", ""SiteCode"": ""World"" }, ""SiteNumber"": ""Bar"" },
        ]}");

I'm trying this way, but not success

Yes, you have a few syntax errors there. You wrote this:

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

Minimally you need this:

C#:
            SiteExternalId x = new SiteExternalId
            {
                Item = new SiteExternalIdItem[]
                {
                  new () {ExternalIdTypeCd = new ExternalTypeCd {ExternalIdTypeName = "A", SiteCode = "X"} },
                }
            };

I added these bits inside the green boxes:

1656194890253.png
 
Last edited:
Back
Top Bottom