0

i have this:

   foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
        {
            PictureBox box;
            MemoryStream stream;
            Panel panel;
            Label label;
            bool? pass;
            if (this.pass.HasValue && this.end)
            {
               pass = this.pass;
            }
            if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)
        }

Variable "pass" near "GetValueOrDefault" underlined as an error: "Use of unassigned local variable pass". I don`t understand why this variable is unassigned because in the same line there is "pass" near "HasValue" and it is assigned. Where is my syntax error?!

Gambarsky
  • 15
  • 2
  • 8
  • 1
    Someone has to say it. Why do you want to make your life difficult? Having two variables with the same name seems really masochist. Change the name of the bool? variable inside the foreach loop to something different from the one outside. – Steve Apr 22 '14 at 17:15
  • i`m making some changes in very old app written by person who does not work in our company anymore. i cannot change whole code and for this moment and i stuck at this point. maybe you can suggest how to assign default value or how to modify this **IF** statement? any ideas? – Gambarsky Apr 22 '14 at 17:26

4 Answers4

5

You are trying to use it before it was actually assigned. Assign null to it.

 bool? pass = null;
milagvoniduak
  • 3,214
  • 1
  • 18
  • 18
2

Assigning your pass variable conditionally doesn't make it safely assigned for every possible scenarios. Use @MyP3uK solution and assign it to null at the same line you declare it.

That being said, you still are at risk of confounding pass and this.pass. I would definitely use another name for the local variable. Also, choosing bool? over bool seems like the wrong choice to me here.

Crono
  • 10,211
  • 6
  • 43
  • 75
1

You want to do either of these:

Make sure you assign to your local pass before using it: On each iteration the local pass will be restored to null.

   foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
        {
            PictureBox box;
            MemoryStream stream;
            Panel panel;
            Label label;
            bool? pass = null; //this line has changed
            if (this.pass.HasValue && this.end)
            {
               pass = this.pass;
            }
            if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)
        }

Use normal bool instead of a nullable one, on each iteration the local pass will be set to default value, i.e. false:

   foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
        {
            PictureBox box;
            MemoryStream stream;
            Panel panel;
            Label label;
            bool pass; //this line has changed
            if (this.pass.HasValue && this.end)
            {
               pass = this.pass;
            }
            if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)
        }

Define pass before the foreach scope if you need to use value from previous iteration in next iteration as well:

   bool? pass = null; //this line has changed
   foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
        {
            PictureBox box;
            MemoryStream stream;
            Panel panel;
            Label label;

            if (this.pass.HasValue && this.end)
            {
               pass = this.pass;
            }
            if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)
        }

I am afraid these are the only options you have, if pass contains the value you actually need.

Tomas Pastircak
  • 2,867
  • 16
  • 28
0

The local variable pass is not always assigned. It's only assigned in your code if the if block is entered. Near as I can tell, this is what you're trying to do:

bool pass = this.pass.HasValue && this.end;
if (pass && row.view_only)
Tim S.
  • 55,448
  • 7
  • 96
  • 122