I'm moving from PHP (procedural) to C# and I've a number of errors in the following block - what am I doing wrong?
Objective is to pass in username and password values, then to trim, validate and return a list of errors (I'm using the dictionary to build an array of errors).
- "The name 'varUserName' does not exist in the current context".
"else" = "Type or namespace definition, or end of file expected".
public class Login (string InUserName, string InUserPass) { string varUserName; string varUserPass; // Dictionary object is c# equivalent of PHP's 'array["key"] = "value"' Dictionary<string, string> errMsg = new Dictionary<string, string>(); varUserName = "123qwe"; varUserName = varUserName.Trim(); if ((varUserPass == "") && (varUserName == "")) { errMsg.Add("Username", "Username cannot be blank"); errMsg.Add("Password", "Username cannot be blank"); } else { if (varUserName == "") { errMsg.Add("Username", "Username cannot be blank"); } if (varUserPass == "") { errMsg.Add("Password", "Password cannot be blank"); } } }
Thanks Newbie Matt
The problem is, that you do everything in your class. You can't do logic in your class. Put your code in a Method. Like so:
You can then call it like that in your code: