strange problem with if statement

P

pei_world

can anyone help me with these problem, code written correctly except those
with * mark.
//==========================================================================
===============================
//recursive function defined for print nodes
private void nodePrint(TreeNode rootNode,string parentidstr,SqlDataAdapter
sqlDANode,DataSet tmpDS)
{
bool printed=false;
try
{
tmpDS.Clear();
sqlDANode.SelectCommand.CommandText="SELECT parentid,childid FROM
relation WHERE parentid="+parentidstr;
sqlDANode.Fill(tmpDS,"relation");

//MessageBox.Show(tmpDS.Tables.Count.ToString(),"try",MessageBoxButtons.OK,M
essageBoxIcon.Asterisk);

if(tmpDS.Tables["relation"].Rows.Count>0)
{
string[] tmpArray = new string[tmpDS.Tables["relation"].Rows.Count];
int i=0;

foreach(DataRow nodeRow in tmpDS.Tables["relation"].Rows)
{
if(i<tmpDS.Tables["relation"].Rows.Count)
{
tmpArray = nodeRow["childid"].ToString();
i=i+1;
}
}
if(printed==false)
{
TreeNode newNode =
rootNode.Nodes.Add(tmpDS.Tables["relation"].Rows[0]["parentid"].ToString());
printed=true;
}
else
{
TreeNode newNode = rootNode;
}
for(int k=0; k<i; k++)
{
parentidstr = tmpArray[k];
nodePrint(newNode,parentidstr,sqlDANode,tmpDS); //************* Error
if(printed!=true)
{
newNode.Nodes.Add(tmpArray[k]);//2 //*********** Error
}
}
}
else
{
return;
}
}
catch(Exception E)
{

MessageBox.Show(E.ToString(),"try",MessageBoxButtons.OK,MessageBoxIcon.Aster
isk);
}
}

//==========================================================================
=====================
Error output in VS.net:
1. The name 'newNode' does not exist in the class or namespace
2. The type or namespace name 'newNode' could not be found (are you missing
a using directive or an assembly reference?)

but I did create newNode object a few lines before.
 
C

Chris Torgerson

Your problem is that newNode is no longer in scope. Since you declared it
in the if block above it is only available in that if statement. You will
need to declare the variable elsewhere in your function so that it is
available at the point you are experiencing errors.

HTH
Chris Torgerson
pei_world said:
can anyone help me with these problem, code written correctly except those
with * mark.
//==========================================================================
===============================
//recursive function defined for print nodes
private void nodePrint(TreeNode rootNode,string parentidstr,SqlDataAdapter
sqlDANode,DataSet tmpDS)
{
bool printed=false;
try
{
tmpDS.Clear();
sqlDANode.SelectCommand.CommandText="SELECT parentid,childid FROM
relation WHERE parentid="+parentidstr;
sqlDANode.Fill(tmpDS,"relation");

//MessageBox.Show(tmpDS.Tables.Count.ToString(),"try",MessageBoxButtons.OK,M
essageBoxIcon.Asterisk);

if(tmpDS.Tables["relation"].Rows.Count>0)
{
string[] tmpArray = new string[tmpDS.Tables["relation"].Rows.Count];
int i=0;

foreach(DataRow nodeRow in tmpDS.Tables["relation"].Rows)
{
if(i<tmpDS.Tables["relation"].Rows.Count)
{
tmpArray = nodeRow["childid"].ToString();
i=i+1;
}
}
if(printed==false)
{
TreeNode newNode =
rootNode.Nodes.Add(tmpDS.Tables["relation"].Rows[0]["parentid"].ToString());
printed=true;
}
else
{
TreeNode newNode = rootNode;
}
for(int k=0; k<i; k++)
{
parentidstr = tmpArray[k];
nodePrint(newNode,parentidstr,sqlDANode,tmpDS); //************* Error
if(printed!=true)
{
newNode.Nodes.Add(tmpArray[k]);//2 //*********** Error
}
}
}
else
{
return;
}
}
catch(Exception E)
{

MessageBox.Show(E.ToString(),"try",MessageBoxButtons.OK,MessageBoxIcon.Aster
isk);
}
}
//==========================================================================
=====================
Error output in VS.net:
1. The name 'newNode' does not exist in the class or namespace
2. The type or namespace name 'newNode' could not be found (are you missing
a using directive or an assembly reference?)

but I did create newNode object a few lines before.
 
C

Casey MacPherson \(msft\)

Error 1: This is because newNode is being declared inside of an if
statement. So this can create a condition where if the statement doesn't
prove to be true when evaulated you are referencing a object that was never
created.

Error 2: Same as above.

Also just a guidance on your if statments. It appears you're doing if's on
bools. you can make it a little smoother looking.

For Example take if(printed==false) , you could make it if(!printed) or if
you wanted to see if the bool was true you can do if(printed). The way you
have it will work, but you can shorten it up :)


pei_world said:
can anyone help me with these problem, code written correctly except those
with * mark.
//==========================================================================
===============================
//recursive function defined for print nodes
private void nodePrint(TreeNode rootNode,string parentidstr,SqlDataAdapter
sqlDANode,DataSet tmpDS)
{
bool printed=false;
try
{
tmpDS.Clear();
sqlDANode.SelectCommand.CommandText="SELECT parentid,childid FROM
relation WHERE parentid="+parentidstr;
sqlDANode.Fill(tmpDS,"relation");

//MessageBox.Show(tmpDS.Tables.Count.ToString(),"try",MessageBoxButtons.OK,M
essageBoxIcon.Asterisk);

if(tmpDS.Tables["relation"].Rows.Count>0)
{
string[] tmpArray = new string[tmpDS.Tables["relation"].Rows.Count];
int i=0;

foreach(DataRow nodeRow in tmpDS.Tables["relation"].Rows)
{
if(i<tmpDS.Tables["relation"].Rows.Count)
{
tmpArray = nodeRow["childid"].ToString();
i=i+1;
}
}
if(printed==false)
{
TreeNode newNode =
rootNode.Nodes.Add(tmpDS.Tables["relation"].Rows[0]["parentid"].ToString());
printed=true;
}
else
{
TreeNode newNode = rootNode;
}
for(int k=0; k<i; k++)
{
parentidstr = tmpArray[k];
nodePrint(newNode,parentidstr,sqlDANode,tmpDS); //************* Error
if(printed!=true)
{
newNode.Nodes.Add(tmpArray[k]);//2 //*********** Error
}
}
}
else
{
return;
}
}
catch(Exception E)
{

MessageBox.Show(E.ToString(),"try",MessageBoxButtons.OK,MessageBoxIcon.Aster
isk);
}
}
//==========================================================================
=====================
Error output in VS.net:
1. The name 'newNode' does not exist in the class or namespace
2. The type or namespace name 'newNode' could not be found (are you missing
a using directive or an assembly reference?)

but I did create newNode object a few lines before.
 
Top