Jump to content

How I Retrieve Inside The Function The Name Of A Variable Entered With Byref Keyword?


Recommended Posts

Can be that I becomes more blind every day... :)

$stuff="init"
SomeFunction ($stuff)

Func SomeFunction (ByRef $v_variable)
    Local $s_name=VarGetName ($v_variable); <==$s_name variable should take a "stuff" string value: $s_name="stuff"
    If... then... something else.
EndFunc

How do I make something like that? That is to say... How I retrieve inside the function the name of a variable entered with Byref keyword?

Note: The function "VarGetName" not exist. The example is only demonstrative of what I want to make.

Link to comment
Share on other sites

I fear that I do not understand what you are wanting, but try this:

$stuff = "init"
$another = 0

MsgBox(0, "Before", $stuff)
SomeFunction($stuff, "stuff")
MsgBox(0, "After", $stuff)

MsgBox(0, "Before", $another)
SomeFunction($another, "another")
MsgBox(0, "After", $another)

Func SomeFunction(ByRef $v_variable, $v_name)
    If $v_name = "stuff" Then $v_variable = "changed"
    If $v_name = "another" Then $v_variable = 1
EndFunc  ;==>SomeFunction

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

I fear that I do not understand what you are wanting, but try this:

$stuff = "init"
$another = 0

MsgBox(0, "Before", $stuff)
SomeFunction($stuff, "stuff")
MsgBox(0, "After", $stuff)

MsgBox(0, "Before", $another)
SomeFunction($another, "another")
MsgBox(0, "After", $another)

Func SomeFunction(ByRef $v_variable, $v_name)
    If $v_name = "stuff" Then $v_variable = "changed"
    If $v_name = "another" Then $v_variable = 1
EndFunc ;==>SomeFunction

Thanks @herewasplato, my question has been if there was some way to obtain the name from a sending variable to the function with Byref. Something like "VarGetName ($var)"...

Although your code also work. Thank you for your help anyway.

Link to comment
Share on other sites

...Although your code also work...

Good, then I did understand what you wanted to do. :-)

...but I do not know how to do it another way. :-(

@Others,

How could one determine the name of the variable used in the line that called a function???

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Developers

@elgabionline

Understand the question, but fail to see why you would need to know this ... please explain ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@elgabionline

Understand the question, but fail to see why you would need to know this ... please explain ?

Simply Curiosity... :(

I was "experiencing" with isdeclared and assign. I tripped with this while my mind digressed.

stuff="init"
IsChanged ($stuff)
$stuff="change"
If IsChanged ($stuff) Then MsgBox (0,"CHANGED","New value")


Func IsChanged (ByRef $v_variable)
    Local $name=VarGetName ($v_variable); $name variable should take a "stuff" string value: $name="stuff"
    
    If Not IsDeclared ($name&"_duplicate") Then
        Assign ($name&"_duplicate",$v_variable,2); it "clones" the variable
        Return False
    Else
        If Eval ($name&"_duplicate")<>$v_variable Then
            Assign ($name&"_duplicate",$v_variable)
            Return True
        EndIf
    EndIf
EndFunc

You are not too hard with me, I think that it is my fool way to make:

if $stuff<>$stuff_duplicate then...  (do something); lol

This is something very silly, but to be honest it is when ideas comes... although, for some strange reason, I think that it can end up being useful. :)

Link to comment
Share on other sites

...Understand the question, but fail to see why you would need to know this ... please explain ?

I know that this question was directed at elgabionline, and I have little use for a function like VarGetName - but I see it as a way to pass two bits of info to the function with one variable.

The function could then branch based on the variable name that called it and use the value of the variable as well.

_Receipts($Monday)
_Receipts($Tuesday)
_Receipts($Wednesday)

Func _Receipts($Amount)
    If VarGetName ($Amount) = "Monday" _
    And $Amount > 1000 Then _
    MsgBox(0, "", "Book flight out of country."
EndFunc  ;==>_Receipts

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Moderators

I would say just code it properly using Arrays to keep track of what you want to.

I mean something like this seems like alot of work:

$Monday = 'Monday'
$Tuesday = 'Tuesday'
$Wednesday = 'Wednesday'
$Thursday = 'Thursday'
$Friday = 'Friday'
$Saturday = 'Saturday'
$Sunday = 'Sunday'
_ShowVarSent('1' & $Monday)
_ShowVarSent('3' & $Wednesday)
_ShowVarSent('5' & $Friday)
_ShowVarSent('7' & $Sunday)

Func _ShowVarSent($show)
    Local $AllVars[8] = ['', '$Monday', '$Tuesday', '$Wednesday', '$Thursday', '$Friday', '$Saturday', '$Sunday']
    MsgBox(0, '', $AllVars[StringLeft($show, 1)] & ' = ' & StringTrimLeft($show, 1))
EndFunc

BTW, this topic had been discussed before in some detail a few months ago by Chris something or nother.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I would say just code it properly...

Is this type of function common in other languages?

Even if it is, I'm not asking for it to be added to AutoIt as I have little interest in this.

However, I do see how such an AutoIt function could double the info passed to a UDF.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

BTW, this topic had been discussed before in some detail a few months ago by Chris something or nother.

I was looking for in the forum before post this topic, and I could not find anything. Would you be able to guide me with some title, or keywords, please?

However, the same as @Herewasplato, I was not thinking in" to add" anything in the Autoit. I simply wanted to know if something existed as that because I didn't find it. For that reason this is here and not in Idea Lab forum.

The function could then branch based on the variable name that called it and use the value of the variable as well.

Their example of use of VarGetName until I found useful... :) ( I would add at your code only Byref to the pass of variable).

Now I think although don't make anything that the AutoIt today could not make, it could simplify in some cases the code, but I don't know if it is worthwhile...

Edited by elgabionline
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...