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();
iis not referenced in theforeachother than increment, and you have0and1indexes hard coded. Don't think that is what you want.