Calling a method

Asked

Viewed 36 times

0

Hello, I would like to do something extremely simple, but I’m having difficulty applying. I made the following class:

public class nome
    {

        string aluno = "Olá, eu sou um aluno";
        string aluna = "Olá, eu sou um outro aluno";

    }

From it, I would like a Messagebox to show these two messages together in the Form, ie in another location.

When I arrive at the Form, I get doubts of what to call them in the Messagebox.

1 answer

2

First point is that the two strings are not with access modifiers, soon are private and will only be accessible within the class itself.

Taking advantage and changing the code, because it makes no sense for a class called nome and two students inside it. The code should be like this:

public class Aluno
{
    public string Nome {get;set;}
}

Now on the form, you could instill your students. Example:

Aluno alunoA = new Aluno(){ Nome = "Aluno Exemplo A" };
Aluno alunoB = new Aluno(){ Nome = "Aluno Exemplo B" };

Finally, I could display the message:

MessageBox.Show(alunoA.nome,"Aluno");

This last section has to be within some method or event of the form, a button click for example:

private void button1_Click(object sender, EventArgs e)
{
    Aluno alunoA = new Aluno(){ Nome = "Aluno Exemplo A" };
    Aluno alunoB = new Aluno(){ Nome = "Aluno Exemplo B" };

    MessageBox.Show(alunoA.Nome+"\n"+ alunoB.Nome,"Alunos");
}
  • 1

    I liked this answer. The class should describe an entity, so Aluno, and entities have properties, so Nome.

  • 1

    Thank you very much, man. Your answer got me a lot of questions. Really worth!

  • @Rafaelventura if the question helped you, mark it as an answer. If you have questions, access the [Tour]

Browser other questions tagged

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