Solved

scope and object types - not knowing before initializing

Posted on 2004-08-01
9
286 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 250 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now