Announcement

Collapse
No announcement yet.

Guidance Needed in VB: When NOT to use public functions

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Guidance Needed in VB: When NOT to use public functions

    I'm pretty much in my infant stages with vb. I can modify code to my tastes but have yet to complete a project from scratch. My question is pretty basic i guess so excuse my ignorance. Basically i just wanted insite on the reasons to use public/private declaration of functions. If a function is listed as private what are the repercussions of changing it to a public function?
    mp3Car.com Senior Tech Blogger (Want a product reviewed? Contact me.)
    Follow Me on Twitter or Facebook
    Live mp3Car Facebook Chat

  • #2
    Public functions/subroutines can be called from the Call statement, whereas private ones can't.....i think :P

    You can change private functions and subs to public and then call them if you want. I do this for my frontend app to manipulate play control from the Nav screen. Instead of re-writing the play/pause/next/prev/stop code i just call the sub for my command buttons on whatever module is playing (call frmMP3.cmdMain_Click()) and send the index of the button so it knows which one i'm wanting it to "press" so to speak.

    Comment


    • #3
      Your question applies to all languages that use private/public. Basically you can create an entire application using public functions if you want to. But they can be called and modified from any instance of the class.

      This can be good for intances such as the above. But this can be bad for security reasons.

      Suppose some bank program has an option to delete an account. Now you don't want this to be available for everyone to use. So you make that private deep within some class A. Then you have a public function that does some sort of authorization in class A as well. Then from some class B where it takes all the user input it calls an instance of A and then the public authorization function. The delete function is impossible to access directly because it is private. If the authorization succeeds, then the public function calls the private function.

      Just safer code. Obviously that is a really simple example, but it has good uses some times.
      Fusion Brain Version 6 Released!
      1.9in x 2.9in -- 47mm x 73mm
      30 Digital Outputs -- Directly drive a relay
      15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
      Buy now in the MP3Car.com Store

      Comment


      • #4
        It really isn't about security so much as 'self documentation' about what code is supposed to call this function. This is called 'encapsulation' and is a means to help prevent errors and make the code more maintainable.

        When YOU write something, you would want to make it private if you need to guarantee a certain state for a function to be called.

        Say you have 2 functions:
        LoadSong
        PlaySong

        now suppose that play song require that the setup that loadsong does before it can be called safely. One solution to this would be to make them both private, then make a third public function call LoadAndPlaySong which called the two in order. Of course you could always make them both public and require the person using them (even if it is only you) to remember to always do the right thing, but encapsulation in this manner makes it a little bit more clear, and also makes it harder to do the wrong thing.

        |V1 Virtual Display|

        Hard work often pays off over time, laziness always pays off right now.

        Comment


        • #5
          Public = function can be called outside the module (or form) it resides in.
          Private = can only be used within the same module/form.

          If you can, keep the functions/subs private. It's not only good for security, but also keeps things clean and keeps the bugs down.

          Comment


          • #6
            Access modifiers are not about security. It is certiainly aobut keeping things clean and helping to avoid bugs, but it is not security in any way, shape or form.

            |V1 Virtual Display|

            Hard work often pays off over time, laziness always pays off right now.

            Comment


            • #7
              Originally posted by zPilott View Post
              Access modifiers are not about security. It is certiainly aobut keeping things clean and helping to avoid bugs, but it is not security in any way, shape or form.
              It is "security" in the form of protecting certain aspects of your class from being directly accessed. On a group coding project where you write a class or two and so do like 50 other people, it is very useful.

              Maybe some function needs certain variables to be set or currently active before it is called. Now to make sure you cannot call that function at all externally, make it private. Now you make a public function that sets what is needed and then calls the private function.

              It is security in the program, but not security for the end-user.
              Fusion Brain Version 6 Released!
              1.9in x 2.9in -- 47mm x 73mm
              30 Digital Outputs -- Directly drive a relay
              15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
              Buy now in the MP3Car.com Store

              Comment


              • #8
                When in doubt, make it private.

                Keep this in mind: It's very easy to break code by making something go from public to private. You will NEVER (though, I'm sure there's some insanely ridic instance when this is false) break code by making something private public.

                Comment


                • #9
                  Originally posted by BrownieMan View Post
                  You will NEVER (though, I'm sure there's some insanely ridic instance when this is false) break code by making something private public.
                  Or the not-so-insane instance that there are two forms with a Button1 on them and the event Button1Click is public somewhere...
                  Or that Button1Click calls Play() on the video page which actually calls the public function on the music page instead...

                  Just quick examples I could see off the bat. For the record, I use all public functions and name them accordingly. IE: musicPlay, videoPlay, albumPlay, etc... Mostly because each one has different parameters but... you get the idea.
                  2001 Mustang Convertible Worklog
                  Indigo Custom Frontend (Flash/Delphi)
                  Blog

                  Qube v1.3 Now Available at the mp3Car Store!!!!!!
                  The simplest IO controller you'll ever use!

                  Comment


                  • #10
                    I never use private/protected functions either but that is also because I am a 1 man team and dont need to protect my little part of code from playing nice with others' code in a giant project where people can name things the same and not know.
                    Fusion Brain Version 6 Released!
                    1.9in x 2.9in -- 47mm x 73mm
                    30 Digital Outputs -- Directly drive a relay
                    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
                    Buy now in the MP3Car.com Store

                    Comment


                    • #11
                      Even for solo projects, there are two people to consider:
                      1) You, today, churning away on the code in question,
                      2) You, next year, going back to the same code for the first time in many months (for a bug fix, new feature, performance improvement, whatever).

                      Generally speaking, a class should have a single clear purpose, which is captured by the class name. The operations that the user of the class (you, today & you, next year) needs to understand to meet that purpose, should be public. Anything else the class does, should be private.

                      One of the key things about private functions and private member variables is that you can revise them all you want without breaking the code that uses the class (unless you introduce bugs, of course). It's only when you change the public members (or the behavior, of coures) that you have to revise the rest of the project.

                      As other said, stuff should be public if something else needs it. Otherwise, leave it private. That's a note to you-next-year that you're free to revise this without worrying too much about side-effects in other areas. It's also a note to you-next-month that the member in question isn't important if all you want to do is USE the class. It's stuff you can forget about when you're focused on other parts of the project and just want to use the class for whatever purpose it was designed for.

                      Comment


                      • #12
                        Originally posted by colin View Post
                        Or the not-so-insane instance that there are two forms with a Button1 on them and the event Button1Click is public somewhere...
                        Or that Button1Click calls Play() on the video page which actually calls the public function on the music page instead...

                        Just quick examples I could see off the bat. For the record, I use all public functions and name them accordingly. IE: musicPlay, videoPlay, albumPlay, etc... Mostly because each one has different parameters but... you get the idea.
                        I was talking about if you had a private method and then made it public. I think you misread as vice versa, which you are 100% right on.

                        Comment


                        • #13
                          wow.. its amazing that just a year ago when i started this thread i knew NOTHING about VB lol. Now i've got 5 or 6 active plugins with more to come!
                          mp3Car.com Senior Tech Blogger (Want a product reviewed? Contact me.)
                          Follow Me on Twitter or Facebook
                          Live mp3Car Facebook Chat

                          Comment

                          Working...
                          X