Class with method to change a parameter

1

I have a User class with private attributes like Nome , Endereco and Telefone . This class has a constructor method that receives parameters like nome , endereco and telefone .

I'd like to use a constructor method to change the phone parameter. That is, to change the data (phone number) that the person inserted into the textbox.

All of this is in a Windows Forms Application in C #.

I would like to know how to change this data and taking into account that the proposed exercise I received only accepts the use of object-oriented C # language (I'm starting to learn about and is to later learn about all of SQL and everything else, now it's just the basics without getting into ADO.NET or ASP.NET).

    
asked by anonymous 28.02.2017 / 03:08

2 answers

1
public class Usuario
{
    public string Nome {get; private set;}
    public string Endereco {get; private set;}
    public string Telefone {get; private set;}

    public Usuario()
    {

    }

    public Usuario(string nome, string endereco, string telefone)
    {
        Nome = nome;
        Endereco = endereco;
        Telefone = telefone;
    }

    public void AlterarNome(string novoNome)    
    {
        Nome = novoNome;
    }

    public void AlterarEndereco(string novoEndereco)
    {
        Endereco = novoEndereco;
    }

    public void AlterarTelefone(string novoTelefone)
    {
        Telefone = novoTelefone;
    }
}

//win form
var usuario = new Usuario();
usuario.AlterarNome(this.txtNome.Text);
    
28.02.2017 / 03:35
2

In C # what you call the attribute is actually called the field ( confusion of terms ). In C # no methods to encapsulate fields, uses properties , which are actually disguised methods that access a field private implicit .

Of course I will not do a full class with validation, and other components, but basically must have a constructor, as the statement asks for and the properties. Nothing else is needed for the basics.

The use in Winforms depends on what you are doing, as there is no example in the question, I commented how can be a possibility.

public class Program {
    public static void Main() {
        var usuario = new Usuario("João", "Rua da avenida, 123", "1234-5678");
        usuario.Telefone = "9876-5432";
        //No Winforms seria algo como
        //usuario.Telefone = Formulario.Telefone.Text
    }
}

public class Usuario {
    public string Nome {get; set;}
    public string Endereco {get; set;}
    public string Telefone {get; set;}

    public Usuario(string nome, string endereco, string telefone) {
        Nome = nome;
        Endereco = endereco;
        Telefone = telefone;
    }
}

See working on .NET Fiddle . And at Coding Ground . Also I put it on GitHub for future reference .

    
28.02.2017 / 13:02