Solved

scope and object types - not knowing before initializing

Posted on 2004-08-01
9
285 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
 

Author Comment

by:sethUSer420
ID: 11693170
that's weak
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

16 Experts available now in Live!

Get 1:1 Help Now