Home > Software design >  How to pass values from a form to an interface class in C#?
How to pass values from a form to an interface class in C#?

Time:05-01

I need some homework help, I have a bunch of interfaces that implement credit card forms. How can I pass a value entered into a form (like a textbox entry) by a user to a class that implemented the interfaces? The interface code is below

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);
}

I created a new class that implements those interfaces. The interface implements my form but the instructor hid that code from us. Here is the class code that implemented the above two interfaces.

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";
        }
    }

The section where I have commented I need help is where am stuck, That method is supposed to return all the data entered into the CreditCard info fields as a single string. Since the forms were implemented through the interfaces, how do I pass the data from the form to the class method GetDataAsString()?

CodePudding user response:

public string GetName(int ID)
{
if (ID < names.Length)
    return names[ID];
else
    return String.Empty;
}
private string[] names = { "Spencer", "Sally", "Doug" };

Try something like this, if that's not help let me know ;)

CodePudding user response:

It looks like you need to determine if the dialog result is true, which then calls the GetDataAsString() method. You may also need some PropertyChanged events, to assign the data from the form to the properties, but I’m not sure entirely, as I’m not at my computer atm.

To get the properties into 1 string, you will need to do something like this

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

Hope this helps

  • Related