Click here to Skip to main content
15,921,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Explain this to me Pin
Nick Parker31-Jul-03 17:54
protectorNick Parker31-Jul-03 17:54 
GeneralRe: Explain this to me Pin
J. Dunlap31-Jul-03 17:57
J. Dunlap31-Jul-03 17:57 
GeneralRe: Explain this to me Pin
Nick Parker31-Jul-03 18:00
protectorNick Parker31-Jul-03 18:00 
GeneralRe: Explain this to me Pin
J. Dunlap31-Jul-03 18:04
J. Dunlap31-Jul-03 18:04 
GeneralRe: Explain this to me Pin
Ista31-Jul-03 18:17
Ista31-Jul-03 18:17 
GeneralRe: Explain this to me Pin
Nick Parker31-Jul-03 18:31
protectorNick Parker31-Jul-03 18:31 
GeneralRe: Explain this to me Pin
Ista1-Aug-03 4:28
Ista1-Aug-03 4:28 
GeneralRe: Explain this to me Pin
James T. Johnson1-Aug-03 0:00
James T. Johnson1-Aug-03 0:00 
I use as when its possible for some object to not be castable to the type I want.

For example:

private void MyEventHandler(object sender, EventArgs e)
{
  ToolBarButton b = sender as Button;
  MenuItem mi = sender as MenuItem;
  
  if( b != null )
  {
    // User clicked the toolbar button
  }
  else if( mi != null )
  {
    // User clicked the menu item or 
    // used the shortcut keys
  }
}
You would so something like this where the same event handler is used for two different events. In this case standard behavior dictates (at least it used to) that when you click the Print button on the toolbar you don't show the Print dialog box. While clicking the Print command in the menu you should display the Print dialog box.

Unfortunately my example has one slight flaw, the way the ToolBar control works makes it hard to use my example. I still chose to use that example because it is something we should be familiar with.

You should use a cast when you don't expect the cast to fail (an exceptional condition calls for an exception to be thrown).

James

"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him

GeneralRe: Explain this to me Pin
Ista1-Aug-03 4:37
Ista1-Aug-03 4:37 
GeneralRe: Explain this to me Pin
Nick Parker1-Aug-03 5:36
protectorNick Parker1-Aug-03 5:36 
GeneralRe: Explain this to me Pin
Ista1-Aug-03 5:51
Ista1-Aug-03 5:51 
GeneralRe: Explain this to me Pin
StealthyMark1-Aug-03 0:26
StealthyMark1-Aug-03 0:26 
GeneralRe: Explain this to me Pin
Arun Bhalla1-Aug-03 6:55
Arun Bhalla1-Aug-03 6:55 
GeneralRe: Explain this to me Pin
Ista1-Aug-03 7:06
Ista1-Aug-03 7:06 
GeneralRe: Explain this to me Pin
Philip Fitzsimons1-Aug-03 2:31
Philip Fitzsimons1-Aug-03 2:31 
GeneralRe: Explain this to me Pin
Ista1-Aug-03 4:47
Ista1-Aug-03 4:47 
GeneralC# .NET control - Problem in NT 4.0 Help!! Pin
CodeFriendly31-Jul-03 14:09
CodeFriendly31-Jul-03 14:09 
GeneralRe: C# .NET control - Problem in NT 4.0 Help!! Pin
Ista31-Jul-03 16:03
Ista31-Jul-03 16:03 
GeneralBuilding and running on other machines. Pin
mikemilano31-Jul-03 13:19
mikemilano31-Jul-03 13:19 
GeneralRe: Building and running on other machines. Pin
Mike Dimmick31-Jul-03 13:31
Mike Dimmick31-Jul-03 13:31 
GeneralProper class design Pin
Ista31-Jul-03 12:27
Ista31-Jul-03 12:27 
GeneralRe: Proper class design Pin
Nick Parker31-Jul-03 18:05
protectorNick Parker31-Jul-03 18:05 
GeneralRe: Proper class design Pin
Ista31-Jul-03 18:09
Ista31-Jul-03 18:09 
GeneralRe: Proper class design Pin
Nick Parker31-Jul-03 18:19
protectorNick Parker31-Jul-03 18:19 
GeneralRe: Proper class design Pin
Ista1-Aug-03 4:26
Ista1-Aug-03 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.