W
web1110
In the following code I get an error. I delcare and use "MenuItem
mniBackItem" within the loop. After exiting the loop, I use it again. But
I get the following error on the declaration after the loop:
S:\Acorn\Applications\APPRunner\FRMRun\FRMRun.cs(231): A local variable
named 'mniBackItem' cannot be declared in this scope because it would give a
different meaning to 'mniBackItem', which is already used in a 'child' scope
to denote something else
I thought that after I left the loop, I could reuse then name like in C.
Changing the name works. So can I assume that in C#, a variable name
declared within a block (loop) cannot be used below that block?
===================================================
private void mniBack_Select(object sender, System.EventArgs e)
{
int siControlCnt=clControlManager.siControlDepth;
string[] astrControlNames = new String[siControlCnt];
clControlManager.siGetControlNames(ref astrControlNames);
mniBack.MenuItems.Clear();
for(int si=0; si<siControlCnt;si++)
{
if(astrControlNames[si]==String.Empty)
{
break;
}
MenuItem mniBackItem = new MenuItem();
mniBackItem.Click += new System.EventHandler(this.vdBackClick);
mniBackItem.Text = astrControlNames[si];
mniBack.MenuItems.Add(mniBackItem);
}
MenuItem mniBackItem = new MenuItem();
<-------------------------------- ERRORS BEGIN HERE
mniBackItem.Click += new System.EventHandler(this.vdBackClick);
mniBackItem.Text = astrControlNames[si];
mniBack.MenuItems.Add(mniBackItem);
}
mniBackItem" within the loop. After exiting the loop, I use it again. But
I get the following error on the declaration after the loop:
S:\Acorn\Applications\APPRunner\FRMRun\FRMRun.cs(231): A local variable
named 'mniBackItem' cannot be declared in this scope because it would give a
different meaning to 'mniBackItem', which is already used in a 'child' scope
to denote something else
I thought that after I left the loop, I could reuse then name like in C.
Changing the name works. So can I assume that in C#, a variable name
declared within a block (loop) cannot be used below that block?
===================================================
private void mniBack_Select(object sender, System.EventArgs e)
{
int siControlCnt=clControlManager.siControlDepth;
string[] astrControlNames = new String[siControlCnt];
clControlManager.siGetControlNames(ref astrControlNames);
mniBack.MenuItems.Clear();
for(int si=0; si<siControlCnt;si++)
{
if(astrControlNames[si]==String.Empty)
{
break;
}
MenuItem mniBackItem = new MenuItem();
mniBackItem.Click += new System.EventHandler(this.vdBackClick);
mniBackItem.Text = astrControlNames[si];
mniBack.MenuItems.Add(mniBackItem);
}
MenuItem mniBackItem = new MenuItem();
<-------------------------------- ERRORS BEGIN HERE
mniBackItem.Click += new System.EventHandler(this.vdBackClick);
mniBackItem.Text = astrControlNames[si];
mniBack.MenuItems.Add(mniBackItem);
}