StackOverflowException in C # by visual studio code Linux

0

I'm trying to learn a bit of C #, and the microsoft site encourages me to use vscode to program in linux, however I'm having a StackOverflowException problem and I can not understand the reason (the code does not run any visually infinite loops) .

Follow the main code:

static void Main(string[] args) {
        UserInfo info = new UserInfo("user","password");
        Console.WriteLine(info.user);
}

And the code for the UserInfo class

public class UserInfo {
    public string user {
        get {
            return this.user;
        }

        set {
            this.user = user;
        }
    }

    public string password {
        get {
            return this.cryptPassword(this.password);
        }

        set {
            this.password = password;
        }
    }

    public UserInfo(string user, string password) {
        this.user = user;
        this.password = password;
    }

    private string cryptPassword(string password){
        string[] cryptArray = password.Split("");
        string[] reverseArray = new string[cryptArray.Length];

        for(int index = 0; index < cryptArray.Length; index++){
            reverseArray[index] = cryptArray[cryptArray.Length - index];
        } 

        string crypt = string.Join("",cryptArray);

        return $"HASH{crypt}000";
    }
}

And all the files are within the same namespace.

So what am I doing wrong?

    
asked by anonymous 27.02.2018 / 13:30

2 answers

3

Your problem is here:

public string user {
    get {
        return this.user;
    }

    set {
        this.user = user;
    }
}

You are referencing the property name and not the private variable name, which results in an infinite loop at the time of the get.

You should create the private variable for password and user like this (I removed the cryptPassword method just to simplify the explanation):

public static void Main()
{
    UserInfo info = new UserInfo("user","password");
    Console.WriteLine(info.User);
    Console.WriteLine(info.Password);
}

public class UserInfo {

private string _user;
private string _password;   

public string User {
    get {
        return _user;
    }

    set {
        _user = value;
    }
}

public string Password {
    get {
        return _password;
    }

    set {
        _password = value;
    }
}

public UserInfo(string user, string password) {
    this.User = user;
    this.Password = password;
}   
    
27.02.2018 / 14:01
4

Notice that all your properties your SET accessor recursively call itself, so that's your problem. If you want to encapsulate in this way, you need a field private to hold these values.

private string user;
public string User {
    get {
        return this.user;
    }
    set {
        this.user = value;
    }
}

If you are in the above version of C # 3.0, you can use the auto-property:

public string User { get; set; } //Obedecendo o Pascal Case

At compile time a private field will be made to do this encapsulation as well as its accessor methods, it would do something like Java works with Get and Set methods:

public string get_User {...}
public void set_User (string value) {...}
    
27.02.2018 / 13:42