0

I have a class that I will initialize from another class within that class there a property a with class[] as type, how do i initialize and fill that array with value {1,"something"}, I am unable to get to it, Thanks. At the very bottom what I tried so far is coded

///Calss A

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

///Class 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));
            }
        }
    }

Coded so far:

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

Final working code:

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();  
4
  • i is not referenced in the foreach other than increment, and you have 0 and 1 indexes hard coded. Don't think that is what you want. Commented May 21, 2015 at 19:33
  • changed 0 and 1 to i Commented May 21, 2015 at 19:35
  • why is the array initialized outside the class it lives in? Commented May 21, 2015 at 19:49
  • its a service provider file pattern I am just using those to input my data for various queries I can't change anything in those files, but I guess oop Commented May 21, 2015 at 20:04

2 Answers 2

2

You're creating the array, but since the array is an array of reference types, each reference needs to be initialized as well. Also you are not using i in the loop - I think you want:

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

Note that you could also use Linq to create the array:

b.numberOfUnit = hash.Select(h => new classB_1 {
                                                numberOfUnits = h.Key.ToString(),
                                                typeOfUnit = h.Value.ToString()
                                               })
                     .ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks changed from storage type from hashtable to dictionary and both of the codes work.
0

If you're attempting to initialize a array of reference types, you can't use Array.Initialize, but you can use Linq.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.