Use of unassigned local variable

  • Thread starter Thread starter Antonio
  • Start date Start date
A

Antonio

Can somebody tell me what's wrong with this code? When I try to debug, I
get "Use of unassigned local variable 'ip2se'.

string ip2se;
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text.ToString();

Thanks,


Antonio
 
Yes you only assign the ip2se variable if a condition is met .. I assume its
giving the error below this somewhere ..
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text.ToString();
i.e. error happens here ..

Try changing it to

string ip2se = null;

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Antonio said:
Can somebody tell me what's wrong with this code? When I try to debug, I
get "Use of unassigned local variable 'ip2se'.

string ip2se;
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text.ToString();

Thanks,


Antonio
 
Hi, Greg,

Thank you for your reply. I have been working on this, while waiting for a
response, and here is what I am fighting with:

I am trying to get the IP addresses as a range. They are entered as IPSeg1,
IPSeg2Start, IPSeg2End, IPSeg3Start, IPSeg3End, IPSeg4Start, and IPSeg4End.

Each of them store the data into a separate cell in the datagrid and here's
what I came up with, but it doesn't display the IP address' 2nd octate in
the Cell[8]

private void Item_Bound(Object sender, DataGridItemEventArgs e)
{
string ip2se = "";
string ip3se = "";
string ip4se = "";
{
{
//Group IP2Start and End if same value
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text + ".";
else if(e.Item.Cells[2].Text != e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text + "-" + e.Item.Cells[3].Text + ".";
}
{
//Group IP3Start and End if same value
if(e.Item.Cells[4].Text == e.Item.Cells[5].Text)
ip2se = e.Item.Cells[4].Text + ".";
else if(e.Item.Cells[4].Text != e.Item.Cells[5].Text)
ip2se = e.Item.Cells[4].Text + "-" + e.Item.Cells[5].Text + ".";
}
{
//Group IP4Start and End if same value
if(e.Item.Cells[6].Text == e.Item.Cells[7].Text)
ip3se = e.Item.Cells[6].Text;
else if(e.Item.Cells[6].Text != e.Item.Cells[7].Text)
ip3se = e.Item.Cells[6].Text + "-" + e.Item.Cells[7].Text;
}
{

//checks that data in the cell is not the value of the header or footer to
compare values

if(e.Item.ItemType !=ListItemType.Header &&
e.Item.ItemType!=ListItemType.Footer)

e.Item.Cells[8].Text = e.Item.Cells[1].Text + "." + ip2se + ip3se + ip4se;
}
}

Greg Young said:
Yes you only assign the ip2se variable if a condition is met .. I assume
its giving the error below this somewhere ..
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text.ToString();
i.e. error happens here ..

Try changing it to

string ip2se = null;

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Antonio said:
Can somebody tell me what's wrong with this code? When I try to debug, I
get "Use of unassigned local variable 'ip2se'.

string ip2se;
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text.ToString();

Thanks,


Antonio
 
Hi, Greg,
Thank you for your reply. I have been working on this, while waiting for a
response, and here is what I am fighting with:

I am trying to get the IP addresses as a range. They are entered as IPSeg1,
IPSeg2Start, IPSeg2End, IPSeg3Start, IPSeg3End, IPSeg4Start, and IPSeg4End.

Each of them store the data into a separate cell in the datagrid and here's
what I came up with, but it doesn't display the IP address' 2nd octate in the
Cell[8]

private void Item_Bound(Object sender, DataGridItemEventArgs e)
{
string ip2se = "";
string ip3se = "";
string ip4se = "";
{
{

why the '{' ? It's not needed here
//Group IP2Start and End if same value
if(e.Item.Cells[2].Text == e.Item.Cells[3].Text)
ip2se = e.Item.Cells[2].Text + ".";
else if(e.Item.Cells[2].Text != e.Item.Cells[3].Text)

Why the extra test? you already *know* it is not equal.
A plain "else" (without the extra "if") should be enough.
ip2se = e.Item.Cells[2].Text + "-" + e.Item.Cells[3].Text + ".";
}
{
//Group IP3Start and End if same value
if(e.Item.Cells[4].Text == e.Item.Cells[5].Text)
ip2se = e.Item.Cells[4].Text + ".";
else if(e.Item.Cells[4].Text != e.Item.Cells[5].Text)

(see above)
ip2se = e.Item.Cells[4].Text + "-" + e.Item.Cells[5].Text + ".";
}
{
//Group IP4Start and End if same value
if(e.Item.Cells[6].Text == e.Item.Cells[7].Text)
ip3se = e.Item.Cells[6].Text;
else if(e.Item.Cells[6].Text != e.Item.Cells[7].Text)
ip3se = e.Item.Cells[6].Text + "-" + e.Item.Cells[7].Text;
}
{

//checks that data in the cell is not the value of the header or footer to
compare values

if(e.Item.ItemType !=ListItemType.Header &&
e.Item.ItemType!=ListItemType.Footer)

e.Item.Cells[8].Text = e.Item.Cells[1].Text + "." + ip2se + ip3se + ip4se;
}
}

Did you mean that "ip2se" is empty? What are the (input) values of the
Cells[] and what are the resulting values of ip2se/ip3se/ip4se?

Hans Kesting
 
Back
Top