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!

Double indirection in VB? Equivalent to C++ pointers?

Status
Not open for further replies.

nath

Programmer
Dec 5, 2001
109
ES
How can I access the content of a variable/constant X when I use a variable Y which contains the name of the first variable, i.e. X?
Similarly how can I run a function/procedure if I call it using a variable containing its name?
I know there are many alternative ways to do that, but I'd like to know if someone is aware of how to do exactly
what I need (No public variables, no arrays).

I want to do something like the following:

Code:
'*******
Public const MYCONST1 = "Portfolio1.txt"
Public const MYCONST2 = "Portfolio2.txt"

Sub Test()
	Dim i as Integer

	For i = 1 to 2
		Open "MYCONST" & i For Input as #1	
		'I want it to open Portfolio1.txt and Portfolio2.txt but it will try to open 
		'MYCONST1 and MYCONST2 instead, which are not existing files!
	Next i
End Sub
'*******

A similar problem is:

Sub Test2()
	...
	sType = "Rectangle"	
	...
	Call "Print" & sType 	'I want it to call the procedure PrintRectangle
End Sub

Sub PrintRectangle()
	...
End Sub

'********************

Thank you for your help.

Nath.
 
For the first problem, use an array. Something like this

dim arrFiles(1) as string

arrFiles(0)="Portfolio0.txt"
arrFiles(1)="Portfolio1.txt"

For i=0 to ubound(arrFiles)
Open arrFiles(i) For Input as #1
Close #1
Next

I don't think you can start procedures using a variable, but it doesn't seem very usefull either.

Hope it helps a bit,

Heico
 
Sounds like what you're looking for is a macro substitution thing like they have in FoxPro. Sorry, but this simply doesn't exist in VB. As far as I know, there's no way to call internal functions by a string containing the name, and the closest thing to getting values from variable names is collections, which probably isn't what you're really looking for.
 
Yes it is indeed the idea of macro substitution.
It was also my impression it doesn't exist in VB but I couldn't believe it... There many ways round the problem, but never very neat. Never mind. Thanks for your input/confirmation.
 
[tt]
const cFiles = _
&quot;&MYCONST1.txt<REC>&MYCONST2.txt<REC>&MYCONST3.dat&quot;

Dim fList() as String, fName as String
Dim I as Integer

fList = Split( _
Replace(cFiles, &quot;&MYCONST&quot;, &quot;Portfolio&quot;), _
&quot;<REC>&quot; _
)

Open fList(I)...
Open format(I, &quot;\P\o\r\t\f\o\l\i\o0\.\t\x\t&quot;)...
[/tt]

:)

Wil Mead
wmead@optonline.net

 
There are some quite neat methods of solving this with the Scripting Host control, and its eval and executestatement methods
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top