Solved

scope and object types - not knowing before initializing

Posted on 2004-08-01
9
284 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
that's weak
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
What do you mean "weak"?
0
 

Author Comment

by:sethUSer420
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
No need for this if _TAD_, my solution makes the same with less code...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

12 Experts available now in Live!

Get 1:1 Help Now