How to instantiate an object of this class in C #

2

I'm reading Caelum's handout on C # and object orientation, and I put the following code in Conta.cs :

namespace Banco
{
    class Conta
    {
        public int numero;
        public string nome;
        public double saldo;

        public bool Sacar(double valor)
        {
            if (this.saldo >= valor)
            {
                this.saldo -= valor;
                return true;
            }
            return false;
        }
    }
}

You have the Account class with the Take method. It says that to instantiate an object has to do the following:

private void button1_Click(object sender, EventArgs e)
{
    Conta r = new Conta();
    r.numero = 1;
    r.nome = "Flano";
    r.saldo = 300;

    if (r.Sacar(250.0)) {
        MessageBox.Show("Operação realizada com sucesso!");
    } else {
        MessageBox.Show("Saldo insuficiente!");
    }
}

I understand how everything works, I just do not know where to put this button.

    
asked by anonymous 13.11.2014 / 15:48

1 answer

4

1-) Open Visual Studio;

2-) Create a Windows project;

3-) You should now see the drawing of a form. If you do not see, fuce there until you find;

4-) There should be a left flap with various controls. Expand the tab, find a Button control, and drag it onto the form. You can leave it in the position you want;

5-) Double-click this button that you placed on the form!

6-) ???

7-) Profit!

6-) This will open the code file of the form, already with a button click method ready for you to fill. Underneath the wipes, Visual Studio has also associated the click event of the button with this method.

Put your code that treats the account within this method generated by Visual Studio. Then just run the application and see how it works, debugging and everything else.

This whole methodology also works with a web project. But web projects are a bit more complex, so I recommend focusing on Windows applications and console applications to master the concepts you're learning for now.

It is important to further understand how the association of the click event is made to the method. So fucking enough, okay? One of the main characteristics of good programmers is curiosity. Good luck!

    
13.11.2014 / 16:07