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

Returning a string corresponding to the name of a variable

Status
Not open for further replies.

electricpete

Technical User
Oct 1, 2002
289
US
It's probably not possible, but I'll ask anyway.

Let's say I pass an argument of type double byRef to a function. The function knows the address of that variable. Can the function also figure out the name of that variable in the calling environment and return it to me as a string?
 
Either I'm being thick or you're being cryptic.
The function knows the address of that variable.
Does it? What exactly does it know? What exactly is it you pass and what exactly do you wish to get returned?
Please detail; best: paste some sample code together with the desired output.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
>Does it?

Yes, it does

>What exactly does it know?

It knows exactly where in memory the variable's data is held. That is exactly what ByRef means - a copy of the value of the variable is not passed, the address is.
 
Why don't you just pass the name of the variable in anouther variable.

Simi
 
electricpete, what we really need to undederstand is what it is that you are trying to achieve, as there may be alternatives to what you are trying.
 
@strongm: I know. It's just that it is not really clear as to what the OP wishes to achieve and what he needs the variable name for, as there might be a confusion on his behalf about how to best approach this, just as you have mentioned in your last post.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
strongm - thanks, you confirmed my suspicions.

The existing code works fine but requires passing a variable and it's name:
Code:
Sub ma_print(a() As Double, aname As String)
' Accepts a 2x2 variable for printing to the immediate window
Dim row As Long, col As Long
Dim outrow As String
    Debug.Print (aname)
    For row = 1 To UBound(a, 1)
        outrow = ""
        For col = 1 To UBound(a, 2)
            outrow = outrow + CStr(a(row, col)) + ","
        Next col
        Debug.Print outrow
    Next row
End Sub
If possible, I would have liked to simplify the routine so it takes only one argument (a) and figures out aname. Not possible as strongm pointed out - thanks.
 

But you are not using [tt]aname[/tt] in your Sub, you just print it into the Immiediate Window and that's all.

Why do you pass [tt]aname[/tt]?

Have fun.

---- Andy
 
Yes, exactly, I print it.

It is a routine to help with testing out a variety of 2-dimensional matrix functions. I print out results that have been stored in variables and inspect them to make sure it's working as expected.

It works fine passing the name of the variable - I just wondered if there was an easier way. It would be easier if I could call the function as:
call ma_print(a)
then as
call ma_print(a,"a")

I think my question has been answered by mstrong. Thanks for your interest.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top