silly Q regarding local var

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,566
Reaction score
3,437
Code:
private void DoSomething(int rowID, int selectedIndex)
{
    if (selectedIndex != rowID)
    {
	[COLOR="Red"]Button btn[/COLOR] = (Button)SomeTable.Rows[selectedIndex].Cells[0].Controls[0];
	
        //do something with button
    }

    [COLOR="Red"]Button btn[/COLOR] = (Button)SomeTable.Rows[rowID].Cells[0].Controls[0];
    
    //do something with button
}

Seems that IDE don't like the Button being declared in the IF and just under it with the same name, now i thought that if a var is declared inside the IF it would be seperate instance of the button itself from declartion outside the IF. :confused:

why ?
 
The scope of the variable is everything inside the { }, not from beneath where you declare it. So your second btn scope is the method.
 
if you're re-using the btn variable, why not just declare it once at the top of the method
 
The scope of the variable is everything inside the { }, not from beneath where you declare it. So your second btn scope is the method.
i understand that just wondering as the first decleration is inside the IF's {}.

if you're re-using the btn variable, why not just declare it once at the top of the method
what i did, just curious as to why per OP.
 
To understand why you can't do this consider you code to look like this:

Code:
private void DoSomething(int rowID, int selectedIndex)
{
   [b] Button btn = (Button)SomeTable.Rows[rowID].Cells[0].Controls[0];[/b]

    if (selectedIndex != rowID)
    {
	Button btn = (Button)SomeTable.Rows[selectedIndex].Cells[0].Controls[0];
	
        //do something with button
    }
    
    //do something with button
}

I don't think the c# compiler knows the difference between the two so it disallows it. Here you nested btn is declared with the same as the method level btn with the same name - not allowed. (Makes sense?)

Rather do as was said before:
Code:
private void DoSomething(int rowID, int selectedIndex)
{
   Button btn;

    if (selectedIndex != rowID)
    {
	btn = (Button)SomeTable.Rows[selectedIndex].Cells[0].Controls[0];
	
        //do something with button
    }
    btn = (Button)SomeTable.Rows[rowID].Cells[0].Controls[0];
    //do something with button
}
 
To understand why you can't do this consider you code to look like this:

Code:
private void DoSomething(int rowID, int selectedIndex)
{
   [b] Button btn = (Button)SomeTable.Rows[rowID].Cells[0].Controls[0];[/b]

    if (selectedIndex != rowID)
    {
	Button btn = (Button)SomeTable.Rows[selectedIndex].Cells[0].Controls[0];
	
        //do something with button
    }
    
    //do something with button
}

I don't think the c# compiler knows the difference between the two so it disallows it. Here you nested btn is declared with the same as the method level btn with the same name - not allowed. (Makes sense?)

Rather do as was said before:
Code:
private void DoSomething(int rowID, int selectedIndex)
{
   Button btn;

    if (selectedIndex != rowID)
    {
	btn = (Button)SomeTable.Rows[selectedIndex].Cells[0].Controls[0];
	
        //do something with button
    }
    btn = (Button)SomeTable.Rows[rowID].Cells[0].Controls[0];
    //do something with button
}

makes sense. As i said- i did do it as suggested was just curious as to why the compiler would do that....
 
i understand that just wondering as the first decleration is inside the IF's {}..

I think you misunderstood, because it's in the entire scope it's the equivalent of this:

{
Button btn;

if() {
Button btn
}
}

So you are redeclaring btn. In your original example just because you declared btn after the if doesn't mean that's where it comes into existence. The variable belongs to the entire block { } in C#

This type of declaration works in C/C++ though. My guess is that the C# designers disallowed this because it's easy to introduce hard to find bugs.

EDIT: Consider this in C

#include <stdio.h>

int main() {
int x = 1;
printf("%d\n", x);

if(x==1) {
int x;
x = 3;
printf("%d\n", x);
}

if(x==1) {
printf("%d\n", x);
}
}

It should print out 1,3,1. Another coder comes along months later and decides to optimize by removing the redeclaration of x. Now it only print 1,3. The original coder never expected the original x value to change. Obviously this is a trivial example, but it can really difficult of find this type of bug.
 
Last edited:
UPFRONT DISCLAIMER: The following post does not answer the OP in any way, but is just a general comment on the method used to access the Button control.

Why not use
Code:
SomeTable.Rows[selectedIndex].FindControl("ServerSideControlID")

instead of

Code:
SomeTable.Rows[selectedIndex].Cells[0].Controls[0]

Works better for me and makes a lot more sense. ;)
 
It should print out 1,3,1. Another coder comes along months later and decides to optimize by removing the redeclaration of x. Now it only print 1,3. The original coder never expected the original x value to change. Obviously this is a trivial example, but it can really difficult of find this type of bug.
i see.

UPFRONT DISCLAIMER: The following post does not answer the OP in any way, but is just a general comment on the method used to access the Button control.

Why not use
Code:
SomeTable.Rows[selectedIndex].FindControl("ServerSideControlID")

instead of

Code:
SomeTable.Rows[selectedIndex].Cells[0].Controls[0]

Works better for me and makes a lot more sense. ;)

rofl. i was wondering when someone is going to comment on the trivial parts of the OP, but thank you and yes the id method is obvioulsy better than asuming its always there at pos 0 :)
 
Top
Sign up to the MyBroadband newsletter
X