Global Variable Ends Up as Nothing in Form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred Block

    Global Variable Ends Up as Nothing in Form

    Hi All,

    I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and
    I'm very excited.

    Here's an issue I'm experiencing right off the starting line and cannot make
    sense of it:

    I have a module (Public) and in it contains a few global variables (String
    data type). When I go to use these in a Form the value is "Nothing." Why is
    this?
    ...
    Public Module modMain

    Public g_strMyPublicVa r As String

    Then in the form (frmMain), the g_strMyPublicVa r does not contain the data
    as it was set in modMain.


    Any insight is greatly appreciated and I thank you in advance!

    Kind regards - Fred


  • kimiraikkonen

    #2
    Re: Global Variable Ends Up as Nothing in Form

    On Nov 19, 4:28 pm, "Fred Block" <fblock_no_spam m...@w-systems.com>
    wrote:
    Hi All,
    >
    I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and
    I'm very excited.
    >
    Here's an issue I'm experiencing right off the starting line and cannot make
    sense of it:
    >
    I have a module (Public) and in it contains a few global variables (String
    data type). When I go to use these in a Form the value is "Nothing." Why is
    this?
    ..
    Public Module modMain
    >
    Public g_strMyPublicVa r As String
    >
    Then in the form (frmMain), the g_strMyPublicVa r does not contain the data
    as it was set in modMain.
    >
    Any insight is greatly appreciated and I thank you in advance!
    >
    Kind regards - Fred
    In your post, you didn't assign any value to "g_strMyPublicV ar", thus
    you may get a null refence exception.

    If you do something like this, you won't get an exception.

    ' In your module, assign a value to "g_strMyPublicV ar"
    Public Module modMain
    ' eg: Assign value "foo"
    Public g_strMyPublicVa r As String = "foo"
    End Module

    or assign a value to "g_strMyPublicV ar" in your form(frmMain) inside a
    method:

    ' In your form, that'll work
    Public Class Form1
    Sub mymethod()
    g_strMyPublicVa r = "foo"
    End Sub
    End Class

    ' That won't work, if you assign value at class-level
    ' in your form
    Public Class Form1
    g_strMyPublicVa r = "foo"
    End Class

    ....And as you pointed, you are declaring it in your module so,
    assigning a value to "g_strMyPublicV ar" in your module should work
    when you call it within your form.

    Hope this helps,

    Onur Güzel

    Comment

    • rowe_newsgroups

      #3
      Re: Global Variable Ends Up as Nothing in Form

      On Nov 19, 9:28 am, "Fred Block" <fblock_no_spam m...@w-systems.com>
      wrote:
      Hi All,
      >
      I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and
      I'm very excited.
      >
      Here's an issue I'm experiencing right off the starting line and cannot make
      sense of it:
      >
      I have a module (Public) and in it contains a few global variables (String
      data type). When I go to use these in a Form the value is "Nothing." Why is
      this?
      ..
      Public Module modMain
      >
      Public g_strMyPublicVa r As String
      >
      Then in the form (frmMain), the g_strMyPublicVa r does not contain the data
      as it was set in modMain.
      >
      Any insight is greatly appreciated and I thank you in advance!
      >
      Kind regards - Fred
      What is the expected value? From what you posted the value should be
      Nothing since nothing was assigned.

      If you would like to set a value right off the bat, you could do the
      following:

      //////////
      Public MyPublicVar As String = "please drop the hungarian
      notation :-)"
      /////////

      I also hate modules (just my personal preference) and would recommend
      you either switch to a static class or implement a singleton class to
      expose the properties.

      Thanks,

      Seth Rowe [MVP]

      Comment

      • sloan

        #4
        Re: Global Variable Ends Up as Nothing in Form


        Here are a couple of hints since you're new:

        Stop using the Hungarian Notation.

        Static (shared in VB.NET) variables might be a little better than Global
        Variables. (Aka, stop with the VB6 global variable concept).

        ............


        Since you're new, here is a "extra" for you, not related to your question:

        http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry




        "Fred Block" <fblock_no_spam ming@w-systems.comwrot e in message
        news:ugFMhMlSJH A.4524@TK2MSFTN GP06.phx.gbl...
        Hi All,
        >
        I'm an experienced VB6 developer and now starting (newbee) with VB 2008
        and I'm very excited.
        >
        Here's an issue I'm experiencing right off the starting line and cannot
        make sense of it:
        >
        I have a module (Public) and in it contains a few global variables (String
        data type). When I go to use these in a Form the value is "Nothing." Why
        is this?
        ..
        Public Module modMain
        >
        Public g_strMyPublicVa r As String
        >
        Then in the form (frmMain), the g_strMyPublicVa r does not contain the data
        as it was set in modMain.
        >
        >
        Any insight is greatly appreciated and I thank you in advance!
        >
        Kind regards - Fred
        >
        >

        Comment

        • Fred Block

          #5
          Re: Global Variable Ends Up as Nothing in Form

          Hi Onur and thanks!

          I found the issue to be that I "accidental ly" had the same global variable
          in two different modules. VB did not complain however which now puzzles me.

          When the code ran I was trying to create a file in a path that did not exist
          (System.IO.Dire ctoryNotFoundEx ception)...

          I was using: sw = New StreamWriter(st rFile, True)

          Is there a parameter I can use or some other way to have the path ctraed if
          it does not exist?

          Thanks again! - Fred

          ---------------------------------------
          "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
          news:26f30f31-8ac8-476c-8d7d-88a283f999ab@v1 3g2000pro.googl egroups.com...
          On Nov 19, 4:28 pm, "Fred Block" <fblock_no_spam m...@w-systems.com>
          wrote:
          Hi All,
          >
          I'm an experienced VB6 developer and now starting (newbee) with VB 2008
          and
          I'm very excited.
          >
          Here's an issue I'm experiencing right off the starting line and cannot
          make
          sense of it:
          >
          I have a module (Public) and in it contains a few global variables (String
          data type). When I go to use these in a Form the value is "Nothing." Why
          is
          this?
          ..
          Public Module modMain
          >
          Public g_strMyPublicVa r As String
          >
          Then in the form (frmMain), the g_strMyPublicVa r does not contain the data
          as it was set in modMain.
          >
          Any insight is greatly appreciated and I thank you in advance!
          >
          Kind regards - Fred
          In your post, you didn't assign any value to "g_strMyPublicV ar", thus
          you may get a null refence exception.

          If you do something like this, you won't get an exception.

          ' In your module, assign a value to "g_strMyPublicV ar"
          Public Module modMain
          ' eg: Assign value "foo"
          Public g_strMyPublicVa r As String = "foo"
          End Module

          or assign a value to "g_strMyPublicV ar" in your form(frmMain) inside a
          method:

          ' In your form, that'll work
          Public Class Form1
          Sub mymethod()
          g_strMyPublicVa r = "foo"
          End Sub
          End Class

          ' That won't work, if you assign value at class-level
          ' in your form
          Public Class Form1
          g_strMyPublicVa r = "foo"
          End Class

          ....And as you pointed, you are declaring it in your module so,
          assigning a value to "g_strMyPublicV ar" in your module should work
          when you call it within your form.

          Hope this helps,

          Onur Güzel


          Comment

          • Tom Shelton

            #6
            Re: Global Variable Ends Up as Nothing in Form

            On 2008-11-19, Fred Block <fblock_no_spam ming@w-systems.comwrot e:
            Hi Onur and thanks!
            >
            I found the issue to be that I "accidental ly" had the same global variable
            in two different modules. VB did not complain however which now puzzles me.
            >
            Well, actually, it's legal. The full name of a variable in a module is
            essentilally, namespace.modul ename.variablen ame. So, a variable in a
            different module has a different name...

            --
            Tom Shelton

            Comment

            • Michel Posseth  [MCP]

              #7
              Re: Global Variable Ends Up as Nothing in Form

              Variables. (Aka, stop with the VB6 global variable concept).

              I dare to even go a step further,, in my code you wil not find anny shared
              keywords
              it is verry easy to just wrap all needed parameters in a parameter class and
              pass this back and forward to constructors of classes that need these
              parameters

              for small projects it is enough to create a base class with the required
              properties

              regards

              Michel



              "sloan" <sloan@ipass.ne tschreef in bericht
              news:uUP0zmmSJH A.5860@TK2MSFTN GP02.phx.gbl...
              >
              Here are a couple of hints since you're new:
              >
              Stop using the Hungarian Notation.
              >
              Static (shared in VB.NET) variables might be a little better than Global
              Variables. (Aka, stop with the VB6 global variable concept).
              >
              ...........
              >
              >
              Since you're new, here is a "extra" for you, not related to your question:
              >
              http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry
              >
              >
              >
              >
              "Fred Block" <fblock_no_spam ming@w-systems.comwrot e in message
              news:ugFMhMlSJH A.4524@TK2MSFTN GP06.phx.gbl...
              >Hi All,
              >>
              >I'm an experienced VB6 developer and now starting (newbee) with VB 2008
              >and I'm very excited.
              >>
              >Here's an issue I'm experiencing right off the starting line and cannot
              >make sense of it:
              >>
              >I have a module (Public) and in it contains a few global variables
              >(String data type). When I go to use these in a Form the value is
              >"Nothing." Why is this?
              >..
              >Public Module modMain
              >>
              >Public g_strMyPublicVa r As String
              >>
              >Then in the form (frmMain), the g_strMyPublicVa r does not contain the
              >data as it was set in modMain.
              >>
              >>
              >Any insight is greatly appreciated and I thank you in advance!
              >>
              >Kind regards - Fred
              >>
              >>
              >
              >

              Comment

              • kimiraikkonen

                #8
                Re: Global Variable Ends Up as Nothing in Form

                On Nov 19, 7:11 pm, "Fred Block" <fblock_no_spam m...@w-systems.com>
                wrote:
                Hi Onur and thanks!
                When the code ran I was trying to create a file in a path that did not exist
                (System.IO.Dire ctoryNotFoundEx ception)...
                >
                I was using: sw = New StreamWriter(st rFile, True)
                >
                Is there a parameter I can use or some other way to have the path ctraed if
                it does not exist?
                >
                Thanks again! - Fred
                >
                Hi Fred,
                For the question about checking path, when you use StreamWriter's
                constructor specifying a path, new file is created even the file does
                not exist in the path that you passed. If it exists, it's
                overwritten.

                See here:


                But as you get DirectoryNotFou nd Exception, it means that file's
                "directory" specified in StreamWriter's constructor, is invalid. You
                need to enter correct directory, at least.

                Additionaly, if you still need to check if a directory is available,
                you can use:
                "My.Computer.Fi leSystem.Direct oryExists" in a if-then conditional to
                determine.

                See it here:


                Hope this helps,

                Onur Güzel




                Comment

                • rowe_newsgroups

                  #9
                  Re: Global Variable Ends Up as Nothing in Form

                  On Nov 19, 12:34 pm, "Michel Posseth [MCP]" <M...@posseth.c omwrote:
                  Variables.  (Aka, stop with the VB6 global variable concept).
                  >
                  I dare to even go a step further,,  in my code you wil not find anny shared
                  keywords
                  it is verry easy to just wrap all needed parameters in a parameter class and
                  pass this back and forward to constructors of classes that need these
                  parameters
                  >
                  for small projects it is enough to create a base class with the required
                  properties
                  >
                  regards
                  >
                  Michel
                  >
                  "sloan" <sl...@ipass.ne tschreef in berichtnews:uUP 0zmmSJHA.5860@T K2MSFTNGP02.phx .gbl...
                  >
                  >
                  >
                  Here are a couple of hints since you're new:
                  >
                  Stop using the Hungarian Notation.
                  >
                  Static (shared in VB.NET) variables might be a little better than Global
                  Variables.  (Aka, stop with the VB6 global variable concept).
                  >
                  ...........
                  >
                  Since you're new, here is a "extra" for you, not related to your question:
                  >
                  http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry
                  >
                  "Fred Block" <fblock_no_spam m...@w-systems.comwrot e in message
                  news:ugFMhMlSJH A.4524@TK2MSFTN GP06.phx.gbl...
                  Hi All,
                  >
                  I'm an experienced VB6 developer and now starting (newbee) with VB 2008
                  and I'm very excited.
                  >
                  Here's an issue I'm experiencing right off the starting line and cannot
                  make sense of it:
                  >
                  I have a module (Public) and in it contains a few global variables
                  (String data type). When I go to use these in a Form the value is
                  "Nothing." Why is this?
                  ..
                  Public Module modMain
                  >
                  Public g_strMyPublicVa r As String
                  >
                  Then in the form (frmMain), the g_strMyPublicVa r does not contain the
                  data as it was set in modMain.
                  >
                  Any insight is greatly appreciated and I thank you in advance!
                  >
                  Kind regards - Fred
                  I agree, whenever plausible I try to put the properties into an
                  instanced property.

                  In many of the large applications I work on, I prefer to use a
                  constructor injection pattern and then have an IoC (inversion of
                  control) container shove a settings class into any class that requires
                  it. Takes a bit of time to get used to, but it is extremely powerful
                  once it's implemented.

                  Thanks,

                  Seth Rowe [MVP]

                  Comment

                  • Fred Block

                    #10
                    Re: Global Variable Ends Up as Nothing in Form

                    Thanks again Onur - this is exactly what I did to get it to work.

                    Regards - Fred

                    ------------------------------------
                    "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
                    news:1620ac85-1597-492d-adae-0730a128d64a@c2 2g2000prc.googl egroups.com...
                    On Nov 19, 7:11 pm, "Fred Block" <fblock_no_spam m...@w-systems.com>
                    wrote:
                    Hi Onur and thanks!
                    When the code ran I was trying to create a file in a path that did not
                    exist
                    (System.IO.Dire ctoryNotFoundEx ception)...
                    >
                    I was using: sw = New StreamWriter(st rFile, True)
                    >
                    Is there a parameter I can use or some other way to have the path ctraed
                    if
                    it does not exist?
                    >
                    Thanks again! - Fred
                    >
                    Hi Fred,
                    For the question about checking path, when you use StreamWriter's
                    constructor specifying a path, new file is created even the file does
                    not exist in the path that you passed. If it exists, it's
                    overwritten.

                    See here:


                    But as you get DirectoryNotFou nd Exception, it means that file's
                    "directory" specified in StreamWriter's constructor, is invalid. You
                    need to enter correct directory, at least.

                    Additionaly, if you still need to check if a directory is available,
                    you can use:
                    "My.Computer.Fi leSystem.Direct oryExists" in a if-then conditional to
                    determine.

                    See it here:


                    Hope this helps,

                    Onur Güzel





                    Comment

                    • Fred Block

                      #11
                      Re: Global Variable Ends Up as Nothing in Form

                      Hi Seth,

                      Why is hungarian notation not a suggested habit in .NET? Doesn;t it make
                      coding easier to read? If there is a newer scheme, please share this with
                      me. I'm very teachable and want to do this right the first time.

                      VB 2008 book suggestions?

                      Thanks in advance to you Seth (and all others answering my thread).

                      Regards - Fred

                      ------------------------------------------------

                      //////////
                      Public MyPublicVar As String = "please drop the hungarian
                      notation :-)"
                      /////////

                      I also hate modules (just my personal preference) and would recommend
                      you either switch to a static class or implement a singleton class to
                      expose the properties.

                      Thanks,

                      Seth Rowe [MVP]



                      Comment

                      • sloan

                        #12
                        Re: Global Variable Ends Up as Nothing in Form



                        I agree. Using a small parameter-wrapper class is the way to go. It takes
                        disjoint properties and adds some cohesion to them.


                        One idea that a former colleague showed me...was to take this small wrapper
                        class...and put it into a "DataStore" (for a winforms app, it was basically
                        a singleton where you could keep/add/remove some objects).

                        The main form would create and put the small parameter-wrapper class into
                        the DataStore (using a unique key).

                        The DataStore would pull the small-wrapper-parameter class (actually remove
                        it at the same time)....using the same unique key.

                        This way.....the form could be instantiated from anywhere. Just "put into
                        the DataStore", then call the form.

                        Either way is good, just throwing a different nugget out there.





                        "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
                        news:e4AXN0mSJH A.1164@TK2MSFTN GP02.phx.gbl...
                        >Variables. (Aka, stop with the VB6 global variable concept).
                        >
                        I dare to even go a step further,, in my code you wil not find anny
                        shared keywords
                        it is verry easy to just wrap all needed parameters in a parameter class
                        and pass this back and forward to constructors of classes that need these
                        parameters
                        >
                        for small projects it is enough to create a base class with the required
                        properties
                        >
                        regards
                        >
                        Michel
                        >
                        >
                        >
                        "sloan" <sloan@ipass.ne tschreef in bericht
                        news:uUP0zmmSJH A.5860@TK2MSFTN GP02.phx.gbl...
                        >>
                        >Here are a couple of hints since you're new:
                        >>
                        >Stop using the Hungarian Notation.
                        >>
                        >Static (shared in VB.NET) variables might be a little better than Global
                        >Variables. (Aka, stop with the VB6 global variable concept).
                        >>
                        >...........
                        >>
                        >>
                        >Since you're new, here is a "extra" for you, not related to your
                        >question:
                        >>
                        >http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!234.entry
                        >>
                        >>
                        >>
                        >>
                        >"Fred Block" <fblock_no_spam ming@w-systems.comwrot e in message
                        >news:ugFMhMlSJ HA.4524@TK2MSFT NGP06.phx.gbl.. .
                        >>Hi All,
                        >>>
                        >>I'm an experienced VB6 developer and now starting (newbee) with VB 2008
                        >>and I'm very excited.
                        >>>
                        >>Here's an issue I'm experiencing right off the starting line and cannot
                        >>make sense of it:
                        >>>
                        >>I have a module (Public) and in it contains a few global variables
                        >>(String data type). When I go to use these in a Form the value is
                        >>"Nothing." Why is this?
                        >>..
                        >>Public Module modMain
                        >>>
                        >>Public g_strMyPublicVa r As String
                        >>>
                        >>Then in the form (frmMain), the g_strMyPublicVa r does not contain the
                        >>data as it was set in modMain.
                        >>>
                        >>>
                        >>Any insight is greatly appreciated and I thank you in advance!
                        >>>
                        >>Kind regards - Fred
                        >>>
                        >>>
                        >>
                        >>
                        >
                        >

                        Comment

                        • rowe_newsgroups

                          #13
                          Re: Global Variable Ends Up as Nothing in Form

                          Why is hungarian notation not a suggested habit in .NET? Doesn;t it make
                          coding easier to read? If there is a newer scheme, please share this with
                          me. I'm very teachable and want to do this right the first time.


                          I prefer to just use names that make sense, no special notation at
                          all. If your methods are short and concise and have a single
                          responsibility, most likely there will be few variables that are
                          easily understood by assignments, and special notations aren't
                          required. I am also an advocate against using abbreviations in
                          variable names, as they hurt readability (unless they are truly
                          accepted acronyms like "guid" for globally unique identifier).

                          Most times I end up using the type name with a lowercase first letter,
                          unless a more specific name is required.

                          Dim notificationSer vice As New NotificationSer vice()
                          Dim dataStore As IDataStore = GetCustomerData Store()
                          Dim message As String = "The name 'string' wouldn't make sense here"

                          Later versions of .NET added in implicit typing which can shorten
                          these declarations. However due to the confusion between implicitly
                          typed variables and the rubbish done in VBScript, I dislike it. I much
                          prefer the C# approach of using the 'var' keyword. Below are the same
                          samples (if you desire to use the feature)

                          Dim notificationSer vice = New NotificationSer vice()
                          Dim dataStore = GetCustomerData Store()
                          Dim message = "I love putting messages inside strings"
                          VB 2008 book suggestions?
                          Book recommendations have never been my strong suit, mainly because I
                          don't read many (shame, shame I know). My recommendations would be to
                          hit the library and see if they have any, or even better try to find a
                          local .NET university or other training event. Being involved in the
                          community is the best way to learn.

                          Thanks,

                          Seth Rowe [MVP]



                          Comment

                          • rowe_newsgroups

                            #14
                            Re: Global Variable Ends Up as Nothing in Form

                            My recommendations would be to
                            hit the library and see if they have any, or even better try to find a
                            local .NET university or other training event.
                            I should clarify, I didn't mean going to a University and taking a
                            college class, .NET universities are sessions about new Microsoft
                            features etc:



                            Not sure if they have any near you, but it's worth checking out. You
                            might also look up your local Microsoft Developer Evangelists and
                            contact them for upcoming events (USA thing only I believe, so it
                            might not be applicable to you):



                            Thanks,

                            Seth Rowe [MVP]


                            Comment

                            • Fred Block

                              #15
                              Re: Global Variable Ends Up as Nothing in Form

                              Hi Seth and thanks again!

                              Point taken and I found just about all I needed in the MSDN I installed here
                              on the topic. This all makes sense and before I get ANY further with this
                              first .NET project I've taken on, I'll undo the old VB6 notations I was
                              using. A lot has changed!

                              Regards - Fred
                              -----------------------
                              "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                              news:ac878900-ed6b-4d51-b278-aab12fd95605@s9 g2000vbp.google groups.com...
                              >Why is hungarian notation not a suggested habit in .NET? Doesn;t it make
                              >coding easier to read? If there is a newer scheme, please share this with
                              >me. I'm very teachable and want to do this right the first time.
                              >

                              >
                              I prefer to just use names that make sense, no special notation at
                              all. If your methods are short and concise and have a single
                              responsibility, most likely there will be few variables that are
                              easily understood by assignments, and special notations aren't
                              required. I am also an advocate against using abbreviations in
                              variable names, as they hurt readability (unless they are truly
                              accepted acronyms like "guid" for globally unique identifier).
                              >
                              Most times I end up using the type name with a lowercase first letter,
                              unless a more specific name is required.
                              >
                              Dim notificationSer vice As New NotificationSer vice()
                              Dim dataStore As IDataStore = GetCustomerData Store()
                              Dim message As String = "The name 'string' wouldn't make sense here"
                              >
                              Later versions of .NET added in implicit typing which can shorten
                              these declarations. However due to the confusion between implicitly
                              typed variables and the rubbish done in VBScript, I dislike it. I much
                              prefer the C# approach of using the 'var' keyword. Below are the same
                              samples (if you desire to use the feature)
                              >
                              Dim notificationSer vice = New NotificationSer vice()
                              Dim dataStore = GetCustomerData Store()
                              Dim message = "I love putting messages inside strings"
                              >
                              >VB 2008 book suggestions?
                              >
                              Book recommendations have never been my strong suit, mainly because I
                              don't read many (shame, shame I know). My recommendations would be to
                              hit the library and see if they have any, or even better try to find a
                              local .NET university or other training event. Being involved in the
                              community is the best way to learn.
                              >
                              Thanks,
                              >
                              Seth Rowe [MVP]

                              >
                              >

                              Comment

                              Working...