无法初始化数组或用数据填充数组

本文关键字:数组 数据 填充 初始化 | 更新日期: 2023-09-27 17:50:16

我有一个类,我将从该类内的另一个类初始化,有一个属性a类[]作为类型,我如何初始化和填充数组值{1,"某物"},我无法得到它,谢谢。到目前为止,我尝试的最下面的代码是

放学

///

public partial class classA_1: object,System.ComponentModel.INotifyPropertyChanged {
private classB_1[] numberOfUnitField;

/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order=0)]
[System.Xml.Serialization.XmlArrayItemAttribute("unitNumberDetail", IsNullable=false)]
public classB_1[] numberOfUnit {
            get {
                return this.numberOfUnitField;
            }
            set {
                this.numberOfUnitField = value;
                this.RaisePropertyChanged("numberOfUnit");
            }
        }
}

///B类

 public partial class classB_1 : object, System.ComponentModel.INotifyPropertyChanged {
        private string numberOfUnitsField;
        private string typeOfUnitField;
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=0)]
        public string numberOfUnits {
            get {
                return this.numberOfUnitsField;
            }
            set {
                this.numberOfUnitsField = value;
                this.RaisePropertyChanged("numberOfUnits");
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=1)]
        public string typeOfUnit {
            get {
                return this.typeOfUnitField;
            }
            set {
                this.typeOfUnitField = value;
                this.RaisePropertyChanged("typeOfUnit");
            }
        }
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

目前编码:

 class Program
    {
        static void Main(string[] args)
        {
ClassA_1 a = new ClassA_1 ();

            Hashtable hash = new Hashtable();
            hash.Add("1", "PX");
            hash.Add("200", "RC");
            int i = 0;      
            int d = hash.Keys.Count;
            b.numberOfUnit = new classB_1 [d];

            foreach (DictionaryEntry entry in hash)
            { 
               //throws an error object not instantiated on the following code              
                b.numberOfUnit[i].numberOfUnits = entry.Key.ToString();
                b.numberOfUnit[i].typeOfUnit = entry.Value.ToString();
                i++;
            } 
}
}   

最终工作代码:

Dictionary<int, string> hash = new Dictionary<int, string>();
 hash.Add(1, "PX");
   hash.Add(200, "RC");
 b.numberOfUnit = hash.Select(h => new ClassB_1
                                                    {
                                                        numberOfUnits = h.Key.ToString(),
                                                        typeOfUnit = h.Value.ToString()
                                                    })
                                                             .ToArray();  

无法初始化数组或用数据填充数组

您正在创建数组,但由于该数组是引用类型的数组,因此还需要初始化每个引用。你也没有在循环中使用i -我认为你想要:

b.numberOfUnit = new classB_1 [d];
foreach (DictionaryEntry entry in hash)
{ 
   //throws an error object not instantiated on the following code              
    b.numberOfUnit[i] = new classB_1();
    b.numberOfUnit[i].numberOfUnits = entry.Key.ToString();
    b.numberOfUnit[i].typeOfUnit = entry.Value.ToString();
    i++;
} 

请注意,您也可以使用Linq来创建数组:

b.numberOfUnit = hash.Select(h => new classB_1 {
                                                numberOfUnits = h.Key.ToString(),
                                                typeOfUnit = h.Value.ToString()
                                               })
                     .ToArray();

如果您试图初始化引用类型的数组,则不能使用array。初始化,但你可以使用Linq.

private MyClass[] _myArray = Enumerable.Range(1, 10).Select(i => new MyClass()).ToArray();