Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. plz help!!error use of unassigned local variable

plz help!!error use of unassigned local variable

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crisjala
    wrote on last edited by
    #1

    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; }

    M U M OriginalGriffO 4 Replies Last reply
    0
    • C crisjala

      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; }

      M Offline
      M Offline
      Matt U
      wrote on last edited by
      #2

      This line: string messEmptyField; should probably be: string messEmptyField = string.Empty;

      1 Reply Last reply
      0
      • C crisjala

        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; }

        U Offline
        U Offline
        uraghu
        wrote on last edited by
        #3

        hi try this string messEmptyField="";

        A 1 Reply Last reply
        0
        • U uraghu

          hi try this string messEmptyField="";

          A Offline
          A Offline
          Arun Jacob
          wrote on last edited by
          #4

          uraghu wrote:

          string messEmptyField="";

          Using string.Empty is the good practice.

          Arun Jacob http://codepronet.blogspot.com/

          1 Reply Last reply
          0
          • C crisjala

            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; }

            M Offline
            M Offline
            Morgs Morgan
            wrote on last edited by
            #5

            You can try the following:

            string messEmptyField = ""; //declares an empty string

            --the choice is all yours!-- :-\

            1 Reply Last reply
            0
            • C crisjala

              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; }

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              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

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups