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 / C++ / MFC
  4. which one is better?

which one is better?

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 8 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.
  • S Smith

    To acess a control... * using GetDlgItem(IDC_) is better or using the control Variable m_Control is better? :confused: regards, Rookie

    B Offline
    B Offline
    Bob Stanneveld
    wrote on last edited by
    #2

    Hello, It's just a matter of preference. Using the GetDlgItem() involves a lot of ugly typecasts and unnassecary code. Using the m_Control variable looks much more cleaner. I even think that it is slightly faster than the other method, since you already have a CWnd derived object. (Unless you use primitive types or CStrings for managing dialog data.) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

    S 1 Reply Last reply
    0
    • S Smith

      To acess a control... * using GetDlgItem(IDC_) is better or using the control Variable m_Control is better? :confused: regards, Rookie

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #3

      as bob said, using GetDlgItem() each time you want to access a control add several function calls to your code that you could avoid. but of course, using a member variable to store the CWnd* of a control is working unless you initialize it correctly (with... GetDlgItem() :-D ) what i used to do personnaly is having a set of m_p???????? variables in my Dialog classes, that i initialize into OnInitDialog() with a set of GetDlgItem() calls...


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      G 1 Reply Last reply
      0
      • B Bob Stanneveld

        Hello, It's just a matter of preference. Using the GetDlgItem() involves a lot of ugly typecasts and unnassecary code. Using the m_Control variable looks much more cleaner. I even think that it is slightly faster than the other method, since you already have a CWnd derived object. (Unless you use primitive types or CStrings for managing dialog data.) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        S Offline
        S Offline
        Smith
        wrote on last edited by
        #4

        thanx bob :) regards, Rookie Installing MFC...2% complete.

        B 1 Reply Last reply
        0
        • S Smith

          To acess a control... * using GetDlgItem(IDC_) is better or using the control Variable m_Control is better? :confused: regards, Rookie

          M Offline
          M Offline
          Marc Soleda
          wrote on last edited by
          #5

          IMO depends on the situation. eg: if you have lots of string resources and you need to reset its value it would be a good thing to create all of them consecutively to permit looping from the base one to the last just incresing a counter. Marc Soleda ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

          S 1 Reply Last reply
          0
          • M Marc Soleda

            IMO depends on the situation. eg: if you have lots of string resources and you need to reset its value it would be a good thing to create all of them consecutively to permit looping from the base one to the last just incresing a counter. Marc Soleda ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

            S Offline
            S Offline
            Smith
            wrote on last edited by
            #6

            nice stuff guys.. it seems that i'd use both the ways according to the need.. thank u so much :-D regards, Rookie Installing MFC...2% complete.

            1 Reply Last reply
            0
            • S Smith

              To acess a control... * using GetDlgItem(IDC_) is better or using the control Variable m_Control is better? :confused: regards, Rookie

              P Offline
              P Offline
              Pradyumna Gogte
              wrote on last edited by
              #7

              Hi, Well you could use either method is correct however, using m_Control has a slight overhead. The reason is that m_Controls are MFC controls while the form has windows controls. So when the form is instantiated the MFC code kicks in and creates the m_Controls. I would prefer this method if I have a lot of interaction with the controls. However if I dont have to interact with the controls or have only to interact with them one time then it is better to use the GetDlgItem method. Prady

              B 1 Reply Last reply
              0
              • S Smith

                thanx bob :) regards, Rookie Installing MFC...2% complete.

                B Offline
                B Offline
                Bob Stanneveld
                wrote on last edited by
                #8

                You're welcome :-D Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                1 Reply Last reply
                0
                • P Pradyumna Gogte

                  Hi, Well you could use either method is correct however, using m_Control has a slight overhead. The reason is that m_Controls are MFC controls while the form has windows controls. So when the form is instantiated the MFC code kicks in and creates the m_Controls. I would prefer this method if I have a lot of interaction with the controls. However if I dont have to interact with the controls or have only to interact with them one time then it is better to use the GetDlgItem method. Prady

                  B Offline
                  B Offline
                  Bob Stanneveld
                  wrote on last edited by
                  #9

                  Overhead you say? If you use the other method for a lot of controls you typically use not so much (controls in option dialogs for exemple), you create more unnaccesary overhead using more code to initialze and use the controls. All the unaccessary error checks, function calls are also overhead. The initialization done by MFC is behind the scenes and in GUI applications, the overhead added by MFC doesn't matter very much. So why go through all the fuss of writing pure API code with all the trouble it brings when you can do without? IMHO, one should write as few lines of Win32 API code for the GUI when he / she uses MFC. This is not only for consistancy, but also saves a lot of troubles. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                  P 1 Reply Last reply
                  0
                  • B Bob Stanneveld

                    Overhead you say? If you use the other method for a lot of controls you typically use not so much (controls in option dialogs for exemple), you create more unnaccesary overhead using more code to initialze and use the controls. All the unaccessary error checks, function calls are also overhead. The initialization done by MFC is behind the scenes and in GUI applications, the overhead added by MFC doesn't matter very much. So why go through all the fuss of writing pure API code with all the trouble it brings when you can do without? IMHO, one should write as few lines of Win32 API code for the GUI when he / she uses MFC. This is not only for consistancy, but also saves a lot of troubles. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                    P Offline
                    P Offline
                    Pradyumna Gogte
                    wrote on last edited by
                    #10

                    True, but you did not read my post. I said only if you wanted to do one or two interactions with the control you could use GetDlgIt.. fn. Prady

                    B 1 Reply Last reply
                    0
                    • S Smith

                      To acess a control... * using GetDlgItem(IDC_) is better or using the control Variable m_Control is better? :confused: regards, Rookie

                      RaviBeeR Offline
                      RaviBeeR Offline
                      RaviBee
                      wrote on last edited by
                      #11

                      GetDlgItem() is error prone - see this[^] article. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

                      1 Reply Last reply
                      0
                      • P Pradyumna Gogte

                        True, but you did not read my post. I said only if you wanted to do one or two interactions with the control you could use GetDlgIt.. fn. Prady

                        B Offline
                        B Offline
                        Bob Stanneveld
                        wrote on last edited by
                        #12

                        If you realize that only 10% of your code is used 90% of the time leaves you 90% of your code that is used almost never. Believing this, most of your controls are almost never used. If I would use your method and write GetDlgItem() code, I would be better off writing pure C windows applications, simply because it would make more sense. The control variable method on the contrary, fits perfectly in the MFC. Thats one part of the MFC that is specially designed that way, jsut because you don't have to use GetDlgItem() anyore! Why would you use MFC in the first place if you bypass all the fancy stuff it was designed for? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                        1 Reply Last reply
                        0
                        • S Smith

                          To acess a control... * using GetDlgItem(IDC_) is better or using the control Variable m_Control is better? :confused: regards, Rookie

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          There may be times when you create controls dynamically then GetDlgItem() is usually easier, otherwise m_Control makes for clearer and more compact code. Elaine :rose: The tigress is here :-D

                          1 Reply Last reply
                          0
                          • T toxcct

                            as bob said, using GetDlgItem() each time you want to access a control add several function calls to your code that you could avoid. but of course, using a member variable to store the CWnd* of a control is working unless you initialize it correctly (with... GetDlgItem() :-D ) what i used to do personnaly is having a set of m_p???????? variables in my Dialog classes, that i initialize into OnInitDialog() with a set of GetDlgItem() calls...


                            TOXCCT >>> GEII power
                            [toxcct][VisualCalc]

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #14

                            toxcct wrote: what i used to do personnaly is having a set of m_p???????? variables in my Dialog classes, that i initialize into OnInitDialog() with a set of GetDlgItem() calls... You do realize that the return value of GetDlgItem() points to a temporary CWnd object that is only valid for the current Windows message? This means you can't save that return value and use it later, since CWnd object may no longer exist.


                            Software Zen: delete this;

                            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