Bizarre Question - Accessing Variables w/ a String

Im obsessed with finding a way to do this, haha, but i dont know
if its remotely possible.
Instead of explaining it i will post some pseudo-code...
class HackClass{
int AVar = 1;
int BVar = 2;
int CVar = 3;
// ...use some arbitrary method of getting user input
String input = // user's input, in this case it is = "AVar"
// and then what im trying to achieve...
// im looking for something like this ---> (variable)input = 5;
System.out.println("Avar = " + AVar); // this would output 5
Note, this is very oversimplified. So though it would be easy in this example, i am not looking to register every
variable in some table and then use some elaborate lookup function with the string, rather, is there a way to refer to a variable in code with a string?
I cant imagine there is, but that would be WICKED.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Wow. Thanks for the code.
Im writing test code now.
You said this was "java-beanish"... is it reflection?
Because the only way it compiles is if i import...
import java.lang.reflect.*;also, in your code you never defined the variable 'object' in
method.invoke(object, invokeParam);and for anyone else interested in the code - here is my test class:
import java.lang.reflect.*;
public class HackThis{
public static void main(String[] args) {
     String var = "Var1";
     Integer value = new Integer( 1234 );
     HackThis ht = new HackThis();
     Class[] param = new Class[1];
     param[0] = value.getClass();
     Method method = ht.getClass().getMethod("set" + var, param);
     Object[] invokeParam = new Object[1];
     invokeParam[0] = value;
     method.invoke(object, invokeParam);
public HackThis(){
public Integer getVar1(){
     return Var1;
public void setVar1(Integer i){
     Var1 = i;
     Integer Var1 = new Integer(99);
}...but note it wont compile because 'object' hasnt been defined yet

Similar Messages

  • Simple coding question: Adding variable to a string

    I can't figure out what the syntax is for this in Actionscript. Basically, I have a variable I want to add into a string that will call a certain frame based on user's language selection. So...
    var currentPage:int; <<this updates dynamically when nextPage, previousPage, etc are called
    if (currentLanguage == "English") {
         myMovie.gotoAndStop ("English%n", currentPage);
    else if (currentLanguage == "Spanish") {
         myMovie.gotoAndStop ("Spanish%n", currentPage);
    Then each frame of myMovie will be named "English1" "Spanish1" "English2" "Spanish2" etc to correlate with page numbers.
    Is this possible in Actionscript 3.0?
    Thanks!

    sure.  you can use a variable in place of the frame label and/or the scene name.  from your code, you're using a variable in place of the scene name.  that should work (if you're using scenes).
    if you want to go to frame label english1, when currentPage is 1, you should use:
    myMovie.gotoAndStop("english"+currentPage,possiblescenenamehere);
    and because i don't think you're using scenes, just use:
    myMovie.gotoAndStop("english"+currentPage);
    or, even easier:
    var currentPage:int;  // defined somewhere;
    var language:String;  // defined somewhere;
    myMovie.gotoAndStop(language+currentPage);

  • Accessing variable names using String

    I have to check up to 13 JCheckBox variables to see if they are checked or not, and I have the variables named: count1, count2, count3,...,count13. I wanted to know if there was a way to go through them in a while loop until I found the last one that was checked. I want to do the following:
    int i = 1;
    while([count+i].isSelected()) {
    i++;
    int foodCount = i;
    //foodCount is stored in database
    I am doing this for a pet store and I don't want to have 12 extra columns in the database - I just want to have one for the overall food count. Any ideas how I can dynamically create a variable name?

    Put the JCheckBoxes in an arrayJCheckBox[] myCheckBoxes = {check1, check2, check3,...,check13};
    JCheckBox someCheckBox = null;
    for (int x=0; x<myCheckBoxes.length; x++) {
      if (myCheckBoxes[x].isSelected()) someCheckBox = myCheckBox[x];
    }

  • Variable or Substitution String in Interactive Report Filter

    Background: I have an application that has a number of customized Interactive Reports where the Filter on the reports is set to a custom company name. When I install the application, I do not want to go through the reports and change the filter for the new company's name. The filters I need to change are all set by LOVs.
    Question: Is there a way that I can do one of the following:
    - associate a Filter with a Variable or Substitution String such that I can set that variable or string on login and then the user always sees the correct Filter displayed in the Interactive Report?
    - or is there a code method to update a sql apex table behind the scenes to reset the Filter to the new value. So that I could run that after installation as part of a customization / set-up time?
    System Info: 4.1.0.00.32
    Thanks,
    Stephen
    I used the IR_FILTER function, and while it allowed for a new filter to be set. It did not save the filter after logout or for other users accessing the Primary report. I could not find a SAVE Interactive Report function. So, my question still stands on looking for a method to set and save Filters programatically.
    Edited by: slsmith on Apr 19, 2012 8:07 PM

    Hi,
    I don't have access to an Apex 3 environment to check this, but I think that the Apex 4 Interactive Reports save the filters slightly differently and an attempt to mimic that functionality using IR conditions would fail.
    The only alternative I can think of right now is to have an additional column that returns 0 or 1 - eg:
    SELECT FIELD1,
    FIELD2,
    FIELD3,
    CASE WHEN (FIELD1 IS NULL OR FIELD2 IS NULL OR FIELD3 IS NULL) THEN 1 ELSE 0 END INCLUDE_NULLS
    FROM YOURTABLEand then apply a filter to INCLUDE_NULLS = 1
    Andy

  • How to Access Variables defined in the PopUpWindow

    Hi Friends
    I am building a flex site using flex builder 2.....
    Here I need your help.....
    My Problem is how to store a variable from a popup window
    I have this problem while in the login window comes as popup
    while clicking login button in the page a popup where user
    can enter username and password...
    after submitting if the login is successufl the popup
    vanish...
    After a successful login I need to store the logged user name
    in the main index page but I am not able to store
    At first I created a variable 'loggedUsername' in the popup
    panel and after success log i assigned the username to it ... after
    it i am not able to get the loggedUsername..
    doubting I defined the variable in the main index.mxml page
    but this variable is not accessible from the loginwidow.mxml page
    where the popup will function...
    Heip me
    how to store the name if the user login is success..
    thanks
    Chintu...

    Lets say you have a public property in your application like
    public var name:String = "John Smith";
    to access this using an inline item renderer:
    <mx:itemRenderer>
    <mx:Component>
    <mx:VBox>
    <mx:TextInput text={outerDocument.name} />
    </mx:VBox>
    </mx:Component>
    </mx:itemRenderer>
    To access variables of the applicaton you can use
    Application.application.name (for example) to reference "global"
    variables, so you might also use this technique. With an inline
    item renderer, the outerDocument property will refer to the
    component which contains the renderer.

  • How to access variables declared in main mxml in itemrenderer files

    Hi,
    I have a main mxml which has a cutomRenderer that defines two
    mxml components
    namely raidobutton and textinput.
    Now in this TextInput.as i need to access variable defined in
    main mxml.
    Please suggest a way.
    Thanks,
    Lucky

    Lets say you have a public property in your application like
    public var name:String = "John Smith";
    to access this using an inline item renderer:
    <mx:itemRenderer>
    <mx:Component>
    <mx:VBox>
    <mx:TextInput text={outerDocument.name} />
    </mx:VBox>
    </mx:Component>
    </mx:itemRenderer>
    To access variables of the applicaton you can use
    Application.application.name (for example) to reference "global"
    variables, so you might also use this technique. With an inline
    item renderer, the outerDocument property will refer to the
    component which contains the renderer.

  • Variable name to string

    Hi, I'm really new to TestStand, so this is probably an easy question:
    I'm using TestStand 4.5 and I'm trying to convert the name of a variable, Locals.MaxArrayIndex, which is a numeric variable, into a String, Locals.VarString.
    I know that the Str() function can be used to convert a numeric into a String, but how can I store the actual words "Locals.MaxArrayIndex" in a string?
    Thanks! 

    Hmmm...it works great for Parameters that are arrays, but for me it won't work if the Parameter is a numeric variable. Maybe I'm making a mistake somewhere else that I'm not picking up; so I attached the sequence file if you want to take a look at it.
    The issue arises in the 'Else' statement. I'm pretty sure the syntax in my function and popup are correct, but the String (ParamName) that is supposed to hold the value of the Parameter variable name (DataSelect) doesn't seem to ever be filled and outputted in the error message.
    I would really appreciate it if any of you guys could have a look at the file (I'm sure I'm making some kind of really rookie mistake)
    Thanks! 
    Attachments:
    GenerateError.seq ‏9 KB

  • Assigning a node value from an XML variable to a String type  in Weblogic Process Integrator

    Hi,
    Is there any way to assign a node value from an XML variable to a String variable
    in Weblogic Process Integrator...
    Thanx.
    Narendra.

    Nerendra
    Are you talking about using Xpath on the XML document and assigning to a
    variable, it is unclear what you are asking
    Tony
    "Narendra" <[email protected]> wrote in message
    news:3bba1215$[email protected]..
    >
    Hi,
    Is there any way to assign a node value from an XML variable to a Stringvariable
    in Weblogic Process Integrator...
    Thanx.
    Narendra.

  • How to store serval char variables into a string variable?

    I have serval char variables, but i don't know how to put them together (without using arrays). I am thinking to store these char variables into a string variable but i don't know how to do it. For example,
    char letter1 = 'a', letter2 = 'b', letter3 = 'c';
    String letters;
    then how can i do to make letters = "abc" from using letter1,2,3?
    I am just a beginner of Java, if anyone can help me, i will appreciate that very much!!!

    String letters=""+leter1+letter2+letter3;is fine and dandy. What it actually compiles to is
    String letters = new StringBuffer().append(letter1).append(letter2).append(letter3).toString();Which ofcourse is much more code to write, but still good to know.
    So do see the API for java.lang.StringBuffer.
    Heikki

  • How to access variables outside user exit

    Hi,
    I'm working with a user exit and my problem is that in a particular moment I have to access variables located outside the scope of the user exit (they are in a standard program)
    How can I reach these variables?
    thanks in advance

    Hi,
    If they are global variables then you can access them using Global assign technique,
    For example,
    FIELD-SYMBOLS: <fs_value> TYPE ANY.
    ASSIGN ('(SAPMV45A)XVBAK') TO <fs_value>.
    It is basically,
    ASSIGN ('(<Std. Program Name>)<Variable name>') TO <field symbol>.
    NOTE: To make sure they are accessible in your user exit, just put a break-point in the user exit and once you are there in debugging, type in,
    (<Std. Program Name>)<Variable name> in the Field names section and if it does not show it in RED then it is accessible..
    Hope this helps.. 
    Sri
    Message was edited by: Srikanth Pinnamaneni

  • Geting a variable from a String

    Hi, i need to get a variable from a string. For example:
    int test1;
    int test2;
    int test3;
    int test = getvariablefromstring("test"+3);
    ..................

    Reflection will work for member variables, not locals. It'sprobably not the right solution for you though.
    If you just want test1, test2, test3, ... testN, then you want an array or a List.
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
    http://java.sun.com/docs/books/tutorial/collections/

  • Question on Variables in OBIEE

    Hi,
    I am prasanna.I have a question on variables.
    Is it necessary to create a different connection pool for session variables? I am unable to use the same connection pool for both repository and session variables.
    can anyone help me.
    Regards,
    prasanna.

    Hi Prasanna,
    You can use the same connection pool by checking out “Allow First Connection for init block” to true
    For setting *“Allow First Connection for init block”* follow below steps :
    1. In the Administration Tool, select Tools, then select Options to display the Options dialog.
    2. On the General tab, select ”Allow First Connection for init block.
    But this option is not a best practice and might cause performance issues.
    Better think is to use Different connection pool for variables.
    Try this...

  • Can't access variables in specific S7-1200 DB's in LabVIEW project

    Hi all,
    I'm trying to establish a connection between LabVIEW and a Siemens S7-1200 though Ethernet and SIemens OPC Server.
    The physical connection is OK (I can ping S7-1200 with no problem).
    When I needed to access variables from specifics DB's inside S7-1200 ( I want to access variable DB190,X0.4), I called Siemens support and they said I had to modify the variable's definition when using OPC Scout, from "MX0.4" to "DB190,X0.4", and then it was possible to access this variable.
    Same solution (renaming the path) applies to NI OPC Client, I can read and write variables properly.
    My problem is that when I try to add variables in LabVIEW Project, I can't find those variables whose address were modified, so I can't access the correct variable in my program.
    I tried to change the variable path in Multiple Variable Editor, but it doesn't work either.
    Any suggestions on what I can try??

    First: Avoid the ODBC/JDBC Bridge if at all possible.
    It's the worst JDBC driver I've ever seen. It's buggy
    and a great hindrance both to learning and to
    producing usefull code.I agree that there might be problems with M$ Access, but there are problems with all databases, including MySQL (e.g., no referential integrity in free download version). It's capable enough for the query the OP is trying to execute.
    The problem is with his code, not Access or the bridge driver. He'll go through a lot of effort to switch databases and still have this problem. Better to understand what HE'S done wrong and fix it so he'll do it correctly for all databases, including Access.
    You just need to be more careful with your query, I'm sure.
    %

  • Javascript variable names in strings

    This should be easy, but i can't find any info on how to do
    it!
    It's easy to put the contents of a variable into a string but
    how does one place the name of a Javascript variable into a
    string??
    The only solution i have found is to store the name
    separately as a string, but this isn't very neat.

    OK, simple statement first:
    Integer does not equal string.
    You also seem to be tossing around "runtime" and "static" somewhat liberally. Once you wire an integer (be it a control or block diagram constant) to the case structure's selector, the cases can only take on integer values. This is by design. If you wire a string, the case items take on string values. If you wire an enum, the case items take on the enumeration values. Once you've defined a case structure's cases you can't change them at "runtime". This, by definition, is different code.
    That said, it seems to me that you're looking for an enum.

  • Writing variable as a string

    im trying to write variable as a string and i get weird errors.
    this is the query:
    select *
    from [ebs-DB2].ebenefits_Repl.dbo.TBL_Enrollment
    where enr_enrollmentid in (select EnrollmentID 
    from FileTransfer.dbo.StLukes_Wellness_DataRecord_V1
    where Employee_Status in ( 'E' )) and  Con_IPE_StatusId in ( 'APPR' )
    and when i write it this way:
    declare 
    @string varchar (100) = N'[ebs-DB2].ebenefits_Repl.dbo.'
    ,@e varchar (4) = 'E'
    ,@APPR varchar(4) = 'APPR'
    ,@SQLSplit1 varchar (700)
    ,@SQLstatement nvarchar (max)
    set @SQLStatement=N'
    select *
    from '+@string+'TBL_Enrollment
    where enr_enrollmentid in (select EnrollmentID 
    from FileTransfer.dbo.StLukes_Wellness_DataRecord_V1
    where Employee_Status in ('+@E+')) and Con_IPE_StatusId in ('+@APPR+')
    print @SQLStatement
    EXEC sp_executesql @SQLStatement
    it generates error saying " Invalid column name 'E'. " " Invalid column name 'APPR'. "
     

    set @SQLStatement=N'
    select *
    from '+@string+'TBL_Enrollment
    where enr_enrollmentid in (select EnrollmentID
    from FileTransfer.dbo.StLukes_Wellness_DataRecord_V1
    where Employee_Status in ('''+@E+''')) and Con_IPE_StatusId in ('''+@APPR+''')
    Tweak it as above and see
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Maybe you are looking for