Results 1 to 12 of 12

Thread: Declaring API, public?

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    Declaring API, public?

    I have some graphic api declarations in a module, i had to declare them like

    VB Code:
    1. Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long

    if i removed the public at the start, the program would not function properly.

    Why is this?

  2. #2

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Ooooops

    my bad, i realized what was wrong, it wasnt nuthink to do with the declares but some constants.

    While im at it then, whats the difference at putting a public infront? ( when its in a module )

  3. #3
    jim mcnamara
    Guest
    Anything (function, sub, datatype) that is Public in a module can be referenced from any other form or module.

    Private means that nobody outside of the module can see it.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    If you don't preface the Declare Function Yada with Public (in a module) or Private (in a form) VB can get confused about what you are talking about.

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Public (in a module)
    This is generally a rule of thumb I try to use. This way you can call it from wherever you need it it your program...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I like tight code. Be judicious in the use of Public anythings. I've seen variables declared as Public, and used on one form, or even worse, in one sub routine/function. A good, general rule of thumb is to, as you are developing your project, declare your variables at the procedure level. Then, if you find yourself needing them in other procedures, move their declaration to the form level.

    There are things that need to be exposed to the entire project, and thus need to be placed in a module, and declared Public. However, there aren't that many.

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    There are things that need to be exposed to the entire project, and thus need to be placed in a module, and declared Public. However, there aren't that many.
    That many? I use global settings for file, path, connection strings to databases, user name throughout program , along with their level of access, global arrays, alot of stuff...

    Maybe I am wrong, but I like to be able to access my info from wherever in the project I might need it...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  8. #8
    jim mcnamara
    Guest
    Public variables & especially Public Subs and Functions are necessary.

    Without going into my age - suffice it to say that globals are a necessary evil - and I've seen them for LOOONG time.

    You can abstract data to the point you make the program hard to understand & maintain, even if it supposedly makes 'tighter code'.
    Redundant functions are a REAL NO-NO, hence the need for Public Functions.

    In the code I've seen over the years - good production code, about 5% of the variables are global. Formatting functions, I/O functions, and general purpose functions also fall into the public area.

  9. #9
    Megatron
    Guest
    Originally posted by Hack
    If you don't preface the Declare Function Yada with Public (in a module) or Private (in a form) VB can get confused about what you are talking about.
    VB won't get confused, rather, the programmer will. If you don't specifically declare a function/sub with a scope modifier, VB will default it to public.

  10. #10
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    WHy does a Form accept

    Declare Function yadayadayada then?

    As it doesn't accept Public Declare Function, but will accept Private...
    ?

    Wouldn't the form reject Declare??

    i've confused myself

  11. #11
    Megatron
    Guest
    A Form doesn't accept either of the following:
    Code:
    Declare Function...
    Public Declare Function...
    But it does accept
    Code:
    Private Declare Function

  12. #12
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Ahh ok.

    I thought that it accepted Declare Function

Posting Permissions

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



Click Here to Expand Forum to Full Width