简体   繁体   中英

Displaying<List> reports in a message box

Within my WinForm i have a method that checks the validation of various user controls and adds them to an errorList. When the user clicks the save button I want it to check the validation method and show the errors if any in a message box. The Validate method is in another form and class so i think that might be my problem.

 private void Save_Click(object sender, EventArgs e)
    {
        var errorList = string.Join(Environment.NewLine, Validate.ToArray());
        MessageBox.Show(errorSet);

    }

Thank you for any help.

The error 'Form1.Validate(System.Collections.Generic.List<string>)' is a 'method', which is not valid in the given context means that you're using the method wrong.

var errorList = string.Join(Environment.NewLine, Validate.ToArray());

makes no sense. You're missing the parentheses:

var errorList = string.Join(Environment.NewLine, Validate().ToArray());

That's only one problem. The method has a parameter of type List<string> , but you don't pass an argument to the function.

Also, you said in a comment that the return value is of type bool , but it seems you expect it to return a collection of strings.

You have this issue because you are calling the validate method that is on another form without mentioning the instance of that form.

Lets say you have another class Class1.

 //create instance of your class/form that has this method
 OperationControl oc  = new OperationControl ();

 private void Save_Click(object sender, EventArgs e)
    {
        //call the method with form instance created above
        var errorList = string.Join(Environment.NewLine, oc.Validate().ToArray());
        MessageBox.Show(errorSet);

    }

some time this error means you may have the same method with the same name in your program scope. Check if no other function named MessageBox exist in your program

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