简体   繁体   中英

c# output from a form to another class

Hi I am creating a application of three types of dark chocolate. I have a windows form (inherited from another form) and one other class.

For my windows form class:

    darkchoco darkco = new darkchoco();
    string[] darkType = new string[3] { "Dark Chocolate Truffle", "Dark Chocolate Bar", "Dark Chocolate Almond" };
    public string returnType;

darkco is the another class. for the box in the windows form, I have a mousedown action defined:

    private void optionBox1_MouseDown(object sender, MouseEventArgs e)
    {
        optionBox1.DoDragDrop(optionBox1.Text,
            DragDropEffects.Copy);
        returnType = darkType[0];
        darkco.darkType = returnType;

for my darkco class, I have:

         public string darkType;
         public override string ToString()
    {
        return "For dark chocolate, you have selected: " + darkType;
    }

I don't know why the darkType doesn't show up, the only thing shows us is "for dark chocolate, you have selected: " then nothing. My question is where is my code wrong that makes darkType not showing up.

Looking forward to get it fixed, thanks!

Your example is not complete. How do you transfer the chocolate type to your darktype class? I'm not sure your darktype variable is part of the darktype class. I would expect something like this. Now you can construct your darco class, using the chocolate type as construction parameter. You also can add logic to the property.

    public class darco
    {
    public string darktype { set; get; } = String.Empty;

    public darco( string mydarktype)
        {
        darktype = mydarktype;
        }

    public override string ToString()
        {
        return "For dark chocolate, you have selected: " + darkType;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM