//Hi all just switching from c++ to c#.Can anyone  help,with the question i have:
//this is a peace of code from c++ banking aplication
int ShowMenu(void);

void AddAccount(vector<CAccount*>& list);
void DisplayAccount (vector<CAccount*>& list);
void Lodgement (vector<CAccount*>& list);
void Withdroawal (vector<CAccount*>& list);
void InterestRate (vector<CAccount*>& list);

CAccount* findAccount (vector<CAccount*>string );

bool quit = false;

int _tmain(int argc, _TCHAR* argv[])
{
    vector<CAccount*> list;

    int option = 0;

    do{
        quit =false;
        option =ShowMenu();
        switch ( option )
        {
        case 1:
            AddAccount(list);
            break;
        case 2:
            DisplayAccount(list);
            break;
        case 3:
            Lodgement(list);
            break;
        case 4:
            Withdroawal(list);
            break;
        case 5:
            InterestRate(list);
            break;
        case 6:
            quit = true;
            break;
        default:
            cout << "Invalid Entry, Please Try Again";
        }
    }while(!quit);


        return 0;
}
//How to change c++ vector to c# and what #include should be?Thank you.

Recommended Answers

A List<T> in c# is the same as a vector<T> in C++.
cout would become Console.Write or WriteLine in C#.

Jump to Post

If you have a CAccount class, you don't need a pointer to that class just use List<CAcount>. In C# you seldom use pointers. The compiler does it for you. You still can use pointers but then, you have to mark the code with the unsafe keyword.

Jump to Post

In C# you can think of all classes as references (pointers)

Like ddanbe said above, if you really want the pointer, you can use the unsafe keyword and then use it like C++. A more safe way is to marshall it out into an IntPtr object, but this is generally …

Jump to Post

Perhaps you could specify everyting by function(method in C# talk) at the time. We (at least I) don't see all the things going on, please explain them.
Your AddAccount function seems rather easy this would be something like list.Add(AnAcount);

Jump to Post

On line 207 of your previous post you have List<Account> list = new List<Account>();
If CurrentAcount is another class you have to make a new List unless CurrentAcount is of type Account and you overloaded the consturtor in the Account class. Have to say goodnight now, see you in the …

Jump to Post

All 42 Replies

A List<T> in c# is the same as a vector<T> in C++.
cout would become Console.Write or WriteLine in C#.

I am also stuck on this line of code vector<CAccount*> list; How to make vector List<T> as you mentioned to be able to store pointer to base class?

If you have a CAccount class, you don't need a pointer to that class just use List<CAcount>. In C# you seldom use pointers. The compiler does it for you. You still can use pointers but then, you have to mark the code with the unsafe keyword.

In C# you can think of all classes as references (pointers)

Like ddanbe said above, if you really want the pointer, you can use the unsafe keyword and then use it like C++. A more safe way is to marshall it out into an IntPtr object, but this is generally reserved for interop (Win32 API) code.

Given that your code looks to be self-contained you don't need the actual pointer reference.

Quick overview:

Nearly all objects are of two basic types; value and reference. A reference type can be treated almost identically to the pointer type in C++ and value types are the same as normal definitions and primitives (such as int, float, long CMyClass)
A quick easy rule as to whether something is a value type or reference type;
structs are value types and classes are reference types.

You can also say that anything that inherits Object is also a reference type but there are special rules around this too (such as boxing and unboxing).

To make it relevant to your code:

List<CAccount> list = new List<CAccount>(); in C# is the equivalent of
std::vector<CAccount*> list; in C++

commented: Could not have explained it better. +15
//How do i transfer this code in to c#:
#include "stdafx.h"
#include"Account.h"
#include"DepositAccount.h"
#include"CurrentAccount.h"
#include"CSSIAAccount.h"

using namespace std;

int ShowMenu(void);

void AddAccount(vector<CAccount*>& list);
void DisplayAccount (vector<CAccount*>& list);
void Lodgement (vector<CAccount*>& list);
void Withdroawal (vector<CAccount*>& list);
void InterestRate (vector<CAccount*>& list);

CAccount* findAccount (vector<CAccount*>string );

bool quit = false;

int _tmain(int argc, _TCHAR* argv[])
{
    vector<CAccount*> list;

    int option = 0;

    do{
        quit =false;
        option =ShowMenu();
        switch ( option )
        {
        case 1:
            AddAccount(list);
            break;
        case 2:
            DisplayAccount(list);
            break;
        case 3:
            Lodgement(list);
            break;
        case 4:
            Withdroawal(list);
            break;
        case 5:
            InterestRate(list);
            break;
        case 6:
            quit = true;
            break;
        default:
            cout << "Invalid Entry, Please Try Again";
        }
    }while(!quit);


        return 0;
}


int ShowMenu(void)
{
    cout << " Welcome to account App" << endl;

    int option;
    cout << "\t\t1. Add New Account" << endl;
    cout << "\t\t2. Show Account list" << endl;
    cout << "\t\t3. Do Lodge" << endl;
    cout << "\t\t4. Do Withdrawal" << endl;
    cout << "\t\t5. Set Interest Rate" << endl;
    cout << "\t\t6. Exit" << endl;
    cout << "\n";
    cout << "\t\tEnter Option Here: ";

    cin >> option;
    return option;


}




void AddAccount(vector<CAccount*>& list)
{
    CAccount* ptr;

    ptr = new CCurrentAccount ("ca1234", 100, 500);
    list.push_back(ptr);

    ptr = new CDepositAccount ("da1234", 500, 10);
    list.push_back(ptr);

    ptr = new CSSIAAccount ("sa1234", 100, 20);
    list.push_back(ptr);

}

void DisplayAccount (vector<CAccount*>& list)
{
    cout <<list.size() << endl;
    CAccount* ptr;

    for(int i = 0; i < list.size(); i ++)
    {
        ptr = list[i];
     ptr-> ShowDetails();

    }
}
CAccount* findAccount(vector<CAccount*>list, string idIn){
{
    for (int i = 0 ; i < list.size(); i ++)
    { 
        if (list[i]->GetID() == idIn)
    return list[i];
    }
}
return NULL;
}

void Lodgement (vector<CAccount*>& list)
{ 

    string AccId;
    CAccount* p =NULL;
    float lodgementAmount =0;
    cout << "Process Lodgement" << endl;
    cout<< "Enter account ID:" << endl;
    cin >> AccId;
    cout << "Enter lodgement account =" << endl;
    cin >> lodgementAmount;

    ///call method to return pointer on vector on vector that matches id
    p = findAccount(list,AccId);
    if ( p == NULL)
    {
     cout << "Error account can not be found" << endl;
    }
    else
    {
        p->Lodge(lodgementAmount);
        cout << "new balance details "<< endl;
        p->ShowDetails();
    }

}
void Withdroawal (vector<CAccount*>& list)
{
    string AccId;
    CAccount* p =NULL;
    float withDrawalAmount =0;
    cout << "Process Withdraw" << endl;
    cout<< "Enter account ID:" << endl;
    cin >> AccId;
    cout << "Enter withdrawal account =" << endl;
    cin >> withDrawalAmount;

    ///call method to return pointer on vector on vector that matches id
    p= findAccount(list,AccId);
    if ( p == NULL)
    {
     cout << "Error account can not be found" << endl;
    }
    else
    {
        p->Withdraw(withDrawalAmount);
        cout << "new balance details "<< endl;
        p->ShowDetails();

     }
}
void InterestRate (vector<CAccount*>& list)
{
    for(int i =0;i <list.size();i++)
    {
        CDepositAccount *devptr = dynamic_cast<CDepositAccount*>(list[i]);
        if(devptr != 0)
        {

        cout << "interest rate" << endl;
        devptr->AddQtrInterest();
        }
    }


}
///I have so far this ,what i managed :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UseInheritance
{
    class Program
    {






        static void Main(string[] args)
        {
            List<Account> list = new List<Account>();

             bool quit = false;

             SavingsAccount sa = new SavingsAccount("Freda Smith", 100.00, 25);
             sa.Display();

             //sa.Credit(100);
             //sa.Debit(180);
             //sa.ChangeName("Freda Jones");
             //sa.Display();
             //Console.WriteLine("Bank charge: ${0}",
             //sa.CalculateBankCharge());
             CurrentAccount ca = new CurrentAccount("ca1234", 200, 20);
             list.Add(ca);
             ca.Display();
             DepositAccount da = new DepositAccount("da2345", 500, 10);
             list.Add(da);
             da.Display();
             CSSIAAccount cssia = new CSSIAAccount("sa0023", 100, 20);
             list.Add(cssia);
             cssia.Display();

           int option = 0;

    do{
        quit =false;
          option = ShowMenu();
        switch ( option )
        {
        case 1:
                AddAccount(list);
            break;
        case 2:
            DisplayAccount(list);
            break;
        case 3:
            Lodgement(list);
            break;
        case 4:
            Withdroawal(list);
            break;
        case 5:
            InterestRate(list);
            break;
        case 6:
            quit = true;
            break;
        default:
            Console.WriteLine  ("Invalid Entry, Please Try Again");
                break;
        }
    }while(!quit);




}
     public static  int ShowMenu()
{
    Console.WriteLine(" Welcome to account App");

    int option;
    Console.WriteLine("\t\t1. Add New Account");
    Console.WriteLine("\t\t2. Show Account list");
    Console.WriteLine("\t\t3. Do Lodge");
    Console.WriteLine("\t\t4. Do Withdrawal");
    Console.WriteLine("\t\t5. Set Interest Rate");
    Console.WriteLine("\t\t6. Exit");
    Console.WriteLine("\n");
    Console.WriteLine("\t\tEnter Option Here: ");

    option = (int)Console.Read();
    return option;


}
        public static void AddAccount(List<Account> list)
        {







        }


        public static void DisplayAccount(List<Account> list)
        {

        }



        public static void Lodgement(List<Account> list)
        { }
        public static void Withdroawal(List<Account> list)
        { }
        public static void InterestRate(List<Account> list)
        { }




        }
    }

//// the hard coded output works,but how do i fill in functions,like add Account //and those with the pointers  convert to c#?

Perhaps you could specify everyting by function(method in C# talk) at the time. We (at least I) don't see all the things going on, please explain them.
Your AddAccount function seems rather easy this would be something like list.Add(AnAcount);

// function adAccount
 public static void AddAccount(List<Account> list)
        {

            CurrentAccount ca = new CurrentAccount("ca1234", 200, 20);
            list.Add(ca);

            DepositAccount da = new DepositAccount("da2345", 500, 10);
            list.Add(da);
            CSSIAAccount cssia = new CSSIAAccount("sa0023", 100, 20);
            list.Add(cssia);

        }

///function display next,do i need to declare assignement again 
  CurrentAccount ca = new CurrentAccount("ca1234", 200, 20);
  ca.Display();
  //this Display in base class:
   public virtual void Display()
        {
            Console.WriteLine("Name={0}, balance={1}",
            this.name, this.balance);
        }
        //next functions like:
         public static void Lodgement(List<Account> list)
        { }
        public static void Withdroawal(List<Account> list)
        { }
        public static void InterestRate(List<Account> list)
        { 


        }
        //No idea how to do,as in c# no pointers,classes works as a references ,but,i never came across with a syntax,like c++
        //this is my first convertion from c++ to c#,that's why asking too many questions
        //Whant to see how it works in c#, because syntax a bit different.Regards

On line 207 of your previous post you have List<Account> list = new List<Account>();
If CurrentAcount is another class you have to make a new List unless CurrentAcount is of type Account and you overloaded the consturtor in the Account class. Have to say goodnight now, see you in the morning. :)

Could you post the code of your account class please?

//Here is account class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UseInheritance
{
    public abstract class Account
    {
        private string name;
        protected double balance;

        public string Name
        {  get
              {

            return name;
              }
            set
            {
                name = value;

            }

        }
        public double Balance
        {
            get {
                return balance;
                }
            set 
                {
                    balance = value;

                }


        }
        public Account(string nm, double bal)
        {
            this.name = nm;
            this.balance = bal;
        }

        public virtual void Credit(double amount)
        {
            this.balance += amount;
        }
        public virtual void Debit(double amount)
        {
            this.balance -= amount;
        }
        public virtual void Display()
        {
            Console.WriteLine("Name={0}, balance={1}",
            this.name, this.balance);
        }
        public void ChangeName(string newName)
        {
            this.name = newName;
        }
        //public virtual double CalculateBankCharge()
        //{


        //}

    }
}

Let's concentrate on AddAcount for the moment.
You are creating 3 new classes there and add them to the Account list. In C# this won't work unless you use an ArrayList, but I would not recommend that.
Couldyou not turn these classes into an Account class also?

//Can you give me an example,please.To see how it works. And even to display all them,looks like need some kind of ref,but need to know how does it work in c#
/////This what i tried to do for addAccount:
 SavingsAccount sa = new SavingsAccount("Freda Smith", 100.00, 25);



            CurrentAccount ca = new CurrentAccount("ca1234", 200, 20);
            list.Add(ca);

            DepositAccount da = new DepositAccount("da2345", 500, 10);
            list.Add(da);

            CSSIAAccount cssia = new CSSIAAccount("sa0023", 100, 20);
            list.Add(cssia);

// but if try to do display function,like :

        public static void DisplayAccount(List<Account> list)
        {
            foreach (Account value in list)
            {
                Console.WriteLine("value");

            }

        }
//it doesn't work

C# is very picky about types, in fact as it should be. One of the reasons I like the language so much. IMO you should have only one account class. I'll come back with a listing of what I mean. Meanwhile, you could read this.

OK if you consider it solved. But here is my first version of an account class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Banking
{
    enum AccountKind
    {
        CSSIAAccount, Deposit, whatever 
    }

    class Account
    {
        public string name { get; set; }       
        public double balance { get; set; }
        public AccountKind Kind { get; set; }

        public Account(string name, double balance, AccountKind Kind)
        {
            this.name = name;
            this.balance = balance;
            this.Kind = Kind;
        }

        public virtual void Credit(double amount)
        {
            this.balance += amount;
        }

        public virtual void Debit(double amount)
        {
            this.balance -= amount;
        }

        public override string ToString()
        {
             return String.Format("Name = {0}, Balance = {1}",this.name, this.balance);
        }
    }
}

//Just somehow pressed solved,sorry

the Display stays the same?
 public virtual void Display()
        {
            Console.WriteLine("Name={0}, balance={1}",
            this.name, this.balance);
        }
//This is my deposit acc,is it correct?
 class DepositAccount:Account
    {
        protected double IntRate;
        public double intRate
        {
            get 
            {
                return IntRate;

            }
            set 
            {
                IntRate = value;

            }

        }

        public override void Display()
        {
            Console.WriteLine("Name={0}, balance={1}, balance={2}",
                 this.name, this.balance, this.IntRate);



        }
        public DepositAccount(string nm, double bal, double IRate)
            : base(nm, bal)
        {
           IntRate = IRate;

        }

        public void AddQtrInterest()
        {
            balance += balance * IntRate / 100 / 4;

        }
        public override void Debit(double amount)
        {
            if (amount <= (balance ))
            {
                balance -= amount;
                base.Debit(amount);

            }
            else
                Console.WriteLine("Not allowed as amount exeeds balance\n");


        }

    }
}

You don't need the Display method (that is soo C++ I guess)
Because depositAccount is an Account you can use the ToString method of Account. You could remove my enum as you apparantly are going to derive other accounts from Account.

//Tahnks.Why when i try to do thid in depostAccount
class DepositAccount : Account
{

}


//it complains now for Account ,i change it to public abstract(the Account)to be it base
//In your example variables declared public,should i redefine the as protected?

Should not worry too much about abstract and protected.
Concentrate on the translation until you got a working project, then start the refinements.

Is it possible somehow to send you whole app,to check,what i got so far?

Could you send it in zip format?
Perhaps also include the C++ listing.

yes,i can,just need destination

//Here 2 copies,one from c++ to be converted to c#

can you see files,i uploaded 2,but can't see them

I see nothing. Did you clicked on the paperclip in thearea above the post?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.