Reply
Convert string to command in VB.NET
Old 01-15-2008, 04:37 AM Convert string to command in VB.NET
InfinitySchima's Avatar
Skilled Talker

Posts: 69
Name: Rafael Schimassek
I have to do a project for school in Visual Basic.net and I needed to know one function that could help me alot.
As I find that VB is far not as versatile as PHP I needed a function that would convert a string variable into a command, pretty much like the eval() function in PHP.
Has anyone some info about this? I searched for some time on the net but didn't found anything useful.

Schimassek...
InfinitySchima is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 01-15-2008, 01:02 PM Re: Convert string to command in VB.NET
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,744
Name: John Alexander
Quote:
Originally Posted by InfinitySchima View Post
As I find that VB is far not as versatile as PHP
Exactly the opposite is true. I don't want to hijack your thread with a debate on the merits of the two languages, but even Visual Basic.NET is an order of magnitude more powerful and versatile than PHP.

You want to use the CompilerServices namespace. This allows any of the following
  • Accept code in a text box, and run it
  • Take code the user enters, and create a DLL file from it. You could then run the code out of the dynamically linked library if you chose to, save it for later use, or even let the client download it.
  • Create a temporary web service with the user's code.
  • Compile many languages
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 01-15-2008, 03:03 PM Re: Convert string to command in VB.NET
InfinitySchima's Avatar
Skilled Talker

Posts: 69
Name: Rafael Schimassek
hm sure, my wrong of saying things without learning much of that language. It's true that I know very few of it, it just seemed so.
With "versatile" I wanted to say like that you can easily fusion things, like to display an image you echo a string in HTML where in the name of the image you just put the variable (something easy).

Ehhm..can you teach how to use that CompilerServices Namespace? I don't even need it to convert user-inputed text, just for things like: I want to change a PictureBox to a certain image, the image name depends on a value of a variable.

2 Off-Topic things: if on that statement I want to be converted I need to put " do I use something like \x22 ?
And, are there associative arrays in VB.NET, and can one array support multiple "spaces" of different types? like: var[1] = 345 and var['test'] = "hi" ?

Schimassek...
InfinitySchima is offline
Reply With Quote
View Public Profile
 
Old 01-15-2008, 06:07 PM Re: Convert string to command in VB.NET
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,744
Name: John Alexander
K, let's take this a piece at a time.
  1. The System.Runtime.CompilerServices is overkill for what you want to do. You don't need to actually evaluate and run code of unknown origin, it sounds like. There are a lot of simpler and faster (to run) options you have.
  2. You can use Response.Write to echo a string. A code sample to do what you want might look like Response.Write("<img src='x.jpg' alt='yyy' />") in VB.NET. That would let you not use a picture box at all.
  3. You can set the the ImageLocation property of a PictureBox. That would let you avoid the Response.Write method, and use code like pictureBox1.ImageLocation = "x.jpg" in VB.
  4. Naturally instead of hard coding "x.jpg" as a constant string, you can take the value from a variable that you might use If Then Else statements to fill out, or with a function you call.
  5. If you want to add a quote mark, you could do \x22 in C++ or C#, probably in J# too. In Visual Basic, you would use Chr(34) which returns ASCII character #34, which as a base 10 value maps to 22 in hex. Code might be Response.Write("<img src=" + Chr(34) + all the rest). You can create a const value, maybe called Q or QT, and set it to Chr(34) then refer to it by a more simple name to save effort.
  6. An array is usually strongly typed, but Visual Basic.NET can have actual arrays with different data types in each slot if you don't use Option Strict. That's not the best way to go, but it will work if that's what you're used to.
  7. A better alternative is the whole bunch of classes under System.Collections. The particular one you want is an ArrayList. This lets you set and get values of any type (including other ArrayLists, or custom types you write yourself) in any "slot" of the list. An ArrayList uses numeric indexing, so var[1] = x and var[22] = y.
  8. I'm not sure what an associative array is, but it sounds like it could be a HashTable? This also lives under the System.Collections namespace, and in most ways is very similar to an ArrayList. The main difference in terms of teh code you write is that now you can do things like var["application name"] = "my app" and var["app version"] = 2.7. You could use numbers instead of strings and make it look like an array, or you could even use a different data type, whatever you want.
  9. Anything in System.Collections.Generic forces you to strongly type the entire list, so every item is the same data type. Anything that's a level up, in System.Collections, stores its data as instances of System.Object (the base class from which all other classes ultimately descend) which is the ultimate in versatility.
  10. When you store an item in a list as an instance of System.Object, it adds a tiny amount of slowness to convert it back to whatever type it ultimately is. You also run the risk that if you saved a string like "x.jpg" and try to use it as an integer, that would provoke an error.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 01-16-2008, 04:26 PM Re: Convert string to command in VB.NET
InfinitySchima's Avatar
Skilled Talker

Posts: 69
Name: Rafael Schimassek
...
...that was an AWESOME explanation. Thanks a lot for all the info. This will really help me a lot.
I was used to the term "Associative Array" also because by fetching MySQL data I use mysql_fetch_array($result, MYSQL_ASSOC).
When I tried the first time to change an image I didn't knew that the name could be a string, I used something like (I don't remember it so well as I got the project at the school)
Code:
PictureBox1.ImageLocation = Globals.ProjectName.Resources.My.picturename
It was a strange code as I didn't even needed to put the picture extension (The picture was imported into the project before) and I could only hardcode the name. This was what originated my thought of using something like eval().
For the project I need to do I can't use any HTML (and also don't know how to use), it's plain VB with Forms and stuff. I want to do something big, so I was now creating an inventory of commands I would need for it.
I could also try Object Oriented Programming but I never did it before and don't even know if VB supports them (although im 95% sure it does), so I prefer to go with Arrays for now.

Schimassek...
InfinitySchima is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Convert string to command in VB.NET
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14345 seconds with 13 queries