PDA

Click to See Complete Forum and Search --> : Declaring API, public?


Geespot
Nov 2nd, 2001, 09:27 AM
I have some graphic api declarations in a module, i had to declare them like


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?

Geespot
Nov 2nd, 2001, 09:28 AM
Ooooops :rolleyes:

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 )

jim mcnamara
Nov 2nd, 2001, 09:33 AM
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.

Hack
Nov 2nd, 2001, 09:42 AM
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.

James Stanich
Nov 2nd, 2001, 11:30 AM
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...

Hack
Nov 2nd, 2001, 06:22 PM
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.

James Stanich
Nov 2nd, 2001, 09:52 PM
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...

jim mcnamara
Nov 2nd, 2001, 10:09 PM
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.

Megatron
Nov 3rd, 2001, 10:32 AM
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.

da_silvy
Nov 5th, 2001, 07:53 AM
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 :eek:

Megatron
Nov 5th, 2001, 02:40 PM
A Form doesn't accept either of the following:

Declare Function...
Public Declare Function...

But it does accept

Private Declare Function

da_silvy
Nov 5th, 2001, 11:16 PM
Ahh ok.

I thought that it accepted Declare Function