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. Global Variables in SDI

Global Variables in SDI

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
25 Posts 7 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 LittleYellowBird

    If I understand you correctly, first declare your variable in a file such as 'Globals.h' and add that file to the project.

    int g_MyVariable = 0;

    Then declare it with the keyword 'extern' before it in a file called for example 'Externs.h'.

    extern int g_MyVariable;

    Then include that file in any cpp files that need toa access the gloabl variable.

    #include "Externs.h"

    Be carefull about using globals though, only use them where absolutely essential, otherwise stick to local variables - they are much easier to manage. :)

    Ali

    H Offline
    H Offline
    hellogany
    wrote on last edited by
    #3

    Hi My Doubt is how to call this glboal variables in other forms... Well i declared like this 1. Declare "static CString g_m" in APP.h 2.Declare CString CISApp::g_m = NULL in App.cpp 3.Use CISApp::g_m where ever you want to use

    K L 2 Replies Last reply
    0
    • H hellogany

      Hi My Doubt is how to call this glboal variables in other forms... Well i declared like this 1. Declare "static CString g_m" in APP.h 2.Declare CString CISApp::g_m = NULL in App.cpp 3.Use CISApp::g_m where ever you want to use

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #4

      in that case you have to declare static CString g_m in the class !!!

      Press F1 for help or google it. Greetings from Germany

      H 1 Reply Last reply
      0
      • K KarstenK

        in that case you have to declare static CString g_m in the class !!!

        Press F1 for help or google it. Greetings from Germany

        H Offline
        H Offline
        hellogany
        wrote on last edited by
        #5

        Ya.. i was tryin to use this.. This works well in Dialog Apps.. but am in SDi now ..Its throws Assertion Failed Error

        K D 2 Replies Last reply
        0
        • H hellogany

          Ya.. i was tryin to use this.. This works well in Dialog Apps.. but am in SDi now ..Its throws Assertion Failed Error

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #6

          use can also declare globals outside an class or in a separete global class.

          Press F1 for help or google it. Greetings from Germany

          1 Reply Last reply
          0
          • H hellogany

            Hi My Doubt is how to call this glboal variables in other forms... Well i declared like this 1. Declare "static CString g_m" in APP.h 2.Declare CString CISApp::g_m = NULL in App.cpp 3.Use CISApp::g_m where ever you want to use

            L Offline
            L Offline
            LittleYellowBird
            wrote on last edited by
            #7

            To access 'g_m' from another cpp file you need to declare 'g_m' as an extern in that file - see my first post. In my first post I suggested that you have dedicated files for this purpose, Globals.h and Externs.h. If you do this each time a cpp file needs to access a global you can just include the 'Externs.h' so it makes life easier. Also it anables you to quickly see all your globals at one time, you could add the extern declaration to each file that needs to access the global but as your project grows it will probably get messy very quickly. :)

            Ali

            H 1 Reply Last reply
            0
            • L LittleYellowBird

              To access 'g_m' from another cpp file you need to declare 'g_m' as an extern in that file - see my first post. In my first post I suggested that you have dedicated files for this purpose, Globals.h and Externs.h. If you do this each time a cpp file needs to access a global you can just include the 'Externs.h' so it makes life easier. Also it anables you to quickly see all your globals at one time, you could add the extern declaration to each file that needs to access the global but as your project grows it will probably get messy very quickly. :)

              Ali

              H Offline
              H Offline
              hellogany
              wrote on last edited by
              #8

              i get linker error when i follow ur steps??

              H L 2 Replies Last reply
              0
              • H hellogany

                i get linker error when i follow ur steps??

                H Offline
                H Offline
                hellogany
                wrote on last edited by
                #9

                how does to update to textbox.. I am Able to get date... is UpdateData(FALSE) Works fine in SDI(FormView) ???

                D 1 Reply Last reply
                0
                • H hellogany

                  i get linker error when i follow ur steps??

                  L Offline
                  L Offline
                  LittleYellowBird
                  wrote on last edited by
                  #10

                  OK, perhaps I've missed something. :) What is the linker error that you get?

                  Ali

                  C 1 Reply Last reply
                  0
                  • L LittleYellowBird

                    OK, perhaps I've missed something. :) What is the linker error that you get?

                    Ali

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #11

                    Usually a global variable is defined in one source file (you did it in a header one). :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    L H 2 Replies Last reply
                    0
                    • C CPallini

                      Usually a global variable is defined in one source file (you did it in a header one). :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      L Offline
                      L Offline
                      LittleYellowBird
                      wrote on last edited by
                      #12

                      :-O Really, I've been doing it that way for years! (I suppose that's how I was taught to declare variables in embedded systems.) Does it matter?

                      Ali

                      C 1 Reply Last reply
                      0
                      • C CPallini

                        Usually a global variable is defined in one source file (you did it in a header one). :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        H Offline
                        H Offline
                        hellogany
                        wrote on last edited by
                        #13

                        i solved it... but error occurs when i transfer the data to edit box... m_txtlog=g_MyVariable; UpdateData(FALSE); Error occurs while displayin

                        C 1 Reply Last reply
                        0
                        • L LittleYellowBird

                          :-O Really, I've been doing it that way for years! (I suppose that's how I was taught to declare variables in embedded systems.) Does it matter?

                          Ali

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #14
                          • If you don't include such header in any source file then you get 'undifined symbol' by the linker.
                          • If you include the header in multiple source files then you get 'multiple definitions' by the linker.
                          • If you include the header into exactly one source file then the linker will cheer you.

                          Of course this is going on my ... :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          L 1 Reply Last reply
                          0
                          • C CPallini
                            • If you don't include such header in any source file then you get 'undifined symbol' by the linker.
                            • If you include the header in multiple source files then you get 'multiple definitions' by the linker.
                            • If you include the header into exactly one source file then the linker will cheer you.

                            Of course this is going on my ... :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            L Offline
                            L Offline
                            LittleYellowBird
                            wrote on last edited by
                            #15

                            CPallini wrote:

                            If you include the header into exactly one source file then the linker will cheer you.

                            That's what I do, I declare my global variable in a header called 'Globals.h', then include it just once! :thumbsup: Then I declare it as extern in Externs.h and include it wherever I want to use it ..... :) The linker is happy, but I thought you considered it a bad habit or bad style and I wondered why ..... maybe I missunderstood ...... :)

                            Ali

                            C I 2 Replies Last reply
                            0
                            • L LittleYellowBird

                              CPallini wrote:

                              If you include the header into exactly one source file then the linker will cheer you.

                              That's what I do, I declare my global variable in a header called 'Globals.h', then include it just once! :thumbsup: Then I declare it as extern in Externs.h and include it wherever I want to use it ..... :) The linker is happy, but I thought you considered it a bad habit or bad style and I wondered why ..... maybe I missunderstood ...... :)

                              Ali

                              C Offline
                              C Offline
                              CPallini
                              wrote on last edited by
                              #16

                              Well, if you define the global variable into a source file then you lower the probabilities of duplicate inclusion... :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              H 1 Reply Last reply
                              0
                              • H hellogany

                                i solved it... but error occurs when i transfer the data to edit box... m_txtlog=g_MyVariable; UpdateData(FALSE); Error occurs while displayin

                                C Offline
                                C Offline
                                Cedric Moonen
                                wrote on last edited by
                                #17

                                hellogany wrote:

                                Error occurs

                                Which error ? Please be specific, we can't see what is displayed on your screen.

                                Cédric Moonen Software developer
                                Charting control [v3.0] OpenGL game tutorial in C++

                                1 Reply Last reply
                                0
                                • C CPallini

                                  Well, if you define the global variable into a source file then you lower the probabilities of duplicate inclusion... :)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

                                  H Offline
                                  H Offline
                                  hellogany
                                  wrote on last edited by
                                  #18

                                  Well While Debugging, i m able to get the value .... But couldnt able to display in edit control. Its throws Assertin Error

                                  C 1 Reply Last reply
                                  0
                                  • H hellogany

                                    Well While Debugging, i m able to get the value .... But couldnt able to display in edit control. Its throws Assertin Error

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #19

                                    Please post the (relevant) code. :)

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    H 1 Reply Last reply
                                    0
                                    • H hellogany

                                      Ya.. i was tryin to use this.. This works well in Dialog Apps.. but am in SDi now ..Its throws Assertion Failed Error

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #20

                                      hellogany wrote:

                                      Its throws Assertion Failed Error

                                      What line of what file is asserting?

                                      "One man's wage rise is another man's price increase." - Harold Wilson

                                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                      "Man who follows car will be exhausted." - Confucius

                                      1 Reply Last reply
                                      0
                                      • H hellogany

                                        how does to update to textbox.. I am Able to get date... is UpdateData(FALSE) Works fine in SDI(FormView) ???

                                        D Offline
                                        D Offline
                                        David Crow
                                        wrote on last edited by
                                        #21

                                        hellogany wrote:

                                        is UpdateData(FALSE) Works fine in SDI(FormView) ???

                                        Why are you using UpdateData()? Without fully understanding what it does and how to use it, it will only cause you grief. Use SetWindowText() instead.

                                        "One man's wage rise is another man's price increase." - Harold Wilson

                                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                        "Man who follows car will be exhausted." - Confucius

                                        1 Reply Last reply
                                        0
                                        • C CPallini

                                          Please post the (relevant) code. :)

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                          [My articles]

                                          H Offline
                                          H Offline
                                          hellogany
                                          wrote on last edited by
                                          #22

                                          m_txtlog=g_MyVariable; UpdateData(FALSE); m_txlog is the edit control i am using the above code in oncreate function...

                                          C 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