Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unknown code 2

Status
Not open for further replies.

bouwob

Programmer
Apr 12, 2006
171
0
0
US
I have a chunk of code that was written prior to me getting on the project.

PageSelection((int)((Page)((Control) sender).Parent).Tag)


Now I understand what page selection does and it accepts an int.

PageSelection((int) //set up method and cast to int

the next part is a call to a class

((Page)

but what exactly does this do?

((Control) sender). //Is this a page event? any ideas? When I go to the deffinition it takes me to the object browser and I am completly loss on what exactly this is trying to do and how to fix it.

tia
 
Control s = (Control)Sender; //Casting the object to a Control
Page p = (Page)s.Parent; //Casting the parent object to a Page
int pagenum = (int)p.Tag; //Casting the tag object of the page to an integer


That line basically finds the number stored in the Tag property of the current page. The code above just breaks down what that line of code is. When you see an object type in brackets (int)(Page)(Control) etc... you are saying that the basic object type that we know of is in fact the type shown in the brackets.

int num = (int)someobject; //convert someobject to int.
 
PageSelection((int)((Page)((Control) sender).Parent).Tag)

A good candidate for nomination at TheDailyWTF.com!

A better/safer way to do this would be to use the as keyword, as it won't throw an exception if the cast fails -- it just returns null, which you can test for.
Code:
Control c = sender as Control;
if (c != null)
{
   Page p = c.Parent as Page;
   if (p != null)
   {
      int t;
      if (int.TryParse(p.Tag, out t))
         PageSelection(t);
   }
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top