Page 1 of 2 12 LastLast
Results 1 to 10 of 13

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

  1. #1
    Mo' Programming Mo' Problems
    Auto Apps:loading...
    Sonicxtacy02's Avatar
    Join Date
    Sep 2004
    Location
    Woodbridge, VA
    Posts
    8,114
    Blog Entries
    49

    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.)
    Find my plugins on the MP3Car App Store!
    Follow Me on Twitter or Facebook
    Live mp3Car Facebook Chat

  2. #2
    Variable Bitrate kamikaze2112's Avatar
    Join Date
    Feb 2005
    Location
    Barrie Ontario
    Posts
    406
    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.


  3. #3
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,052
    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

  4. #4
    Maximum Bitrate zPilott's Avatar
    Join Date
    Jul 2004
    Location
    Austin
    Posts
    509
    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.

  5. #5
    Newbie
    Join Date
    Jan 2007
    Posts
    38
    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.

  6. #6
    Maximum Bitrate zPilott's Avatar
    Join Date
    Jul 2004
    Location
    Austin
    Posts
    509
    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.

  7. #7
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,052
    Quote 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

  8. #8
    Newbie
    Join Date
    Jun 2008
    Posts
    51
    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.

  9. #9
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    Quote 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.

  10. #10
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,052
    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

Page 1 of 2 12 LastLast

Similar Threads

  1. Help needed from VB expert !
    By CdRsKuLL in forum Software & Software Development
    Replies: 4
    Last Post: 12-27-2004, 03:28 PM
  2. VB & Winamp Help Needed
    By ST34LTH in forum Software & Software Development
    Replies: 7
    Last Post: 07-21-2004, 10:13 PM
  3. VB help needed - DVDplayer- Brushed metal
    By Custommx3 in forum MediaCar
    Replies: 2
    Last Post: 06-11-2004, 01:03 AM
  4. BASIC Stamp question
    By Banderon in forum General Hardware Discussion
    Replies: 18
    Last Post: 05-07-2004, 08:33 AM
  5. Programming help needed (DirectShow & VB)
    By All4Norway in forum Software & Software Development
    Replies: 0
    Last Post: 01-06-2003, 05:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •