Solved

scope and object types - not knowing before initializing

Posted on 2004-08-01
9
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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