Click here to Skip to main content
15,922,584 members
Home / Discussions / C#
   

C#

 
GeneralRe: NotifyIcon text? Pin
Eric Gunnerson (msft)24-Jul-02 5:28
Eric Gunnerson (msft)24-Jul-02 5:28 
GeneralRe: NotifyIcon text? Pin
sasdav24-Jul-02 10:29
sasdav24-Jul-02 10:29 
GeneralRe: NotifyIcon text? Pin
Eric Gunnerson (msft)24-Jul-02 10:57
Eric Gunnerson (msft)24-Jul-02 10:57 
GeneralRe: NotifyIcon text? Pin
sasdav25-Jul-02 2:32
sasdav25-Jul-02 2:32 
QuestionCorrect way to try-catch in foreach loop? Pin
leppie22-Jul-02 22:15
leppie22-Jul-02 22:15 
AnswerRe: Correct way to try-catch in foreach loop? Pin
James T. Johnson22-Jul-02 23:08
James T. Johnson22-Jul-02 23:08 
GeneralRe: Correct way to try-catch in foreach loop? Pin
leppie23-Jul-02 0:06
leppie23-Jul-02 0:06 
GeneralRe: Correct way to try-catch in foreach loop? Pin
James T. Johnson23-Jul-02 0:27
James T. Johnson23-Jul-02 0:27 
Yes, that is ok.

There is one problem you may run into; and that is if you want to actually execute DoStuff on instances of MyClass as well as instances of a class that inherits from MyClass.

The above technique also won't work with all interfaces because of the way that the GetType() method works in regard to them.

Both cases can be solved by using the is and as operators. Essentially (not what really happens but the effect is the same) the is operator returns true of the left operand can be cast to the right operand. The as operator casts the operand to the type of the right operand, but returns null if the cast cannot be performed. If is returns true, then as will not return null and vice versa.

public class MyClass {}
public interface IMyInterface {}
 
public class Foo : MyClass {}
public class Bar : MyInterface {}
public class Baz : MyClass, IMyInterface {}
 
...
 
MyClass mc;
Foo foo;
Bar bar;
Baz baz;
 
mc is MyClass == true
mc is IMyInterface == false
 
foo is MyClass == true
foo is IMyInterface == false
 
bar is MyClass == false
bar is IMyInterface == true
 
baz is MyClass == true
baz is IMyInterface == true
To me the is and as operators are more versitile than simply comparing the Type returned from GetType()/typeof.

James
"Java is free - and worth every penny." - Christian Graus
GeneralRe: Correct way to try-catch in foreach loop? Pin
leppie23-Jul-02 0:45
leppie23-Jul-02 0:45 
GeneralRe: Correct way to try-catch in foreach loop? Pin
James T. Johnson23-Jul-02 1:24
James T. Johnson23-Jul-02 1:24 
GeneralRe: Correct way to try-catch in foreach loop? Pin
jparsons23-Jul-02 1:46
jparsons23-Jul-02 1:46 
GeneralRe: Correct way to try-catch in foreach loop? Pin
leppie23-Jul-02 7:17
leppie23-Jul-02 7:17 
GeneralNon-client area of controls Pin
Kastro22-Jul-02 18:32
Kastro22-Jul-02 18:32 
GeneralProperty Grid without an object Pin
Luis Alonso Ramos22-Jul-02 14:43
Luis Alonso Ramos22-Jul-02 14:43 
GeneralRe: Property Grid without an object Pin
James T. Johnson22-Jul-02 17:54
James T. Johnson22-Jul-02 17:54 
GeneralRe: Property Grid without an object Pin
Luis Alonso Ramos22-Jul-02 18:55
Luis Alonso Ramos22-Jul-02 18:55 
GeneralRe: Property Grid without an object Pin
James T. Johnson22-Jul-02 23:16
James T. Johnson22-Jul-02 23:16 
GeneralRe: Property Grid without an object Pin
leppie22-Jul-02 20:40
leppie22-Jul-02 20:40 
GeneralRe: Property Grid without an object Pin
Luis Alonso Ramos24-Jul-02 5:53
Luis Alonso Ramos24-Jul-02 5:53 
GeneralRe: Property Grid without an object Pin
Nathan Blomquist24-Jul-02 6:32
Nathan Blomquist24-Jul-02 6:32 
GeneralRe: Property Grid without an object Pin
Luis Alonso Ramos24-Jul-02 7:32
Luis Alonso Ramos24-Jul-02 7:32 
GeneralRe: Property Grid without an object Pin
Luis Alonso Ramos24-Jul-02 9:49
Luis Alonso Ramos24-Jul-02 9:49 
GeneralRe: Property Grid without an object Pin
Nathan Blomquist24-Jul-02 10:14
Nathan Blomquist24-Jul-02 10:14 
GeneralRe: Property Grid without an object Pin
Luis Alonso Ramos24-Jul-02 10:48
Luis Alonso Ramos24-Jul-02 10:48 
QuestionWhat override when the control is added? Pin
Zombies with Coffee, LLC22-Jul-02 11:04
professionalZombies with Coffee, LLC22-Jul-02 11:04 

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.