1

I am about to explode. I have been looking for two hours for a way to fix this problem. I have a switch statement in the setTimer method. When i debug the program, the timerType value changes while the method is in action, but as soon as it exits, timerType reverts back to null. This makes my case statement kind of useless as I need it to change for the next time I call the method. I would love your helping hand as I am sure it is something simple as usual :( . I tried changing it to an int to see if it was something to do with the String type or whatever. Im a bit of a noob. Please stop me from floundering any longer (at least on this particular problem :) )

public string timerType;



if (checkRoundChanged()) {
     SoundPlayer.Play();
     setTimer(timerType);
}

and the method

protected void setTimer(String timerType){
        switch (timerType) {
            case "ready":
                secLeft = readySec;
                minLeft = readyMin;
                timerType = "round";
                break;
            case "round":
                secLeft = roundSec;
                minLeft = roundMin;
                timerType = "rest";
                break;
            case "rest":
                secLeft = restSec;
                minLeft = restMin;
                timerType = "round";
                break;
            case "relax":
                secLeft = relaxSec;
                minLeft = relaxMin;
                timerType = "done";
                break;
            default:
                timerType = "ready";
                break;
        }

    }

Thankyou!

1 Answers1

4

Strings are passed by value, not by reference. You can either return the new value:

protected string setTimer(String timerType){
    switch (timerType) {
        case "ready":
            secLeft = readySec;
            minLeft = readyMin;
            timerType = "round";
            break;
        case "round":
            secLeft = roundSec;
            minLeft = roundMin;
            timerType = "rest";
            break;
        case "rest":
            secLeft = restSec;
            minLeft = restMin;
            timerType = "round";
            break;
        case "relax":
            secLeft = relaxSec;
            minLeft = relaxMin;
            timerType = "done";
            break;
        default:
            timerType = "ready";
            break;
    }

    return timerType;
}

....

timerType =  setTimer(timerType);

Or pass by reference:

protected void setTimer(ref String timerType) {
   ...
   timerType = newValue;
   ...
}

setTimer(ref timerType);

Here is some recommended reading.

Community
  • 1
  • 1
alexn
  • 57,867
  • 14
  • 111
  • 145
  • In your second example, If you're putting timerType as a reference you can just write setTimer(ref timerType); instead of timerType = setTimer(ref timerType); – Blachshma Oct 28 '12 at 13:46
  • strings are not passed by value in c# – thumbmunkeys Oct 28 '12 at 13:50
  • I know saying that is a bit off, but it's probably the easiest to understand for beginners. Hence my link to the other thread. – alexn Oct 28 '12 at 13:57
  • @pivotnig strings aren't passed *at all* - nor is any other reference-type; instead a reference-to-a-string is passed, and is passed by-value. However, in common parlance it would be correct to say the string is passed by value – Marc Gravell Oct 28 '12 at 14:03
  • Oh wow!! THANKYOU! I was about to go to bed defeated. Very much appreciated, and very fast response. – user1767102 Oct 28 '12 at 14:09