There is no variable c#

Asked

Viewed 187 times

3

This is C#’s mistake, or I’m doing something wrong because in my logic it should work perfectly.

public partial class Form1 : Form
{

    Banco Bbanco;
    Bbanco = new Banco();

}

1 answer

6


Cannot make assignments in the body of a class separate from the declaration.

This doesn’t work:

public class Test
{
    Banco BBanco;
    BBanco = new Banco();       
}

It may, however, make the allocation at the time of the declaration:

public class Test2
{
    Banco BBanco = new Banco();
}   

Or you can assign within a method/constructor:

public class Test3
{
    Banco BBanco;

    public Test3()
    {
        BBanco = new Banco();
    }
}

You can consult the examples here.

  • or within the manufacturer :)

  • 1

    @Marconciliosouza I edited to avoid doubt, but a constructor can be considered a method

  • That cool, so there is the possibility to do assignments outside of a class that super interesting, thank you very much.

  • @rock.ownar, in reality it is outside a method or builder. outside of a class you would be inside the namespace. that also would not be possible.

Browser other questions tagged

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