簡體   English   中英

如何將值從表單傳遞到 C# 中的接口類?

[英]How to pass values from a form to an interface class in C#?

我需要一些功課幫助,我有一堆實現信用卡表格的接口。 如何將用戶輸入表單(如文本框條目)的值傳遞給實現接口的類? 接口代碼如下

public interface IInfoCard
{
    string Name { get; set; }
    string Category { get; }
    string GetDataAsString();
    void DisplayData(Panel displayPanel);
    void CloseDisplay();
    bool EditData();
}
public interface IInfoCardFactory
{
    IInfoCard CreateNewInfoCard(string category);
    IInfoCard CreateInfoCard(string initialDetails);
    string[] CategoriesSupported { get; }
    string GetDescription(string category);
}

我創建了一個實現這些接口的新類。 該接口實現了我的表單,但講師向我們隱藏了該代碼。 下面是實現上述兩個接口的類代碼。

internal class CreditCardInfo : IInfoCardFactory,IInfoCard
    {
        private string[] categories = new string[] { "Credit Card" };
        private string name;

        public string[] CategoriesSupported
        {
            get { return categories; }
            set { categories = value; } 

        }

        public string Name { get
            {
                return this.name;   
            } 
            set { this.name = value; }  
        }

        public string Category
        {
            get
            {
                return this.categories[0];  
            }
            set
            {
                this.categories[0] = value;
            }
        }

        public void CloseDisplay()
        {
            throw new NotImplementedException();
        }

        public IInfoCard CreateInfoCard(string initialDetails)
        {
            string[] stringToSplit = initialDetails.Split(new char[] { '|' });
            string category = stringToSplit[0];
            string description = stringToSplit[1];
            return new CreditCardInfo();
        }

        public IInfoCard CreateNewInfoCard(string category)
        {
            return new CreditCardInfo();
        }

        public void DisplayData(Panel displayPanel)
        {
            string data = this.Name + "\n" + this.Category;
            //add a label and concatenate to the data
            Label label = new Label();
            label.Text = data;
            //add the label to the panel
            displayPanel.Controls.Add(label);
        
        }
        public bool EditData()
        {
            //show the form to edit the credit card information
            //start add credit card info with the fields 
            CreditCardForm creditCardForm = new CreditCardForm();
            creditCardForm.Name = this.Name;
            //get the fields and add the values to them
            
            creditCardForm.ShowDialog();
            return true;
        }

        public string GetDataAsString()
        {
           //I need help here getting the data as string
            return "Data";
        }

        public string GetDescription(string category)
        {
            return "Save personal info about your credit card";
        }
    }

我評論過我需要幫助的部分是卡住的地方,該方法應該將輸入到 CreditCard 信息字段中的所有數據作為單個字符串返回。 由於表單是通過接口實現的,如何將表單中的數據傳遞給類方法GetDataAsString()

看起來您需要確定對話結果是否為真,然后調用 GetDataAsString() 方法。 您可能還需要一些 PropertyChanged 事件,將表單中的數據分配給屬性,但我不確定,因為我不在我的電腦 atm 上。

要將屬性轉換為 1 個字符串,您需要執行以下操作

var string = property1.ToString();
string += property2.ToString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM