this is probably quite a simple problem, however, I can’t figure out how to get around this. Basically, my class Client requires a work address value, however, it’s not 100% required when creating a client. In creating the variable to be used for the object creation i’m using a string value which will then get the value from a text box. I am then checking to see if the textbox is empty, if it is, the string value of the variable is just a whitespace. Else, it’s the textbox value.
string homeAdd;
if(homeAddressText.Text == String.Empty)
{
homeAdd = " ";
}
else if (homeAddressText.Text != String.Empty)
{
homeAdd = homeAddressText.Text;
}
As seen here..
However, when I go to create the client object using this variable, I get an error saying ‘use of unassigned local variable ‘homeAdd’.
Client client = new Client(firstN, lastN, homeAdd, workAdd, email, homeP, cellP);
Is there a simple way I can do the checking I require and still use the variable?
Thanks in advance guys/gals.
Change the whole
else ifline to just:The two conditions are exact opposites. Your code would work (but is verbose) in theory, provided the textbox is only touched on the UI thread. However, the compiler can not confirm this.