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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redefine Variables

Status
Not open for further replies.

LenardD

Programmer
Dec 1, 2002
35
CA
Is it possible to redefine the same variable as two different "types" (a.k.a. classes) within the same scope?

Sometimes I just want to use the same variable name over and over since it is just a "temp".

Thanks

--Lenard


 
You can simply declare the variable as an object, and then cast it between your different types. This has performance implications due to late binding, but I think it may be better than having alot of references lying around on the stack.

Seeing as these references will be pointing to the same number of places on the heap as this single object reference would, then having just the single object reference on the stack should be better? Not sure on that one.

Short answer: Use a variable of type, 'object', to achieve your goal. I'm not positive on the performance implications.

This is all assuming that these variables you speak of are reference types, of course. If they're primitive value types, then I'd suggest just declaring temp vars of each type.

:)
-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top