Home > Back-end >  C# array of type[][] how to build it?
C# array of type[][] how to build it?

Time:12-31

I'm having an issue defining/Creating a valid assignment to a Type defined as AddOnTypeV17[][]

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Order=11)]
        [System.Xml.Serialization.XmlArrayItemAttribute("RequiresOneOf")]
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false, NestingLevel=1)]
        public AddOnTypeV17[][] RequiresAllOf {
            get {
                return this.requiresAllOfField;
            }
            set {
                this.requiresAllOfField = value;
                this.RaisePropertyChanged("RequiresAllOf");
            }
        }

The Type is defined:

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://stamps.com/xml/namespace/2021/01/swsim/SwsimV111")]
    public enum AddOnTypeV17 {        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("SC-A-HP")]
        SCAHP,
//Remainder removed for clarity
    }

This is what I tried with Error:

        RequiresAllOf = GetAddons(), //Set add on of Hidden dollars.
    private AddOnTypeV17[][] GetAddons()
    {
      List<AddOnTypeV17>[][] ja = new List<AddOnTypeV17>
      {
        new AddOnTypeV17 { AddOnTypeV17.SCAHP },
      };
      return ja;
    }

The error(s) I get are:

Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<USPostal.StampsIM.AddOnTypeV17>[][]' to 'USPostal.StampsIM.AddOnTypeV17[][]' < on line: return ja;

and

Error CS1922 Cannot initialize type 'AddOnTypeV17' with a collection initializer because it does not implement 'System.Collections.IEnumerable' < on line: new AddOnTypeV17....

Expected serialization is:

       <ns1:AddOns>
          <ns1:AddOnV17>
            <ns1:Amount>0.00</ns1:Amount>
            <ns1:AddOnType>SC-A-HP</ns1:AddOnType>
          </ns1:AddOnV17>
        </ns1:AddOns>

I know I'm missing something obvious. Anyone with an idea as to what? Thanks in advance.

CodePudding user response:

The type AddOnType[][] is a multidimensional array and not a List<AddOnType>, so you must return a multidimensional array as follows:

 private AddOnTypeV17[][] GetAddons() {
     return new AddOnTypeV17[][]
     {
         new [] { AddOnTypeV17.SCAHP, AddOnTypeV17.SCAHP },
         new [] { AddOnTypeV17.SCAHP },
     };
 }

Hope it helps figuring out how to use it further.

Edit:

You could, potentially, use a list and then call ToArray on it to convert it to the proper type:

return new List<AddOnTypeV17[]> {
    new [] { AddOnTypeV17.SCAHP, AddOnTypeV17.SCAHP },
    new [] { AddOnTypeV17.SCAHP },
}.ToArray();

Using a list can make it easier if you are getting your data dynamically from somewhere else. The following code is equivalent to all the above (in semantics):

var list = new List<AddOnTypeV17[]>();
list.Add(new[] { AddOnTypeV17.SCAHP, AddOnTypeV17.SCAHP });
list.Add(new AddOnTypeV17[] { AddOnTypeV17.SCAHP, });
return list.ToArray();
  • Related