Reputation: 146
i have this code
private static Func<fLogin,BusinessObject.User> Make = f =>
new BusinessObject.Usuario {
Name = f.txtU.Text,
Pass = f.txtPass.Text,
};
So. the User class is this.
public class User
{
public string Name {get; set; }
public string Pass {get; set; }
public Company Com {get; set; }
}
and the user has a Company here's company
public class Company
{
public string CompanyName {get; set;}
}
so, here is the issue, when i want to access company name, gives me error
private static Func<fLogin,BusinessObject.User> Make = f =>
new BusinessObject.Usuario {
Name = f.txtU.Text,
Pass = f.txtPass.Text,
Com.CompanyName = f.txtC.text
};
Com.CompanyName = f.txtC.text
this is not possible?
Upvotes: 1
Views: 194
Reputation: 7747
You should change Com.CompanyName = f.txtC.text
to Com = new Company { CompanyName = f.txtC.text }
. So field should looks like:
private static Func<fLogin,BusinessObject.User> Make = f =>
new BusinessObject.User
{
Name = f.txtU.Text,
Pass = f.txtPass.Text,
Com = new Company { CompanyName = f.txtC.text },
};
In your code you try to set CompanyName property of Com
object which is not initialized yet.
Upvotes: 2
Reputation: 3269
You cannot assign to the property of a property in the property initializer. Also, it doesn't make sense to assign to Com.CompanyName
without actually creating the Company
object.
private static Func<fLogin,BusinessObject.User> Make = f =>
new BusinessObject.Usuario {
Name = f.txtU.Text,
Pass = f.txtPass.Text,
Com = new Company { CompanyName = f.txtC.text }
};
Upvotes: 1
Reputation: 14458
The reason that you're getting an error on
Com.CompanyName = f.txtC.text
is that you have to set the Com
property in the object initializer, not set the CompanyName
property on the Com
property. The fix is to use a nested object initializer to set Com
to a new Company
with the correct CompanyName
property:
private static Func<fLogin,BusinessObject.User> Make = f =>
new BusinessObject.User
{
Name = f.txtU.Text,
Pass = f.txtPass.Text,
Com = new Company { CompanyName = f.txtC.text },
};
Upvotes: 5