Aaron Levy
Medicated Lately?
Join date: 3 Jun 2004
Posts: 2,147
|
07-12-2004 00:55
Situation: I have a variable called strCardFace1 which holds a one-letter code (D,H,S or C) for the type of suit a card is. I wrote some flow control to assign the full name of the suit to a string called strCardSuit1. The flow control I'm using to do this is below... string strCardSuit1 = "NULL";
if (strCardFace1 = "D") { string strCardSuit1 = "Diamonds"; } else if (strCardFace1 = "H") { string strCardSuit1 = "Hearts"; } else if (strCardFace1 = "S") { string strCardSuit1 = "Spades"; } else if (strCardFace1 = "C") { string strCardSuit1 = "Clubs"; }
I get no errors and it looks like the above should work. However, whenever I use the code above, strCardFace1 is somehow STUCK with the value of "D" and strCardSuit1 is never set to anything, it remains "NULL". If I completely comment out the flow control, strCardFace1 keeps its regular value that I obtained earlier in the script. I am confused because no where in the flow control do I ever state that strCardFace1 should = "D". What am I doing wrong?
|
Moopf Murray
Moopfmerising
Join date: 7 Jan 2004
Posts: 2,448
|
07-12-2004 01:28
Comparisons should be == not = (you're actually assigning the values in in the if statement which is why it's always D, as the first will always be true)
|
Aaron Levy
Medicated Lately?
Join date: 3 Jun 2004
Posts: 2,147
|
07-12-2004 01:42
Doh! My days of MS-Basic still come back and haunt me! Thanks Moopf, maker of the BEST jetpack in Second Life! (shameless plug)
|
Moopf Murray
Moopfmerising
Join date: 7 Jan 2004
Posts: 2,448
|
07-12-2004 01:48
From: someone Originally posted by Aaron Levy Doh! My days of MS-Basic still come back and haunt me! Thanks Moopf, maker of the BEST jetpack in Second Life! (shameless plug) It's a common error, something most of us still do from time to time! And thanks for the plug. lol.
|
Aaron Levy
Medicated Lately?
Join date: 3 Jun 2004
Posts: 2,147
|
07-12-2004 02:16
Is there anything else in that code that would prevent it from assigning a value to strCardSuit1?
Right before I hit the "if/else" statements, I have it say the value of strCardFace1, and it is correct.
However, at the end of the "if/else" statements, strCardSuit1 has not been assigned a value.
|
Azelda Garcia
Azelda Garcia
Join date: 3 Nov 2003
Posts: 819
|
07-12-2004 02:21
You're redeclaring your variable within each if, so within each if block there are actually two variables called strCardsuit1: the one at the start ofthe code, and the one in the if statement.
Ditch the string prefix on the variables in the if blocks and it should work.
Theres a concept called "scope". Scope is the bit of your program where a particular variable exists. The if blocks are their own scope (basicaly anything in {} is), so you're creating new variables in each if block and theyre deleted straight away at the end of the block.
Azelda
|
Aaron Levy
Medicated Lately?
Join date: 3 Jun 2004
Posts: 2,147
|
07-12-2004 02:23
Thank you Azelda!! The moment I took out all those "string"s it worked!! 
|
Moopf Murray
Moopfmerising
Join date: 7 Jan 2004
Posts: 2,448
|
07-12-2004 02:24
What Azelda says  I saw the comparison problem in your code and didn't look further than that, fortuantely Azelda took more time over answering. lol 
|