Results 1 to 12 of 12

Thread: Variable Name to string and determining type of referenced variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Question Variable Name to string and determining type of referenced variable

    I have two questions that I think are simple, but are tough to search for, so here i am

    Firstly, I'd like to get a variable name into a string. Ex:

    Dim TestVar as integer = 12 <-- The variable type or value shouldn't matter
    Dim TestStr as string
    TestStr = "The name is " & name(TestVar)

    Therefore the value of TestStr should be "The name is TestVar"


    Second, I'd like write a sub with an if statement dependent on the type of variable referred. Ex:

    Dim TestVar as double = 1232.5 <-- the value shouldn't matter
    Dim TestStr as string
    Test(TestVar, TestStr)

    Private Sub Test(byRef SubVar, byRef SubStr)

    If type(SubVar) = integer then
    SubStr = "It's an integer"
    ElseIf type(SubVar) = double then
    SubStr = "It's a double"
    Else
    SubStr = "It's something else"
    End If

    End Sub

    Therefore the value of TestStr (or SubStr) should be "It's a double"

    Is there a simple way to do these?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable Name to string and determining type of referenced variable

    The first question makes no sense. Let's say that there was an operator 'foo' that got the "name" of a variable. You would have to use it like this in code:
    vb.net Code:
    1. Dim varName As String = foo(myVar)
    You would have to supply the variable itself to get the name anyway, so what's the point of getting the name in a string when you had to type the name anyway. You'd just do this:
    vb.net Code:
    1. Dim varName As String = "myVar"
    The second question is more reasonable, although you wouldn't use that method signature:
    vb.net Code:
    1. Private Function GetObjectType(ByVal obj As Object) As String
    2.     Dim message As String
    3.  
    4.     If TypeOf obj Is Integer Then
    5.         message = "It's an Integer"
    6.     ElseIf TypeOf obj Is Double Then
    7.         message = "It's a Double"
    8.     Else
    9.         message = "It's something else"
    10.     End If
    11.  
    12.     Return message
    13. End Function
    That said, you can also use the GetType method of any object to get a Type object that represents its type. The Type class is the basis for most reflection operations.
    vb.net Code:
    1. Dim typeName As String = obj.GetType().ToString()
    2. Dim message As String = "It's a " & typeName.Substring(typeName.LastIndexOf("."c) + 1)
    3.  
    4. MessageBox.Show(message)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Re: Variable Name to string and determining type of referenced variable

    First of all there is a point to the first question. If you had a hundred variables and a Sub that referenced them and wrote their names to a file, you wouldn't want to also have a hundred strings with the variable's names. So does anyone know how to do this? (read my example in the first post)

    Thank you for the answer to the second one though, also for this one how could you instead determine if the variable is an array or not?

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Variable Name to string and determining type of referenced variable

    The problem is, when you compile the program... the variable names are effectively lost because they don't matter. They're compiled down to memory position pointers and such. The machine understands them at that point as what they are. The variable names are just for the programmer's reference in the source code. I believe some data is retained if you have TRACE and DEBUG flags set though. I'm not the keenest when it comes down to the specifics of how the compiler functions and am going off what I know of other compilers.

    Now... I suppose you could write a text parser that could input the code as a text file and parse out all the variable names it detects and write them to an output file along with their type.

    I'm also wondering what the point is. Why would you even want to write variable names to a file except for programmer's documentation. I use comments for that.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Re: Variable Name to string and determining type of referenced variable

    Yeah, that makes sense about the variable names being lost Jenner, I actually learned the theory about how they're stored a very long time ago so I didn't think of that. Anyway, this is all for a file IO system I wrote to store configurations and settings for a MAME front-end I'm writing. It can save any number of variables and arrays in any order to file and then load them very easily. I realize there's other ways of doing this but I like the file format it creates, each variable or array instance creates one line with the variable name (including array # if it's an array) and the value. It runs perfectly, it's just that each variable needs an accompanying string array that denotes the variable name and 0 or 1 to identify array or not.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Re: Variable Name to string and determining type of referenced variable

    Ok, here is the solution I created. Since I'll only be working with integers and strings, I just use string arrays for everything and use CInt() for those that are meant to hold integers. Each array(0) will always hold the variable name and array(1 to upper bound) will hold values (the upper bound will just be 1 where I don't need an array). This eliminates the need for the nametag strings and cuts the number of variables needed in half. I'll post the code next if anyone cares to look.

  7. #7
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Variable Name to string and determining type of referenced variable

    Sweet! Always looking for nifty MAME frontends for my cabinet. Is it designed to work with cabinet controls or am I going to have to break out the the wireless keyboard? Will it look good on a 1024x768 Wells-Gardner?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Re: Variable Name to string and determining type of referenced variable

    Here it is, next step is to handle the errors if the file doesn't exist:

    Public Class Form1

    'Declare All Variables Needed for the File IO, these must be public
    Dim CurrentFile As String
    Dim FileRead As String
    Dim FileSec As String
    Dim VarPos As Integer
    Dim VarLen As Integer
    Dim TagStart As String = Chr(1)
    Dim VarStart As String = Chr(2)
    Dim VarStop As String = Chr(3)
    Dim i As Integer 'can be shared by all other processes

    'Declare All FilePaths, these should be public
    Dim TestFile As String = "D:\Games\MAME\RLFU\test.txt"

    'Declare Test Variables, all saveable variables must be public string arrays
    Dim VarInt(1) As String
    Dim ArrayInt(5) As String


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Assign Variable Names to (0) place for all saveable variables, this is very important
    VarInt(0) = "VarInt"
    ArrayInt(0) = "VarArray"

    End Sub


    Private Sub Read(ByRef Var) 'File Read Sub, Copy Exactly

    For i = 1 To UBound(Var)
    VarPos = FileRead.IndexOf(TagStart & Var(0) & i & VarStart)
    FileSec = FileRead.Substring(VarPos)
    VarPos = FileSec.IndexOf(VarStart) + 1
    VarLen = FileSec.IndexOf(VarStop) - VarPos
    Var(i) = FileSec.Substring(VarPos, VarLen)
    Next

    End Sub


    Private Sub Write(ByRef Var) 'File Write Sub, Copy Exactly

    Dim i As Integer
    For i = 1 To UBound(Var)
    My.Computer.FileSystem.WriteAllText(CurrentFile, TagStart & Var(0) & i & VarStart & Var(i) & VarStop & ControlChars.NewLine, True)
    Next

    End Sub


    Private Sub LoadTestFile() 'Sample Sub for loading all variables associated with a file

    'Set file to load as CurrentFile
    CurrentFile = TestFile

    'Load the file into FileRead, this is very important
    FileRead = My.Computer.FileSystem.ReadAllText(CurrentFile)

    'Load the desired variables from the file
    Read(VarInt)
    Read(ArrayInt)

    'Remove data from FileRead and FileSec
    FileRead = ""
    FileSec = ""
    'This is useful for performance, as these variables can get very large

    End Sub


    Private Sub SaveTestFile() 'Sample Sub for saving all variables associated with a file

    'Set file to save as CurrentFile
    CurrentFile = TestFile

    'Reset the file for rewrite, this is very important
    My.Computer.FileSystem.DeleteFile(CurrentFile)

    'Save the desired variables to the file
    Write(VarInt)
    Write(ArrayInt)

    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    'Test button which assigns user-defined values and saves them
    VarInt(1) = txtVarIntSave.Text
    ArrayInt(1) = txtArrayIntSave1.Text
    ArrayInt(2) = txtArrayIntSave2.Text
    ArrayInt(3) = txtArrayIntSave3.Text
    ArrayInt(4) = txtArrayIntSave4.Text
    ArrayInt(5) = txtArrayIntSave5.Text
    SaveTestFile()

    End Sub


    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

    'Test button which loads the saved values and displays them
    LoadTestFile()
    txtVarIntLoad.Text = VarInt(1)
    txtArrayIntLoad1.Text = ArrayInt(1)
    txtArrayIntLoad2.Text = ArrayInt(2)
    txtArrayIntLoad3.Text = ArrayInt(3)
    txtArrayIntLoad4.Text = ArrayInt(4)
    txtArrayIntLoad5.Text = ArrayInt(5)

    End Sub


    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click

    'Test button which sums the variables at any point
    'to check that they can operate correctly as integers
    For i = 1 To 5
    VarInt(1) = CInt(VarInt(1)) + CInt(ArrayInt(i))
    Next
    txtCheck.Text = VarInt(1)

    End Sub

    End Class

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable Name to string and determining type of referenced variable

    Quote Originally Posted by rrlslacker
    First of all there is a point to the first question. If you had a hundred variables and a Sub that referenced them and wrote their names to a file, you wouldn't want to also have a hundred strings with the variable's names. So does anyone know how to do this? (read my example in the first post)
    Even if variable names weren't lost when compiling this still makes no sense. Look at this:
    vb.net Code:
    1. Private Sub Method1()
    2.     Dim var1 As String
    3.     Dim var2 As Integer
    4.  
    5.     DisplayVarName(var1)
    6.     DisplayVarName(var2)
    7. End Sub
    8.  
    9. Private Sub DisplayVarName(ByVal param As Object)
    10.     Dim varName As String = GetName(param)
    11.  
    12.     MessageBox.Show(varName)
    13. End Sub
    Now, assuming that the GetName operator exists and returns the name of the variable, what output would that produce? It's going to show "param" twice isn't it? It's the param variable you're passing to the GetName operator so it's that name you'd be getting. That means that you cannot pass the variables to a method because you're creating a different variable. You'd have to call GetName on the original variables themselves, so you'd have to do this:
    vb.net Code:
    1. Private Sub Method1()
    2.     Dim var1 As String
    3.     Dim var2 As Integer
    4.  
    5.     Dim name1 As String = GetName(var1)
    6.     Dim name2 As String = GetName(var2)
    7.  
    8.     MessageBox.Show(name1)
    9.     MessageBox.Show(name2)
    10. End Sub
    As such you have to explicitly type the var1 and var2 variables in order to get their names, so how is that in any way advantageous over this:
    vb.net Code:
    1. Private Sub Method1()
    2.     Dim var1 As String
    3.     Dim var2 As Integer
    4.  
    5.     Dim name1 As String = "var1"
    6.     Dim name2 As String = "var2"
    7.  
    8.     MessageBox.Show(name1)
    9.     MessageBox.Show(name2)
    10. End Sub
    The answer is that it's not, so such a mechanism doesn't exist.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Re: Variable Name to string and determining type of referenced variable

    Actually it'll just be designed for mouse clicks, because I want to use X Arcade controls, including a track ball which takes over for the mouse.

    Basically what I'm aiming for is a whole cabinet with those controls, the keyboard will be in a locked pull out shelf just for maintenance (though now I like the wireless keyboard idea). So I'll have the power button wired to the side or back of the cabinet. When you turn that on the computer will start, then show a different logo instead of the windows loading screen (using some type of sheller), then it'll load straight to my front-end.

    From there you'll pick games with the track ball and a "menu" button on the control panel that's wired to left click (and will double as the exit key for mame). On the left will be the games list and on the right will be a screenshot for each game and a diagram of the controls. When you click play it loads the game in a home compiled mame that doesn't show any ok screens on any roms or that kind of garbage. Then when you're done you push the "menu" button to go back to the menu and then pick another game or click a shut down button. You'll be in Windows but you'll never see it

    It won't be the flashiest front end, but I'll try to make it look as much like a game console as I can and not like windows. I think I might use all bitmaps for text and not just plain font. I think one thing about it will be kind of unique. Before you hit play you'll have the option on some games to pick from a variety of preset control schemes. This can be accomplished just by telling mame to load different cfg files for the different presets (there's a command line option to change the directory they're loaded from).

    I got the idea for this because I know that people will come over and say "oh well I'm really good at Street Fighter III (or whatever game) but I need a PS2 controller to play it." I'll have those hanging on the side and three different presets for games like that - #1 both players use arcade controls, #2 p1 uses arcade, p2 uses a ps2 controller, #3 both players use ps2 controllers (in case two of these people come over).

    Now that I think about it, maybe I'll just have check boxes for "enable PS2 controllers." I'm also thinking about how to support a bunch of other console pads, but I already have a USB adapter for two PS2 controllers and I don't want to take focus away from the arcade control panel.

    Aaaanyway, I'll optimize it for anywhere from 640 to 1024 depending on whether I use a TV or a Wells-Gardner. It'd have to be 800 for the 27" WG. I think that the largest WG that does 1024 is 19"? I don't know, I suppose eventually I should have settings for all three.

    Maybe I should make a post about VB and MAME.

  11. #11
    Lively Member ComITSolutions's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    94

    Re: Variable Name to string and determining type of referenced variable

    This is with respect to the first question.
    for your second requirement you can use following code
    Private Sub Test(ByRef SubVar, ByRef SubStr)
    SubStr = "The vaule type is " & TypeName(SubVar)
    End Sub

    If you are thinking of dynamically to declare and use the variables, then you can create your own class and collection to solve.

    If need any help reg this. I can help you

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    20

    Re: Variable Name to string and determining type of referenced variable

    Thanks for the suggestion, ComIT. The solution I settled on above is working very well, so I'm going to use it for now just because it's a lot simpler. However, if I find that it becomes inflexible (which is easily possible), I'll have to use your method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width