Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Account
- {
- public string UserName { get; private set; }
- public string Password { get; private set; }
- public Account(string userName, string password)
- {
- // check to see if the userName or password contain a space. If so, it is an error as they
- // are not allowed to have spaces.
- if ( ValidateNewUserName(userName) || ValidateNewPassword(password) ){
- throw new Exception("You are not allowed to have spaces in the user name or password.");
- }
- // You already passed the validation -- no need for the else statement since this will only be run
- // if no error is thrown :)
- this.UserName = userName;
- this.Password = password;
- }
- private bool ValidateNewPassword(string password){
- return password.Contains(" ");
- }
- private bool ValidateNewUserName(string userName){
- return userName.Contains(" ");
- }
- }
- // main method in some other class
- public static Main(){
- try{
- Account account = new Account ("someUserName", "somePassword");
- // instead of writing it out to the console, you would just open the
- // filestream and shove it in there.
- Console.Writeln("my user name : " + account.UserName);
- Console.Writeln("my password : " + account.Password);
- }
- catch(Exception e){
- Console.Writeln("An error occurred. The information is as follows : ");
- Console.Writeln("\tInnerException : " + e.InnerException);
- Console.Writeln("\tMessage : " + e.Message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement