Class with method to change a parameter

Asked

Viewed 730 times

1

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

I would like to use a constructor method to change the phone parameter. That is, to change the data (phone number) that the person entered in the textbox.

All 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 that I received only accepts the use of object-oriented C# language (I’m starting to learn about and it’s to then learn about the whole SQL part and everything, now it’s just the basics without getting into ADO.NET or ASP.NET).

2 answers

2

In C# what you call an attribute is actually called a field (muddle). In C# do not use methods to encapsulate the fields, properties are used, who are actually disguised methods that access an implicit private field.

Of course, I’m not going to do a complete class with validation, and other components, but basically there should be a constructor, as the statement asks 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 as it may 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;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • And if I want to put a Button to change all Textbox information at the same time (in this case, it will only change what the user selects with the cursor in the textbox to change what is written), how do I do? I have to call, for example, Alterarnome() and Alterarendereco() there on Form1.Cs? I don’t know if I understand what I’m asking for,

  • @Beginner That’s another golden subject question, if this has already been answered you can accept what was most useful to you. Later you can vote on everything you find useful on the site. If you do not know how to accept see the [tour].

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);
  • And if I want to put a Button to change all Textbox information at the same time, how do I do? I have to call these Alterarnome(), Alterarendereco() there at Form1.Cs?

Browser other questions tagged

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