Hi,
I am new to .NET 3.5, found a situatation where I need to to do object initialization only if certain condition fullfills this is easy if we do using previous way but when i try to do this is .NET 3.5 new feature of Object Initializer way I get syntatic errors, so can you please help me the right way how can i achieve this. Please see my examples below to understand.
Earlier way .NET 2.0 (I want to initialize object only when string is not empty)
Class B { public string bValue; }
Class A
{
private B bclassVar; // B is some another class
public B BClassVar {get;set;}
void main()
{
A obj = new A();
if(s != string.Empty) // s is some string
{
A.BClassVar = new B();
A.BClassVar.bValue = "some value";
}
}
}
Now How can I achieve above with new Object Initializers as when I try to do same as above i get syntaic errors:
New way .NET 3.5 ?
void main()
{
A obj = new A
{
if(s != string.Empty) // Sytatic error her
{
A.BClassVar = new B
{
bValue = “some value”
},
}
};
}
Sow how to do this ???
Thanks