Gambarsky
Gambarsky

Reputation: 15

Use of unassigned bool? local variable

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?!

Upvotes: 0

Views: 3505

Answers (4)

Tomas Pastircak
Tomas Pastircak

Reputation: 2857

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.

Upvotes: 1

Crono
Crono

Reputation: 10478

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.

Upvotes: 2

Tim S.
Tim S.

Reputation: 56536

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)

Upvotes: 0

milagvoniduak
milagvoniduak

Reputation: 3254

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

 bool? pass = null;

Upvotes: 5

Related Questions