Stackoverflowexception in c# by Linux visual studio code

Asked

Viewed 99 times

0

I’m trying to learn a little c#, and the microsoft website encourages me to use vscode for programming on linux, but I’m having a problem with Stackoverflowexception and I can’t understand why (the code does not run any loop visually infinite).

Follow the main code:

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

And the code of 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?

  • 1

    https://answall.com/q/101691/101

2 answers

4

Note that all your properties your SET accessor recursively calls itself, that’s your problem. If you want to encapsulate this way, you need a private field to hold these values.

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

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

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

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

public string get_User {...}
public void set_User (string value) {...}
  • To a getter non-standard, such as the password, I can continue declaring the setter implicitly states only the implementation of the getter?

  • 1

    Unfortunately you cannot implement one without implementing the other as well. Soon the auto-Property will not work and you need to specify a private field like I did in the first example.

  • thanks for the answer :), I had looked that auto-Property but I think I looked right over, reminds me a lot of ruby accessors

3


Your problem is here:

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

    set {
        this.user = user;
    }
}

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

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;
}   
  • functioned here, thank you. This semantics of get and set reminded me a lot of Kotlin / scala, I will pay more attention to it now :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.