0

Not sure where I went wrong here.

I have a class called EMails and routine called EmailEvent, which is called from another page. Here is the method declaration:

public void EmailEvent(
                       string evtDate, 
                       string evtName, 
                       string evtContact, 
                       string evtBody, 
                       string lnkMinutes, 
                       string lnkTReports, 
                       String[] textTo)

As you'll see, I'm passing in an array. On the page calling the routine I have this code:

ArrayList mailIDs = new ArrayList();
        switch (ddlSelection.SelectedValue.ToString())
        {
            case "One":
                mailIDs.Add(ddlallMembers.SelectedValue.ToString());
                break;
            case "Members":
                mailIDs.Add(ddlSelection.SelectedValue.ToString());
                break;
            case "Prospects":
                mailIDs.Add(ddlSelection.SelectedValue.ToString());
                break;
            case "All":
                mailIDs.Add(ddlSelection.SelectedValue.ToString());
                break;
            case "List":
                for (int mems = 0; mems < lbChosen.Items.Count; mems++)
                {
                    mailIDs.Add(lbChosen.Items[mems].Value);
                }
                break;
            default:
                Response.Write("<script>alert('Invalid Selection.  Try again.')</script>");
                break;
        }
        String[] myArr = (String[]) mailIDs.ToArray( typeof( string ) );
        Emails.EmailEvent(
                          tbEventDt.Text, 
                          tbEvent.Text, 
                          tbContact.Text, 
                          tbEventText.Text, 
                          hlMinutes.Target.ToString(), 
                          hlTReport.Target.ToString(), 
                          myArr);

So I'm passing all my params, seems to be correct, but the last line is marked as error as follows:

Error 5 An object reference is required for the non-static field, method, or property 'Emails.EmailEvent(string, string, string, string, string, string, string[])'

I've tried many variations of params (pass JUST the array, pass everything EXCEPT the array with appropriate changes to the procedure). Always the same error.

So what'd I do wrong? This is the first time I've ever tried to pass an array (could be 1 value, could be a dozen).

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • possible duplicate of [An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) – Alexei Levenkov Jul 11 '14 at 03:13
  • "Error 5" looks strange - should be [CS0120](http://msdn.microsoft.com/en-us/library/s35hcfh7.aspx)... Side note: please consider using search on error message before asking questions... – Alexei Levenkov Jul 11 '14 at 03:15

2 Answers2

2

The problem is that you've defined EmailEvent as an instance method:

public void EmailEvent( ... )

And you are calling it like it is a static method:

Emails.EmailEvent( ... );

You meant to create or acquire an instance of the class Emails and then call EmailEvent on that instance:

Emails myEmails = new Emails();
myEmails.EmailEvent( ... );
antiduh
  • 11,853
  • 4
  • 43
  • 66
0

You have two options. If you want the event to be raised per instance of the class, then you will need to declare an instance of the class before you can raise the event.

If you want a single event that is raised from the class throughout the application domain, you will need to change the event declaration to "public static void EmailEvent"

user2920518
  • 318
  • 1
  • 8