lidds Posted September 26, 2005 Posted September 26, 2005 Is there a way that I can use the same variable more than once in a private sub?? Is there a delete variable command so this can be done??? e.g. dim simon as string = "hello" msgbox(simon) simon.delete() dim simon as string = "bye" msgbox(simon) Thanks Simon Quote
*Experts* DiverDan Posted September 26, 2005 *Experts* Posted September 26, 2005 You can re-use variables as often as you wish by just re-setting its value. Dim simon As String = "hello" MessageBox.Show(simon) simon = "bye" MessageBox.Show(simon) simon = "aloha" MessageBox.Show(simon) Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
lidds Posted September 26, 2005 Author Posted September 26, 2005 Sorry should have explained myself a bit clearer. I am actually using a bit of third party software and I can't seem to get it to work unless I keep on declaring a new class to a variable, therefore I am unable to performing a do loop (to tidy my code up instead of loads of repeated code) as the variable name is already declared. Which is why I was hoping there is a way to delete a variable and then re-use it again??? Thanks in advance Simon Quote
Administrators PlausiblyDamp Posted September 26, 2005 Administrators Posted September 26, 2005 Any chance you could post a sample of what you are trying to do and what it does / you expect it to do? Once a variable is defined it cannot be defined again within the same scope, although it can be reused as DiverDan showed above. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
SonicBoomAu Posted September 27, 2005 Posted September 27, 2005 This might be from left field, but here goes! Instead of deleting couldn't you use REDIM? dim simon as string = "hello" msgbox(simon) redim simon as integar = "15" msgbox(simon) Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
Administrators PlausiblyDamp Posted September 28, 2005 Administrators Posted September 28, 2005 You can't redim variables like that, Redim only allows arrays to be resized. Even if you could I fail to see what it would acheive other than making the code lest readable. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted September 28, 2005 Posted September 28, 2005 I was wondering how you got one hundred plus posts and didn't know something like what DiverDan posted. I understand now what you meant, an example of code would be extreamly useful to help you with us answer your question. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
*Experts* Nerseus Posted September 29, 2005 *Experts* Posted September 29, 2005 @lidds: If your variable is an object that is declared as "new", you can simply create another object, as in: Dim o As ThirdPartyObject Dim i As Integer For i = 0 To 10 o = New ThirdPartyObject() ' Use new instance of o here Next or in C# ThirdPartyObject o; for(int i = 0; i<=10; i++) { o = new ThirdPartyObject(); // Use new instance of o here } -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.