Click here to Skip to main content
14,640,669 members
Rate this:
Please Sign up or sign in to vote.
See more:
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?!
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

You should move the 2nd IF block inside the 1st one, because the local pass variable is initialized only inside the 1st IF.

So you should modify your code like this (I updated the code after you update your code in your provided solution):
bool? pass = null;
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;
                }
                // Must test for null before to use it!
                if (pass != null && pass.GetValueOrDefault() || row.view_only)
                //..
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

bool? pass = null; should fix this issue.
   
v2
Comments
Member 10742509 22-Apr-14 13:23pm
   
i`ve already tried this, however, this could not be null...
Rate this:
Please Sign up or sign in to vote.

Solution 3

Solved like this:

i`ve defined pass before foreach scope.

bool? pass = null; // ---THIS WAS ADDED
foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
        {
            PictureBox box;
            MemoryStream stream;
            Panel panel;
            Label label;
            // bool? pass;   ---THIS WAS REMOVED
            if (this.pass.HasValue && this.end)
            {
               pass = this.pass;
            }
            if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)
        }
   
Comments
Volynsky Alex 22-Apr-14 16:14pm
   
Nice answer
Raul Iloc 23-Apr-14 7:41am
   
Your answer to your own question is not OK, because you can get the same error again if the pass is not initialized for the first item (Row).
See my update in my solution (solution1)!
Member 10742509 23-Apr-14 7:50am
   
Thank you for your comment. I`ve tested my solution and it`s ok. error has gone and application works without any error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100