×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Variable Scope Question

Variable Scope Question

Variable Scope Question

(OP)
I'm banging my head against the wall trying to work this one out.

I have the code below:

CODE --> c#

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

Quote (Visual Studio)

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 --> c#

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

RE: Variable Scope Question

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.

RE: Variable Scope Question

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close