Create a class reference from a variable

Asked

Viewed 63 times

0

I don’t know if I explained it correctly in the title and I ask you to correct me.

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

private void buttonCadatro_Click(object sender, EventArgs e)
{

   Conta c1 = new Conta();

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

}

And below the class variable statements Cs account.

public int numero;
public Double Saldo;

And the thing is, I’d like to do that every time he register button a new account is created, for example: C2 with number = 2, C3 with number = 3. That is if I speak of the account with the . number = 3, he already understands that is the reference C3;

I would also like to have account 1 transfer $ 10 to account 2 without using the C1 and C2, only of the use of .number

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

  • which database was used?

  • For now I am not using database, just I am training even. When close the program will be erased memory.

  • Suggested property name Balance for _balance when private and Balance when access modifier, example: public double Balance{ get; set}

  • in case of the name, you need to create a way to view the data and then a read, then add a function in creating the name that would be C + Count(+1)

1 answer

1


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

You can add in your main a List:

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 download 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 it is not the most correct way to do, I have taken this course and more in the future will have a better explanation about, but already solves your problem. Anyway I will edit my reply later with a better solution.

  • I managed to solve most of the problem. I will open a new topic to suit more with my situation.

Browser other questions tagged

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