?
Solved

scope and object types - not knowing before initializing

Posted on 2004-08-01
9
Medium Priority
?
296 Views
Last Modified: 2008-03-03
I am calling a method from an event in a webcontrol, this event passes the sender object, which I want to use.

I want to take the sender object, in whatever form it is, say a button or linkbutton, and put it into a reference.

As I first set out to write this code: I check the type of the object, and depending on what type it is, I cast that type into my var for holding, only problem is, oh yeah it's out of scope when I leave my statement block, and I don't know the type beforehand to define it directly in the method.

How do I define a reference to the sender object, cast to the correct type, that is accessible from within my event method (in the case below "submitForm") without knowing the type of the sender beforehand?


protected void submitForm(object sender, System.EventArgs e)
{
      if ( sender.GetType().Name.ToString() == "Button")
      {
            Button submitter = (Button)sender;
      }
      else if ( sender.GetType().Name.ToString() == "LinkButton")
      {
            LinkButton submitter = (LinkButton)sender;
      }
      // submitter reference not available
}
0
Comment
Question by:sethUSer420
9 Comments
 
LVL 3

Expert Comment

by:dukkorg
ID: 11691251
Something like this should work:

protected void submitForm(object sender, System.EventArgs e)
{
    Button submitter = sender as Button;

    if (submitter != null)
    {
        // Got the submitter... You can do what ever you want with it
    }
    else
    {
        // Couldn't to get the submitter.
    }
}

You could do the same thing for a LinkButton.

I don't think I get what your talking about with it being out of scope. The sender is passed into the method so it would be in scope.
0
 

Author Comment

by:sethUSer420
ID: 11692141
what if submitter could be a Button or -something else-

aka

Button submitter - sender as Button;

wouldn't work if submitter is a LinkButton
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 750 total points
ID: 11693148
It depends on what you want to do after the end of this if. If you want to execute something which is part of the control class, you can do it like this:

protected void submitForm(object sender, System.EventArgs e)
{
     Control submitter = (Control)sender;

     // Do whatever which is part of the control class....
}

If you want to do things that are specific for buttons or link buttons, you have to write the whole code inside the if statement
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:sethUSer420
ID: 11693170
that's weak
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11693173
What do you mean "weak"?
0
 

Author Comment

by:sethUSer420
ID: 11693261
it's weak that I can't make

protected void submitForm(object sender, System.EventArgs e)
{
     if ( sender.GetType().Name.ToString() == "Button")
     {
          Button submitter = (Button)sender;
     }
     else if ( sender.GetType().Name.ToString() == "LinkButton")
     {
          LinkButton submitter = (LinkButton)sender;
     }
     // have reference available here// // can't do it?
}

work
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11693275
What do you want to do with the submitter at the point:

// have reference available here// // can't do it?

I showed you how you can do it by declaring the submitter as a Control object. It is not possible to have one variable being of two types. However both Button and LinkButton are Controls, so this is their common parent and the only way to use a single variable
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 11693896
try something like this...



      private void button1_Click(object sender, System.EventArgs e)
      {
         Control submitter;



         if (sender.GetType().Equals(typeof(Button)))
            submitter = (Button)sender;
         else
            submitter = (TextBox)sender;
         


         Console.WriteLine(submitter.GetType());
      }
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11693912
No need for this if _TAD_, my solution makes the same with less code...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question