Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:30:52+00:00 2026-05-26T22:30:52+00:00

I have a function, void Validate() , that contains all my validation logic for

  • 0

I have a function, void Validate(), that contains all my validation logic for a window.

I can’t simply register this as an event handler because it doesn’t accept the parameters required by the event handler delegates. Additionally different types of controls have different signatures so I can’t just have Validate match one signature while ignoring their contents.

Here is a small example of what I’ve setup

    txt1.TextChanged += Validate_TextChange;
    password1.PasswordChanged += Validate_RoutedEvent;
    txt2.TextChanged += Validate_TextChange;

    txt3.TextChanged += Validate_TextChange;
    password2.PasswordChanged += Validate_RoutedEvent;
    txt4.TextChanged += Validate_TextChange;


void Validate_RoutedEvent(object sender, RoutedEventArgs e)
{
    ValidateOptions();
}

void Validate_TextChange(object sender, TextChangedEventArgs e)
{
    ValidateOptions();
}

public void ValidateOptions()
{
   //Actual validation here
}

This just shows 2 examples, more controls could have even more signatures. Is there any better way of getting all event handlers to call a function in a case where I don’t care about the arguments being passed?


EDIT:
I like the option proposed by Jon to add the parameters and simply ignore them. That does solve most of the problem but anytime I want to call this function directly, such as to manually trigger validation, I then have to include dummy arguments to satisfy the compiler.
ValidateOptions(this, new EventArgs())

The suggestion Dan made, to use anonymous functions, would handle this but isn’t as clean when associating the event handlers.

It doesn’t appear that either solution lets you register a function as an event handler while ignoring the signature while also preserving the ability to call the function without creating dummy arguments but there are multiple ways to get pretty close.


EDIT:

Here is the updated example implementing Jon’s solution for generic event handling but keeping a 0 parameter function that can be called directly

txt1.TextChanged += ValidationEvent;
password1.PasswordChanged += ValidationEvent;
txt2.TextChanged += ValidationEvent;

txt3.TextChanged += ValidationEvent;
password2.PasswordChanged += ValidationEvent;
txt4.TextChanged += ValidationEvent;


//Single event handler accepting EventArgs, which is the base class
//for all more-specific event classes
void ValidationEvent(object sender, EventArgs e)
{
    //Ignores arguments and calls 0 argument ValidateOptions
    ValidateOptions();
}

//0 argument function that performs actual validation and can be called
//directly from other functions without the need to pass in a fake sender
//and eventargs parameter
public void ValidateOptions()
{
   //Actual validation here
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:30:53+00:00Added an answer on May 26, 2026 at 10:30 pm

    You can have a single method which ignores the parameters:

    public void ValidateOptions(object sender, EventArgs e)
    {
    }
    

    The compiler will allow a conversion from the ValidateOptions method group to any delegate type following the normal event pattern such that the first parameter is sender and the second parameter is some type derived from EventArgs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function: void ToUpper(char * S) { while (*S!=0) { *S=(*S >=
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have a function like this: public void AddEntry(Entry entry) { if (entry.Size
This is driving me crazy: I have function void gst_init(int *argc, char **argv[]); in
I'm having problems wrapping my head around this. I have a function void foo(istream&
I code this function which converts all pipes into commas, and after that converting
i have a function that receives a function as a parameter. example: function foo(bar:Function):void()
I have a function void AddEntity(Entity* addtolist) that pushes elements back onto a vector
I have this function: void receive_message(int sock, char buffer[]) { int test = recv(sock,
I have main function like this: void main() { char *s; inputString(s); printf(%s,s); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.