Calling a method

0

Hello, I'd like to do something extremely simple, but I'm having trouble 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 display these two messages together in the Form, that is, in another location.

When I get to Form, I get questions about how to call them in the MessageBox.

    
asked by anonymous 19.06.2018 / 03:11

1 answer

2

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

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

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

Now in form , you could instantiate your students. Example:

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

Finally, you could display the message:

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

This last bit has to be inside some method or event of form , a click of a button 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");
}
    
19.06.2018 / 03:31