【问题标题】:Multiple foreach passes to different class lists多个 foreach 传递到不同的类列表
【发布时间】:2017-12-09 13:37:44
【问题描述】:

我正在 C# WPF 中处理“银行应用程序”,但出现错误:CS0052

可访问性不一致:字段类型“列表”较少 比字段“Customer.CustomerAccountsList”更容易访问

想法是bool DoseThisAccountExistAlready()方法应该对所有客户一一检查,看看他/她的账户是否与即将创建的账户有相同的数据。

但是CustomerAccountsList 必须是公开的,否则DoseThisAccountExistAlready() 无法访问帐户数据。

如何解决这个错误?

//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;  

        }
    }
}

【问题讨论】:

  • 拼写很多?这个引起了我的注意,因为您的标题中有驴子……而且我认为您不是在谈论动物????
  • 在您的 DoseThisAccountExistAlready 中,只要找到任何具有该 kontotyp 的客户,您就会返回 true。我想您首先要检查您是否在正确的客户处并忽略其他任何客户(顺便说一句,它是“DoesThis...”)

标签: c# wpf


【解决方案1】:

您不能拥有public List&lt;Account&gt;,因为Account 类没有访问修饰符,因此编译器默认为internal。您不能拥有internal 类型的public 属性。您需要将其设为 public 才能使其正常工作。

【讨论】:

  • ...或将该属性标记为内部
猜你喜欢
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
相关资源
最近更新 更多