How to instantiate an object of this class in C#

Asked

Viewed 1,412 times

2

I’m reading Caelum’s booklet about C# and object orientation, so I put it in Account.Cs the following code:

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;
        }
    }
}

Has the class Bill with the method Draw. It says that to instantiate an object you have 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 don’t know where to put this "button".

  • 1

    Your object of type Conta is already instantiated, could be more clear in your doubt ? because from what I understand, you have this button code and do not know where to put correct ?

  • Don’t worry about class Conta for this. She’s just your model, she has no relation to the presentation you’re trying to make. Of course, you use it in the presentation but it itself is not part of the presentation. Did you copy this in the project or have VS generate the Form code for you and just add this snippet? Does the handout show all the code, or did you just give it that snippet? It seems to me that you have to create the whole Form (the VS generates the code for you) and then fit this part.

  • I know that the class is the reference for me to instantiate the objects. That must be right, must be missing create the form. But there it was like this, it didn’t show how to create the form.

  • That @Enzotiezzi I instanciei, but I do not know where to put.

  • first you need to identify if your project, is a Windows Form, if so you need to put a button on your screen, click on that button 2 times and include that code that is in the method button1_Click in the code it automatically generates, which will be similar to the one you have.

  • The Button will be placed in the Graphical Interface. https://lh3.googleusercontent.com/-MRRwz5qyaE8/Tn0EKfJ8E9I/AAAAAAAABAY/16jDCAKW4cs/DynamicButtonsInWinforms.gif

  • Visual Studio has a keyboard shortcut to indent code automatically: CTRL + k + d ;)

  • Thank you guys, I’ve identified everything here. Thank you very much.

Show 3 more comments

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, fuck there until you find;

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

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

6-) ???

7-) Profit!

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

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

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

It is important later to understand also how the association of the click event is made to the method. So fuck it up a lot, okay? One of the main features of good programmers is curiosity. Good luck!

  • 2

    The last paragraph is the most important.

  • I got it quick. I am aware that it is a basic question, but it is that I am coming from the web. The college is charging then I got embarrassed in the face to study. Thank you, it worked!

  • Thanks, I’ll look at everything.

Browser other questions tagged

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