plz help!!error use of unassigned local variable
-
hi i just want to display all empty fieldnames that are not filled up by the user but my code has this errorer ' use of unassigned local variable 'messEmptyField' ; how can i fix this
private void btnSave_Click(object sender, EventArgs e) { string[] FieldName = { "Lastname", "Firstname", "Address", "Model", "Trouble Reported" }; string messEmptyField; int i = 0; foreach (Control ctrl in groupBox1.Controls) { TextBox txt = ctrl as TextBox; if (txt.Text == string.Empty) { messEmptyField += FieldName[i]; //error use of unassigned local variable 'messEmptyField' } i++; } MessageBox.Show(messEmptyField + "please filled up all"); dtDateLog.Focus(); return; }
-
hi i just want to display all empty fieldnames that are not filled up by the user but my code has this errorer ' use of unassigned local variable 'messEmptyField' ; how can i fix this
private void btnSave_Click(object sender, EventArgs e) { string[] FieldName = { "Lastname", "Firstname", "Address", "Model", "Trouble Reported" }; string messEmptyField; int i = 0; foreach (Control ctrl in groupBox1.Controls) { TextBox txt = ctrl as TextBox; if (txt.Text == string.Empty) { messEmptyField += FieldName[i]; //error use of unassigned local variable 'messEmptyField' } i++; } MessageBox.Show(messEmptyField + "please filled up all"); dtDateLog.Focus(); return; }
-
hi i just want to display all empty fieldnames that are not filled up by the user but my code has this errorer ' use of unassigned local variable 'messEmptyField' ; how can i fix this
private void btnSave_Click(object sender, EventArgs e) { string[] FieldName = { "Lastname", "Firstname", "Address", "Model", "Trouble Reported" }; string messEmptyField; int i = 0; foreach (Control ctrl in groupBox1.Controls) { TextBox txt = ctrl as TextBox; if (txt.Text == string.Empty) { messEmptyField += FieldName[i]; //error use of unassigned local variable 'messEmptyField' } i++; } MessageBox.Show(messEmptyField + "please filled up all"); dtDateLog.Focus(); return; }
-
uraghu wrote:
string messEmptyField="";
Using
string.Empty
is the good practice.Arun Jacob http://codepronet.blogspot.com/
-
hi i just want to display all empty fieldnames that are not filled up by the user but my code has this errorer ' use of unassigned local variable 'messEmptyField' ; how can i fix this
private void btnSave_Click(object sender, EventArgs e) { string[] FieldName = { "Lastname", "Firstname", "Address", "Model", "Trouble Reported" }; string messEmptyField; int i = 0; foreach (Control ctrl in groupBox1.Controls) { TextBox txt = ctrl as TextBox; if (txt.Text == string.Empty) { messEmptyField += FieldName[i]; //error use of unassigned local variable 'messEmptyField' } i++; } MessageBox.Show(messEmptyField + "please filled up all"); dtDateLog.Focus(); return; }
You can try the following:
string messEmptyField = ""; //declares an empty string
--the choice is all yours!-- :-\
-
hi i just want to display all empty fieldnames that are not filled up by the user but my code has this errorer ' use of unassigned local variable 'messEmptyField' ; how can i fix this
private void btnSave_Click(object sender, EventArgs e) { string[] FieldName = { "Lastname", "Firstname", "Address", "Model", "Trouble Reported" }; string messEmptyField; int i = 0; foreach (Control ctrl in groupBox1.Controls) { TextBox txt = ctrl as TextBox; if (txt.Text == string.Empty) { messEmptyField += FieldName[i]; //error use of unassigned local variable 'messEmptyField' } i++; } MessageBox.Show(messEmptyField + "please filled up all"); dtDateLog.Focus(); return; }
You can use String.Empty as the others say, or better still use StringBuilder:
private void btnSave_Click(object sender, EventArgs e)
{
string[] FieldName = { "Lastname", "Firstname", "Address", "Model", "Trouble Reported" };
StringBuilder messEmptyField = new StringBuilder();
int i = 0;foreach (Control ctrl in groupBox1.Controls)
{
TextBox txt = ctrl as TextBox;if (txt.Text == string.Empty) { messEmptyField.Append(FieldName\[i\]); } i++; }
MessageBox.Show(messEmptyField.ToString() + "please filled up all");
dtDateLog.Focus();
return;
}You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy