Jump to content

get variable name


gcue
 Share

Recommended Posts

48 minutes ago, iamtheky said:

Are we really listing all the ways it doesnt work?  Instead of acknowledging that I aced an answer to Post #1 at slot #13, yall had nothing but bloviating about safety.  Im about to include the scope creep at #16 ftw too.

I just wanted to report more cases of errors :)
want you to fix or someone will write a new code to work.

38 minutes ago, JohnOne said:
GetTheVariableNameOfThis(StringSplit("word", ""))

Func GetTheVariableNameOfThis($Value)
    MsgBox(0,0, VarGetType($Value))
Endfunc

 

It is to get types, not the name of the variable.

Regards,
 

Link to comment
Share on other sites

This can certainly be done, but the methods to do it may fall foul of forum rules and I believe terms of usage. However, a program can be designed which monitors its own behaviour without contravening these terms/rules. Here is an example which monitors itself as it passes a random global variable to the function WhatIs(). It has limited application. In this simple case, the calls to Random() could have been monitored more easily: instead of testing the variable name. This comparison may not work for all data types.

#include <AutoItConstants.au3>

Global $g__aGlobalVariables = ['g__It_Is_Me', 'g__Now_It_Is_Me'] ; global variable names are added to this array [do not include constants]
Global $g__It_Is_Me = 0, $g__Now_It_Is_Me = 1

For $i = 1 To 10
    ; send a random global variable to a function
    If Random(0, 1, 1) Then
        ConsoleWrite('$' & WhatIs($g__It_Is_Me) & @LF)
    Else
        ConsoleWrite('$' & WhatIs($g__Now_It_Is_Me) & @LF)
    EndIf
Next

Func WhatIs(ByRef $vAnyGlobalVariable) ; ByRef must always be used for this method to work (also by all calling functions)
    Local $vTemp = $vAnyGlobalVariable
    For $i = 0 To UBound($g__aGlobalVariables) -1 ; loop through the list of variables
        If IsDeclared($g__aGlobalVariables[$i]) = $DECLARED_GLOBAL And $vAnyGlobalVariable == Eval($g__aGlobalVariables[$i]) Then
            If Assign($g__aGlobalVariables[$i], Not $vAnyGlobalVariable, $ASSIGN_EXISTFAIL) = 1 Then
                If $vAnyGlobalVariable <> $vTemp Then
                    $vAnyGlobalVariable = $vTemp
                    Return $g__aGlobalVariables[$i]
                EndIf
                Assign($g__aGlobalVariables[$i], $vTemp, $ASSIGN_EXISTFAIL)
            EndIf
        EndIf
    Next

    Return SetError(1) ; don't know what it is!
EndFunc

 

Edited by czardas
Link to comment
Share on other sites

All these pointless scripts to do something the OP has already demonstrated how to do it in his first post? The easiest way is to write the script to do what you want when you write it. There's no built-in way to do it, and any other method is WAY overkill, especially when the way demonstrated already is the simplest way to do it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...