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

时间:2015-05-21 19:22:52

标签: c# arrays

我有一个类,我将从该类中的另一个类初始化一个属性a,类[]作为类型,如何初始化并用值{1,“something”}填充该数组,我无法得到它,谢谢。在最底层,我到目前为止尝试编码

/// Calss A

function c() {
    console.log("c");
}

function b() {
    console.log("b");
    return c;
}

function a() {
    console.log("a");
    return b;
}

for(var f = a; f instanceof Function; f = f());

/// B组

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");
            }
        }
}

到目前为止编码:

 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++;
            } 
}
}   

2 个答案:

答案 0 :(得分:2)

您正在创建数组,但由于数组是引用类型数组,因此每个引用也需要初始化。你也没有在循环中使用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();

答案 1 :(得分:0)

如果您尝试初始化一系列参考类型,则无法使用Array.Initialize,但您可以使用Linq。

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