Computing.Net > Forums > Programming > Add two strings to form variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Add two strings to form variable

Reply to Message Icon

Name: Dr. Nick
Date: March 27, 2003 at 17:24:07 Pacific
OS: WinXP Pro b2600 SP1
CPU/Ram: P4 2.0Ghz & 1024MB p133
Comment:

I need to add two strings together to form a variable name. The purpose of the function is to replace an image with the name "truck" with another pre-loaded image "truck_2".

ie:

function replace(img)
{
img.src = img_2.src;
}

I call the function like this: replace(truck);

Without the variables, the function should work like this:

function replace()
{
truck.src = truck_2.src;
}


Sorry if it's confusing! If you don't get what I mean, please ask!

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: Dr. Nick
Date: March 27, 2003 at 17:34:11 Pacific
Reply:

Sorry!

I forgot to mention that this is in Javascript.

Thanks.


0

Response Number 2
Name: SN
Date: March 28, 2003 at 10:20:05 Pacific
Reply:

Are you sure you want to do this that way? Turning the contents of a variable into a variable borders on self-modifying code, which to my knowledge javascript doesn't support (except in the sense that javascript can output javascript) Consider one of the following approaches:

1. Is it possible for you to rename the image files to something like
"truck_1.jpg"
"truck_2.jpg"
...
This way the javascript could dynamically change the source of the img. This is the approach I went with on the homepage of www.sharingds.org (rotating pictures on the top.)

2. If renaming the files isn't an option, consider having an array of the file names, and that way the javascript could just go through the array to reload and swap the images to your liking.

If either of these approaches sounds like it might work, feel free to post back for help implementating them.

Sorry I couldn't solve the original problem,
-SN


0

Response Number 3
Name: Dr. Nick
Date: March 31, 2003 at 14:47:21 Pacific
Reply:

Thanks for the suggestions. I think I'm just going to create an array of images to solve my problem.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


addRow() in JTable.... Not work, Printer Prog fo...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Add two strings to form variable

how to combine two strings into one www.computing.net/answers/programming/how-to-combine-two-strings-into-one/800.html

Extract text between two strings www.computing.net/answers/programming/extract-text-between-two-strings-/18039.html

Find string inside a variable,batch www.computing.net/answers/programming/find-string-inside-a-variablebatch/12011.html