Create a reference of a class from a variable

0

I do not know if I explained correctly in the title and I ask that you correct me.

I am doing a simple system of a bank in C # by object orientation following the apostille of caelum (Link of the handout) and also making some changes based on my knowledge. I would like to make a reference system, below the example of creating an account in the bank:

private void buttonCadatro_Click(object sender, EventArgs e)
{

   Conta c1 = new Conta();

   c1.numero = 1;
   c1.Saldo = 100;

}

And below the variable declarations of class conta.cs

public int numero;
public Double Saldo;

And question is this, I would like to do that every time it in the button register a new account is created, for example: c2 with number = 2, c3 with number = 3

I'd also like to make account 1 transfer $ 10 to account 2 without using both the c1 and c2 , only the use of . number

I hope you have understood, and as I said before, correct my mistakes.

    
asked by anonymous 22.12.2017 / 18:13

1 answer

1

It will be difficult for you to handle transactions without having a database. But let's say you've already loaded your bank:

You can add a List to your main:

public List<Conta> contas = new List<Conta>();

public class Conta
{
    public int numero;
    public Double Saldo;
}    

private void buttonCadatro_Click(object sender, EventArgs e)
{
    contas.add(new Conta()
            {
               numero = 1; 
               Saldo = 100;
            }); 
}

You can create a transfer event:

private void buttonTransfere(object sender, EventArgs e)
{
    Transfere(1,2,300); //transfere 300 reais da conta 1 para a conta 2
}

private bool Transfere(int idContaOrigem, int idContaDestino, double valor){
    if(contas.ElementAtOrDefault(idContaOrigem) != null && contas.ElementAtOrDefault(idContaDestino) != null){
        if(contas[idContaOrigem].Saldo >= valor){
            contas[idContaOrigem].Saldo -=  valor ;
            contas[idContaDestino].Saldo +=  valor ;
            return true;
        }

    }
    return false;
}

Remembering that is not the most correct way to do it, I have already taken this course and in the future will have a better explanation about it, but it solves your problem. Anyway I'll edit my answer later with a better solution.

    
22.12.2017 / 18:35