Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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

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):
C#
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)
                //..
 
Share this answer
 
v3
bool? pass = null; should fix this issue.
 
Share this answer
 
v2
Comments
Member 10742509 22-Apr-14 13:23pm    
i`ve already tried this, however, this could not be null...
Solved like this:

i`ve defined pass before foreach scope.

C#
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)
        }
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900