- Home /
Why does my Switch statement only go through one case correctly?
I have a problem with a switch statement. This switch statement would occur when the app fails to register a player via an email (email, username and password). When I tested it, I used a username and email address that I know had been taken, but only the "EmailAddressNotAvailable" error seems to run and it says "username available" even though I know its not. When I change the email to a new email address it then says "username not available".
I wanted both errors to occur at the same time but it only seems to detect the email address error. Seems not to go through all the cases and not in order either as I place the UsernameError first in the switch.
I am something is wrong with my switch case statements. Here is my code.
private void OnRegisterFailure(PlayFabError error)
{
Debug.LogError(error.GenerateErrorReport());
switch (error.Error)
{
case PlayFabErrorCode.UsernameNotAvailable:
RegisterUsernameErrorText.text = ("Username Taken");
Debug.LogWarning("Register Fail Username Taken");
break;
case PlayFabErrorCode.EmailAddressNotAvailable:
RegisterEmailErrorText.text = ("Email Already Registered");
Debug.LogWarning("Register Fail Email Already Registered");
break;
case PlayFabErrorCode.InvalidEmailAddress:
RegisterEmailErrorText.text = ("Invalid Email");
Debug.LogWarning("Register Fail Invalid Email");
break;
default:
RegisterUsernameErrorText.text = ("Username Available");
RegisterEmailErrorText.text = ("Email Available");
Debug.LogWarning("Register default");
break;
}
}
Thanks for any help.
Answer by rh_galaxy · Oct 18, 2020 at 02:12 AM
In a switch statement only one case will be hit (then depending on if you end with a break or not, more cases can run). Maybe error.Error can be a combination of errors? In that case you must use if-statements.
if((error.Error & PlayFabErrorCode.UsernameNotAvailable) == PlayFabErrorCode.UsernameNotAvailable) { ... }
Thanks for your answer. I Thought it would go through each case if they were separated with a break but I guess not. I will try out your suggestions to use if statement.
I have tried the following code but have the same issue of it only detecting the email error first. I am unsure, but it may be that the playfab checks if the email is available and if it is not it produces that error and does not bother to check if the username is available. Once the email has been corrected, then maybe playfab checks if the username is available and finds it is not and generates an error.
private void OnRegisterFailure(PlayFabError error) { Debug.LogError(error.GenerateErrorReport());
if ((error.Error & PlayFabErrorCode.UsernameNotAvailable) == PlayFabErrorCode.UsernameNotAvailable)
{
RegisterUsernameErrorText.text = ("Username Taken");
Debug.LogWarning("Register Fail Username Taken");
}
else
{
RegisterUsernameErrorText.text = ("Username Available");
}
if ((error.Error & PlayFabErrorCode.EmailAddressNotAvailable) == PlayFabErrorCode.EmailAddressNotAvailable)
{
RegisterEmailErrorText.text = ("Email Already Registered");
Debug.LogWarning("Register Fail Email Already Registered");
}
else
{
RegisterEmailErrorText.text = ("Email Available");
}
}
Your answer
Follow this Question
Related Questions
Why would my switch's case be correct but the output is not? 2 Answers
Problem with my weapons switch statement C# 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers