|
-
Feb 12th, 2008, 02:36 AM
#1
Thread Starter
Junior Member
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?
-
Feb 12th, 2008, 03:27 AM
#2
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:
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:
Dim varName As String = "myVar"
The second question is more reasonable, although you wouldn't use that method signature:
vb.net Code:
Private Function GetObjectType(ByVal obj As Object) As String Dim message As String If TypeOf obj Is Integer Then message = "It's an Integer" ElseIf TypeOf obj Is Double Then message = "It's a Double" Else message = "It's something else" End If Return message 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:
Dim typeName As String = obj.GetType().ToString() Dim message As String = "It's a " & typeName.Substring(typeName.LastIndexOf("."c) + 1) MessageBox.Show(message)
-
Feb 12th, 2008, 11:00 AM
#3
Thread Starter
Junior Member
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?
-
Feb 12th, 2008, 11:38 AM
#4
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.
-
Feb 12th, 2008, 02:36 PM
#5
Thread Starter
Junior Member
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.
-
Feb 12th, 2008, 02:52 PM
#6
Thread Starter
Junior Member
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.
-
Feb 12th, 2008, 03:29 PM
#7
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?
-
Feb 12th, 2008, 03:37 PM
#8
Thread Starter
Junior Member
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
-
Feb 12th, 2008, 08:40 PM
#9
Re: Variable Name to string and determining type of referenced variable
 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:
Private Sub Method1()
Dim var1 As String
Dim var2 As Integer
DisplayVarName(var1)
DisplayVarName(var2)
End Sub
Private Sub DisplayVarName(ByVal param As Object)
Dim varName As String = GetName(param)
MessageBox.Show(varName)
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:
Private Sub Method1()
Dim var1 As String
Dim var2 As Integer
Dim name1 As String = GetName(var1)
Dim name2 As String = GetName(var2)
MessageBox.Show(name1)
MessageBox.Show(name2)
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:
Private Sub Method1()
Dim var1 As String
Dim var2 As Integer
Dim name1 As String = "var1"
Dim name2 As String = "var2"
MessageBox.Show(name1)
MessageBox.Show(name2)
End Sub
The answer is that it's not, so such a mechanism doesn't exist.
-
Feb 12th, 2008, 08:53 PM
#10
Thread Starter
Junior Member
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.
-
Feb 12th, 2008, 11:51 PM
#11
Lively Member
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
-
Feb 13th, 2008, 01:28 AM
#12
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|