Reputation: 3057
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();
Upvotes: 0
Views: 106
Reputation: 152521
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();
Upvotes: 2
Reputation: 52240
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();
Upvotes: 0