Startup properties

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

    #1

    Startup properties

    Can you store "public" variables that may be accessed from anywhere and
    still have a Form (not SubMain) as the designated startup object ?

    For example.... let's say I want all the form's Text property equal to
    "Program Name and Version"

    In a public class I tried....

    Public Class SomeVariables

    Public strProgNameVer as String = "My Name"

    End Class

    On the load of Form1 I placed

    Me.Text = strProgNameVer

    Nothing appeared in the text property of the form AND the program did not
    crash due to lack of a declaration of strProgNameVer.

    I kind of expected one or the other... either see the text, or crash due to
    errror...

    Thanks !


  • Mr. Arnold

    #2
    Re: Startup properties


    "Rob" <robc1@yahoo.co mwrote in message
    news:IqKdnSlpNa _KLsPbnZ2dnUVZ_ hGdnZ2d@comcast .com...
    Can you store "public" variables that may be accessed from anywhere and
    still have a Form (not SubMain) as the designated startup object ?
    >
    For example.... let's say I want all the form's Text property equal to
    "Program Name and Version"
    >
    In a public class I tried....
    >
    Public Class SomeVariables
    >
    Public strProgNameVer as String = "My Name"
    >
    End Class
    I don't think you can get away with it. The variable declaration has to be
    in the scope of the class being used to be seen. Apparently, all you did up
    above was make a local variable that can only be seen in the class above.

    Public Class Form1 is a class.
    >
    On the load of Form1 I placed
    >
    Me.Text = strProgNameVer
    >
    Nothing appeared in the text property of the form AND the program did not
    crash due to lack of a declaration of strProgNameVer.
    Maybe, your project doesn't have Option Explicit set to (ON), which will
    allow a none declared variable to be used, initialized to nullstring in this
    case and the will not blow up because of it or show a compile error.
    >
    I kind of expected one or the other... either see the text, or crash due
    to errror...
    >
    If you're not trying to do that below, then maybe you need to be using a
    global Variableobj to hold and access global variables.

    Public Class Form1
    Public teststr As String = "Help"

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Me.Text = teststr

    End Sub

    End Class

    Comment

    • Ray Cassick

      #3
      Re: Startup properties

      So try this:

      1) Create a Public Class Named Settings

      2) in that class create a public shared member called name as string

      Public Shared name As String = "Myname"

      3) In your form you can now use the value Settings.name and get the string
      'Myname' from it.


      "Rob" <robc1@yahoo.co mwrote in message
      news:IqKdnSlpNa _KLsPbnZ2dnUVZ_ hGdnZ2d@comcast .com...
      Can you store "public" variables that may be accessed from anywhere and
      still have a Form (not SubMain) as the designated startup object ?
      >
      For example.... let's say I want all the form's Text property equal to
      "Program Name and Version"
      >
      In a public class I tried....
      >
      Public Class SomeVariables
      >
      Public strProgNameVer as String = "My Name"
      >
      End Class
      >
      On the load of Form1 I placed
      >
      Me.Text = strProgNameVer
      >
      Nothing appeared in the text property of the form AND the program did not
      crash due to lack of a declaration of strProgNameVer.
      >
      I kind of expected one or the other... either see the text, or crash due
      to errror...
      >
      Thanks !
      >

      Comment

      • Rob

        #4
        Re: Startup properties

        Thanks Ray !!!


        "Ray Cassick" <rcassick@enter procity.comwrot e in message
        news:ORdAtZ$oHH A.4424@TK2MSFTN GP03.phx.gbl...
        So try this:
        >
        1) Create a Public Class Named Settings
        >
        2) in that class create a public shared member called name as string
        >
        Public Shared name As String = "Myname"
        >
        3) In your form you can now use the value Settings.name and get the string
        'Myname' from it.
        >
        >
        "Rob" <robc1@yahoo.co mwrote in message
        news:IqKdnSlpNa _KLsPbnZ2dnUVZ_ hGdnZ2d@comcast .com...
        >Can you store "public" variables that may be accessed from anywhere and
        >still have a Form (not SubMain) as the designated startup object ?
        >>
        >For example.... let's say I want all the form's Text property equal to
        >"Program Name and Version"
        >>
        >In a public class I tried....
        >>
        >Public Class SomeVariables
        >>
        >Public strProgNameVer as String = "My Name"
        >>
        >End Class
        >>
        >On the load of Form1 I placed
        >>
        >Me.Text = strProgNameVer
        >>
        >Nothing appeared in the text property of the form AND the program did not
        >crash due to lack of a declaration of strProgNameVer.
        >>
        >I kind of expected one or the other... either see the text, or crash due
        >to errror...
        >>
        >Thanks !
        >>
        >
        >

        Comment

        • Mr. Arnold

          #5
          Re: Startup properties


          "Ray Cassick" <rcassick@enter procity.comwrot e in message
          news:ORdAtZ$oHH A.4424@TK2MSFTN GP03.phx.gbl...
          So try this:
          >
          1) Create a Public Class Named Settings
          >
          2) in that class create a public shared member called name as string
          >
          Public Shared name As String = "Myname"
          >
          3) In your form you can now use the value Settings.name and get the string
          'Myname' from it.
          You certainly cannot get away with this in C#. I see you can do this in
          VB.net. I guess my question would be why would you do this?

          Why wouldn't you just have a Module.vb with a Public Const set or even
          Public Name as String = "Hello"?

          Comment

          • Rob

            #6
            Re: Startup properties

            Hi,

            I think I did try that, but maybe i did something else wrong... ...
            >>Why wouldn't you just have a Module.vb with a Public Const set or even
            >>Public Name as String = "Hello"?
            As to why not start with Sub Main() (which is my interpretation of what you
            are saying)...
            In VB.net - I could start out with Sub Main, however, then you lose other
            startup options.... watch the check boxes when you do.

            So, I was wanting to keep the Open Form as the first step, then access the
            public varibles whenever I want them.

            Thanks


            "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
            news:uGOAEJGpHH A.4496@TK2MSFTN GP06.phx.gbl...
            >
            "Ray Cassick" <rcassick@enter procity.comwrot e in message
            news:ORdAtZ$oHH A.4424@TK2MSFTN GP03.phx.gbl...
            >So try this:
            >>
            >1) Create a Public Class Named Settings
            >>
            >2) in that class create a public shared member called name as string
            >>
            >Public Shared name As String = "Myname"
            >>
            >3) In your form you can now use the value Settings.name and get the
            >string 'Myname' from it.
            >
            You certainly cannot get away with this in C#. I see you can do this in
            VB.net. I guess my question would be why would you do this?
            >
            Why wouldn't you just have a Module.vb with a Public Const set or even
            Public Name as String = "Hello"?
            >

            Comment

            • Mr. Arnold

              #7
              Re: Startup properties


              "Rob" <robc1@yahoo.co mwrote in message
              news:4-adnTgu4OLx1f3bn Z2dnUVZ_oWdnZ2d @comcast.com...
              Hi,
              >
              I think I did try that, but maybe i did something else wrong... ...
              >>>Why wouldn't you just have a Module.vb with a Public Const set or even
              >Public Name as String = "Hello"?
              >
              As to why not start with Sub Main() (which is my interpretation of what
              you are saying)...
              In VB.net - I could start out with Sub Main, however, then you lose other
              startup options.... watch the check boxes when you do.
              >
              So, I was wanting to keep the Open Form as the first step, then access the
              public varibles whenever I want them.
              VB is a different animal. It's always been that way starting back to VB 3.0.
              MS has kept some concepts in VB that don't lend itself towards good Oops
              programming practices, like making a Public variable visible that can be
              seen globally. One may be able to use this concept in doing Windows desktop
              development, which should be on a limited basis, and it should never be used
              in a Web solution.


              Comment

              • Ray Cassick

                #8
                Re: Startup properties

                "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
                news:u3GjF3IpHH A.3948@TK2MSFTN GP05.phx.gbl...
                >
                "Rob" <robc1@yahoo.co mwrote in message
                news:4-adnTgu4OLx1f3bn Z2dnUVZ_oWdnZ2d @comcast.com...
                >Hi,
                >>
                >I think I did try that, but maybe i did something else wrong... ...
                >>>>Why wouldn't you just have a Module.vb with a Public Const set or even
                >>Public Name as String = "Hello"?
                >>
                >As to why not start with Sub Main() (which is my interpretation of what
                >you are saying)...
                >In VB.net - I could start out with Sub Main, however, then you lose other
                >startup options.... watch the check boxes when you do.
                >>
                >So, I was wanting to keep the Open Form as the first step, then access
                >the public varibles whenever I want them.
                >
                VB is a different animal. It's always been that way starting back to VB
                3.0. MS has kept some concepts in VB that don't lend itself towards good
                Oops programming practices, like making a Public variable visible that can
                be seen globally. One may be able to use this concept in doing Windows
                desktop development, which should be on a limited basis, and it should
                never be used in a Web solution.
                >
                >
                Two points...



                1) I would have not really recommended this in a web solution. Web was not
                mentioned.



                2) The user has to be completely aware of the implication of using ANY
                global variables.



                When I went to college my professor told me that if one of my applications
                was going to use a global variable I needed a note from my mom. I hate
                talking to my mom so I never used them :)



                Anyone can write crap in any language.



                Just because VB lets you do things that maybe you should not do does not
                make it a bad language. Hell if that was the case the inventor of C should
                be shot in the head. But I am not going to get into a religious language
                argument here.



                He asked a question and I answered it.



                Rob, He is right though. Any time you are considering using a global
                variable the first thing you need to ask yourself is WHY? I have never seen
                a case where it could not be solved another way that in the end gets you
                better code that is simpler to maintain and preserves good OOP design.


                Comment

                • Mr. Arnold

                  #9
                  Re: Startup properties

                  >
                  Just because VB lets you do things that maybe you should not do does not
                  make it a bad language. Hell if that was the case the inventor of C should
                  be shot in the head. But I am not going to get into a religious language
                  argument here.
                  I never said VB was a bad language. I have used VB for many years, more
                  years that I would even care to remember. However, since VB.Net is
                  proprietary to MS and is not a standard, unlike C#.Net, then MS can take
                  liberties with VB.NET that will allow a developer to do things, because MS
                  doesn't have to answer to anyone.

                  Comment

                  • Ray Cassick

                    #10
                    Re: Startup properties

                    Got it. Might have been on the offensive too quick there :)

                    Too many years of putting up with VB Bashers :)


                    "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
                    news:uFFd1uLpHH A.4652@TK2MSFTN GP02.phx.gbl...

                    >Just because VB lets you do things that maybe you should not do does not
                    >make it a bad language. Hell if that was the case the inventor of C
                    >should be shot in the head. But I am not going to get into a religious
                    >language argument here.
                    >
                    I never said VB was a bad language. I have used VB for many years, more
                    years that I would even care to remember. However, since VB.Net is
                    proprietary to MS and is not a standard, unlike C#.Net, then MS can take
                    liberties with VB.NET that will allow a developer to do things, because MS
                    doesn't have to answer to anyone.

                    Comment

                    • Kelly Ethridge

                      #11
                      Re: Startup properties

                      I guess I don't understand why you think you can't get away with this in
                      C#. You certainly can create a public static variable in a Settings.cs
                      class and access it directly from the rest of the application.


                      Mr. Arnold wrote:
                      >
                      "Ray Cassick" <rcassick@enter procity.comwrot e in message
                      news:ORdAtZ$oHH A.4424@TK2MSFTN GP03.phx.gbl...
                      >So try this:
                      >>
                      >1) Create a Public Class Named Settings
                      >>
                      >2) in that class create a public shared member called name as string
                      >>
                      >Public Shared name As String = "Myname"
                      >>
                      >3) In your form you can now use the value Settings.name and get the
                      >string 'Myname' from it.
                      >
                      You certainly cannot get away with this in C#. I see you can do this in
                      VB.net. I guess my question would be why would you do this?
                      >
                      Why wouldn't you just have a Module.vb with a Public Const set or even
                      Public Name as String = "Hello"?
                      >

                      Comment

                      • Mr. Arnold

                        #12
                        Re: Startup properties


                        "Kelly Ethridge" <kelly@kellyeth ridge.comwrote in message
                        news:us9H5WOpHH A.5092@TK2MSFTN GP04.phx.gbl...
                        >I guess I don't understand why you think you can't get away with this in
                        >C#. You certainly can create a public static variable in a Settings.cs
                        >class and access it directly from the rest of the application.
                        >
                        lol, I have used the static keyword on a Class not a variable declaration. I
                        see that it can be set too. The use of this is just as bad in C# as it is in
                        VB with the Shared. I would most certainly not give this the light of day in
                        either language. :)

                        Comment

                        • Rob

                          #13
                          Re: Startup properties

                          Thanks for all the input folks....

                          This is a windows app, not a web app.

                          Here is why I want to use a global variable....

                          I simply want to put the name and version of the program in the text
                          property of every form. If I have 15 forms in a program, then I must
                          remember to change it in the Load of every form. How best would I
                          accomplish this ? I chose not to start out with a module, because if I
                          do, some of the startup properties must be changed (like single instance
                          app - which I want to keep selected).

                          I feel like I am going to get bashed bad for this, but here goes...

                          Why are public variables so offensive ? Does it all have to do with
                          security (maybe use a constant instead) ? Are they just as bad as using
                          Public properties ? While we are on the topic, what is the BEST way to pass
                          variable between forms ?

                          Again, thank you for all your input !!!


                          "Ray Cassick" <rcassick@enter procity.comwrot e in message
                          news:upZus$KpHH A.4032@TK2MSFTN GP02.phx.gbl...
                          "Mr. Arnold" <MR. Arnold@Arnold.c omwrote in message
                          news:u3GjF3IpHH A.3948@TK2MSFTN GP05.phx.gbl...
                          >>
                          >"Rob" <robc1@yahoo.co mwrote in message
                          >news:4-adnTgu4OLx1f3bn Z2dnUVZ_oWdnZ2d @comcast.com...
                          >>Hi,
                          >>>
                          >>I think I did try that, but maybe i did something else wrong... ...
                          >>>>>Why wouldn't you just have a Module.vb with a Public Const set or even
                          >>>Public Name as String = "Hello"?
                          >>>
                          >>As to why not start with Sub Main() (which is my interpretation of what
                          >>you are saying)...
                          >>In VB.net - I could start out with Sub Main, however, then you lose
                          >>other startup options.... watch the check boxes when you do.
                          >>>
                          >>So, I was wanting to keep the Open Form as the first step, then access
                          >>the public varibles whenever I want them.
                          >>
                          >VB is a different animal. It's always been that way starting back to VB
                          >3.0. MS has kept some concepts in VB that don't lend itself towards good
                          >Oops programming practices, like making a Public variable visible that
                          >can be seen globally. One may be able to use this concept in doing
                          >Windows desktop development, which should be on a limited basis, and it
                          >should never be used in a Web solution.
                          >>
                          >>
                          >
                          Two points...
                          >
                          >
                          >
                          1) I would have not really recommended this in a web solution. Web was not
                          mentioned.
                          >
                          >
                          >
                          2) The user has to be completely aware of the implication of using ANY
                          global variables.
                          >
                          >
                          >
                          When I went to college my professor told me that if one of my applications
                          was going to use a global variable I needed a note from my mom. I hate
                          talking to my mom so I never used them :)
                          >
                          >
                          >
                          Anyone can write crap in any language.
                          >
                          >
                          >
                          Just because VB lets you do things that maybe you should not do does not
                          make it a bad language. Hell if that was the case the inventor of C should
                          be shot in the head. But I am not going to get into a religious language
                          argument here.
                          >
                          >
                          >
                          He asked a question and I answered it.
                          >
                          >
                          >
                          Rob, He is right though. Any time you are considering using a global
                          variable the first thing you need to ask yourself is WHY? I have never
                          seen a case where it could not be solved another way that in the end gets
                          you better code that is simpler to maintain and preserves good OOP design.
                          >
                          >

                          Comment

                          • Mr. Arnold

                            #14
                            Re: Startup properties


                            "Rob" <robc1@yahoo.co mwrote in message
                            news:AsGdnVYNG7 Os9vzbnZ2dnUVZ_ ompnZ2d@comcast .com...
                            Thanks for all the input folks....
                            >
                            This is a windows app, not a web app.
                            >
                            Here is why I want to use a global variable....
                            >
                            I simply want to put the name and version of the program in the text
                            property of every form. If I have 15 forms in a program, then I must
                            remember to change it in the Load of every form. How best would I
                            accomplish this ? I chose not to start out with a module, because if I
                            do, some of the startup properties must be changed (like single instance
                            app - which I want to keep selected).
                            The proper way to to this would be to have a Help About Menu option. If you
                            go to any Windows Desktop application on your machine like Word, Excel or
                            any desktop application of that nature, you will see the Help with an About
                            where this information is keep.

                            That's kind of a standard that is used. One doesn't put this information on
                            every form. You should be able to use Google and find out how to format an
                            Help About, even if you have to go back to a VB 6 example.

                            Maybe, you can find a book or an article about the guidelines for Windows UI
                            design.


                            >
                            I feel like I am going to get bashed bad for this, but here goes...
                            >
                            Why are public variables so offensive ? Does it all have to do with
                            security (maybe use a constant instead) ? Are they just as bad as using
                            Public properties ? While we are on the topic, what is the BEST way to
                            pass variable between forms ?
                            You should try to keep the scope of a varibale *local* as much as possible,
                            IMHO. I am sure you can find pros and cons of global vs local variable
                            usage.



                            There is nothing stopping you from passing variables in a form's constructor
                            like any other class to keep the Local scope on variables.



                            There is nothing worst than trying to find out the reasons for a Global
                            variable and its intended usage as a developer just flat-out becomes lazy
                            with the usage, and they are all over the place with different meanings
                            sometimes with re-use them for a different reason along the way. :)

                            IMHO, if you need Gobal variables or even functions at times, then put them
                            in a public object.


                            Comment

                            Working...