Jakub Inglot
Jakub Inglot

Reputation: 11

Multiple foreach passes to different class lists

I'm working on a "bank application" in C# WPF where I came upon error:CS0052

Inconsistent accessibility: field type 'List' is less accessible than field 'Customer.CustomerAccountsList'

The idea is that the method bool DoseThisAccountExistAlready() should check all customers one by one and see if his/her accounts have the same data as the account that is about to be created.

However CustomerAccountsList must be public or DoseThisAccountExistAlready() can't access the accounts data.

How do I fix this error?

//main window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Uppgift2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public List<Customer> ListOfAllCustomers = new List<Customer>();

        public bool Login(string fornamn, string efternamn, string pin)
        {
            foreach (Customer c in ListOfAllCustomers)
            {
                if (c.firstname == fornamn && c.lastname == efternamn && c.pinKod == pin)
                {
                    MessageBox.Show("Login succesfull. Welcome " + c.firstname + " " + c.lastname);
                    WindowAccountOperations LoggedInUser = new WindowAccountOperations();
                    LoggedInUser.ShowDialog();
                    return true;
                }
                else
                {
                    MessageBox.Show("Det finns inte ett sånt användare. Kontrolera användarnamn och pin");
                    return false;
                }
            }
            return false;
        }

        //Customer pin could be remade into a rondom generator with an if() that forces it to be a four digit number 
       /* 
        *   Random randomPin = new Random();
            randomPin.Next(x, y);

            if(randomPin.ToString().Length!=4)
             {

             }
      */



        public bool CreateNewCustomer(string fornamn, string efternamn, string telefon, string customerAdress, string customerPin)
        { 
            //Is there already such customer?
            foreach (Customer c in ListOfAllCustomers)
            {
                if ((c.firstname == fornamn && c.lastname == efternamn && c.adress == customerAdress))
                {
                    MessageBox.Show("Kunden med ett sånt förnamn, efternamn och adress redan finns");
                    return false;
                }

                if(c.pinKod == customerPin)
                {
                    MessageBox.Show("Kunden med ett sånt pinkod redan finns");
                    return false;
                }
                else
                {
                    ListOfAllCustomers.Add(new Customer(TBFirstNameNyKund.Text, TBSecondNameLogin.Text, TBTelNyKund.Text, TBAdressNyKund.Text, TBPinNyKund.Text));
                    MessageBox.Show("Registration complete. Du kan nu logga in");

                    TBFirstNameNyKund.Clear(); TBSecondNameLogin.Clear(); TBAdressNyKund.Clear(); TBPinNyKund.Clear();//clear login textboxes
                    return true;
                }
            }
            return false;
        }

        public bool DoseThisAccountExistAlready(string kontoNummer, string kontotyp)
        {
            foreach (Customer c in ListOfAllCustomers )
            {
                foreach(Account a in c.CustomerAccountsList)
                {

                    if(a.accountType==kontotyp)//Does customer c try to create another type of account he already has?
                    {
                        return true;
                    }

                    if(a.accountNumber==kontoNummer)
                    {
                        return true;
                    }

                }
                return false;
            }

            return false;
        }

        private void ButtonLogin_Click(object sender, RoutedEventArgs e)
        {
            Login(TBFirstNameLogin.Text, TBSecondNameLogin.Text, TBPinLogin.Text);
        }

        private void ButtonSkapaKund_Click(object sender, RoutedEventArgs e)
        {
            CreateNewCustomer(TBFirstNameNyKund.Text, TBSecondNameNyKund.Text, TBTelNyKund.Text, TBAdressNyKund.Text, TBPinNyKund.Text);
        }
    }
}

//customer class

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

namespace Uppgift2
{
  public class Customer
  {
    public string adress;
    public string celphone;
    public string firstname;
    public string lastname;
    public string pinKod;

    public List<Account> CustomerAccountsList = new List<Account>();//Error apears here. 



    public Customer(string fname,string lname, string tel, string    customerAdress,string customerPin)
    {
        adress = customerAdress;
        celphone = tel;
        firstname = fname;
        lastname = lname;
        pinKod = customerPin;
    }

  }
}

//account base class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uppgift2
{
     abstract class Account   //The abstract modifier indicates that the thing being modified has a missing or incomplete implementation=base class for other classes
    {

        public double payoutAmmount;
        protected double payoutTax;
        public bool creditPosible;

        public string accountNumber;
        public string accountType;
        public double balance;
        protected double interest; //customer shouldent be able to modify interest % 


        //constructor
        public Account(string kontoNummer, double KontoStartsaldo,string kontoTyp, double ranta, bool finsdetkredit,double uttagPris)
        {
            accountNumber = kontoNummer;
            balance = KontoStartsaldo;
            accountType = kontoTyp;
            creditPosible = finsdetkredit;
            payoutTax = uttagPris;
        }

        public double InsertMoney(int chosenAccount, double payout)  //int chosenAccount becouse i thought to use a combo box index
        {
            payoutAmmount = payout;

            balance = balance - payout + (payout * payoutTax);
            return balance;  

        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

thisextendsthat
thisextendsthat

Reputation: 1355

You can't have a public List<Account> because the Account class has no access modifier so it is defaulted by the compiler to be internal. You can't have a public property of an internal type. You need to make it public for this to work.

Upvotes: 2

Related Questions