Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Comparing type

Comparing type

Scheduled Pinned Locked Moved C#
comtestingbeta-testingtutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lsconyer
    wrote on last edited by
    #1

    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

    L L P N 4 Replies Last reply
    0
    • L lsconyer

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • L lsconyer

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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


        1 Reply Last reply
        0
        • L lsconyer

          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

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          *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.

          1 Reply Last reply
          0
          • L lsconyer

            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

            N Offline
            N Offline
            Nissim Salomon
            wrote on last edited by
            #5

            Hi Try using the GetType() method instead of the Type keyword in your code

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups