Comparing type
-
Can anyone tell me how to compare two objects by testing if they are the same type? I know this doesn't work but I wish it would work like this: private bool ActivateForm(Form form) { bool formIsOpen = false; //loop through child forms to see if the form is already open foreach (Form childForm in this.MdiChildren) { //if the childForm is the parameter type, activate it and break the loop if (childForm.Type == form.Type) { childForm.Activate(); formIsOpen = true; break; } } return formIsOpen; } Lester http://www.lestersconyers.com
-
Can anyone tell me how to compare two objects by testing if they are the same type? I know this doesn't work but I wish it would work like this: private bool ActivateForm(Form form) { bool formIsOpen = false; //loop through child forms to see if the form is already open foreach (Form childForm in this.MdiChildren) { //if the childForm is the parameter type, activate it and break the loop if (childForm.Type == form.Type) { childForm.Activate(); formIsOpen = true; break; } } return formIsOpen; } Lester http://www.lestersconyers.com
lsconyer wrote:
I know this doesn't work but I wish it would work like this:
You know what I wish? I wish it would work like this: Abbra Kadabra *waving hands* But I guess since it doesn't I will continue to Read Documentation and study Best Practices and Software Design Patterns etc.
-
Can anyone tell me how to compare two objects by testing if they are the same type? I know this doesn't work but I wish it would work like this: private bool ActivateForm(Form form) { bool formIsOpen = false; //loop through child forms to see if the form is already open foreach (Form childForm in this.MdiChildren) { //if the childForm is the parameter type, activate it and break the loop if (childForm.Type == form.Type) { childForm.Activate(); formIsOpen = true; break; } } return formIsOpen; } Lester http://www.lestersconyers.com
lsconyer wrote:
if (childForm.Type == form.Type)
if (childForm is Form)
will succeed when childForm is a Form, which it is due to the foreach loop.if (childForm is Form1)
will only succeed for those childForms that are also Form1, derived from Form. And also have a look at the "as" keyword! :)Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Can anyone tell me how to compare two objects by testing if they are the same type? I know this doesn't work but I wish it would work like this: private bool ActivateForm(Form form) { bool formIsOpen = false; //loop through child forms to see if the form is already open foreach (Form childForm in this.MdiChildren) { //if the childForm is the parameter type, activate it and break the loop if (childForm.Type == form.Type) { childForm.Activate(); formIsOpen = true; break; } } return formIsOpen; } Lester http://www.lestersconyers.com
*cough* use GetType() * cough *. Could I suggest that you also look at converting this to a generic method:
private bool ActivateForm<T>(T form) where T : Form { bool isOpen = false; int i = 0; do { if (MdiChildren[i].GetType() == typeof(T)) { MdiChildren[i].Activate(); isOpen = true; } } while (!isOpen && i++ < this.MdiChildren.Count); return isOpen }
I you really want to learn some more advanced .NET, you could look at modifying this to use anonymous methods.
Deja View - the feeling that you've seen this post before.
-
Can anyone tell me how to compare two objects by testing if they are the same type? I know this doesn't work but I wish it would work like this: private bool ActivateForm(Form form) { bool formIsOpen = false; //loop through child forms to see if the form is already open foreach (Form childForm in this.MdiChildren) { //if the childForm is the parameter type, activate it and break the loop if (childForm.Type == form.Type) { childForm.Activate(); formIsOpen = true; break; } } return formIsOpen; } Lester http://www.lestersconyers.com
Hi Try using the GetType() method instead of the Type keyword in your code