Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Scope Question

Status
Not open for further replies.

Otacustes

IS-IT--Management
Apr 15, 2005
40
0
0
GB
I'm banging my head against the wall trying to work this one out.

I have the code below:
Code:
protected override void OnDragOver(DragEventArgs drgevent)
{
	try
	{
		if (this.GetItem(position) != null)
		{
			BaseVar baseVarElement = this.GetItem(position);
		} 
		else
		{
			BaseVar baseVarElement = this.GetGroup(position);
		}

		if (!object.ReferenceEquals(baseVarElement, this.PrevPosition))
		{
			
		}
		
	}
	catch (Exception exception)
	{
	
	}
}
and when I try and compile it, I get this error
Visual Studio said:
A local variable named 'baseVarElement' cannot be declared in this scope because it would give a different meaning to 'baseVarElement', which is already used in a 'parent or current' scope to denote something else

Now I thought it was something to do with the scope of the baseVarElement variable within the curly braces so I made it the a method scope by changing the code to:
Code:
protected override void OnDragOver(DragEventArgs drgevent)
{
	BaseVar baseVarElement;

	try
	{
		if (this.GetItem(position) != null)
		{
			BaseVar baseVarElement = this.GetItem(position);
		} 
		else
		{
			BaseVar baseVarElement = this.GetGroup(position);
		}

		if (!object.ReferenceEquals(baseVarElement, this.PrevPosition))
		{
			
		}
		
	}
	catch (Exception exception)
	{
	
	}
}
but I still get the same error.

Where am I going wrong?

All help is greatly appreciated
Kind regards
Ota
 
I don't use c#, just VB. However I would guess from the error message you are using (or at least have declared) the variable baseVarElement elsewhere in the Class such that that declaration would have class-wide scope. But as you are (re)declaring the variable as local in this Method, I don't see why it is not working - it would in VB. So I would check to see if and how you have declared it in the class. If it is a Property that might be the cause the error. I've never tried to redeclare a Property as a variable in VB so I don't know.
 
Code:
protected override void OnDragOver(DragEventArgs drgevent)
{
	BaseVar baseVarElement;

	try
	{
		if (this.GetItem(position) != null)
		{
	                baseVarElement = this.GetItem(position);
		} 
		else
		{
			baseVarElement = this.GetGroup(position);
		}

		if (!object.ReferenceEquals(baseVarElement, this.PrevPosition))
		{
			
		}
		
	}
	catch (Exception exception)
	{
	
	}
}

Borislav Borissov
VFP9 SP2, SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top