|
-
Jun 19th, 2007, 10:53 PM
#1
Thread Starter
Frenzied Member
not understanding parameters
Hello guys, I am very bad at understanding the parameter things. Everytime I coded up codes from online tutorial, I ended up "huh?" why that param goes there, why the developer declares/knows there is a param supposed to go there, and so on. Are there any tricks/tips for understanding this ? or tutorial on this?
Thanks
-
Jun 20th, 2007, 07:31 AM
#2
Re: not understanding parameters
Procedure or Function can have (but not necesary) some arguments (or parameters for that matter) that could be mandatory or optional.
Each argument has an Explicitly defined type (like Integer, String, etc) or if not then it's a VAriant.
If Optional keyword is not specified in the procedure header then it's a mandatory argument and you must pass that and also it must be of type expected.
Here is an example:
Code:
Private Function Test(parm1 As Double, Optional parm2 As Integer = 0) As Double
Dim lResult As Double
lResult = Sqr(parm1)
If parm2 <> Then
lResult = lResult ^ parm2
End If
End Sub
'to call that function you must pass at least one argument (parm1) because it is not optional
'to do that you may pass number directly or declare a variable but it has to be numeric as well
'however if argument is defined as Integer and you try to pass a Long you will get an error...
Dim myValue As Double
Dim myResult As Double
myValue = 123.45
myResult = Test(myValue)
'or
myResult = Test(myValue, 3)
... and so on...
Executing any api function is basically the same - you need to see how function is defined and pass the same number of arguments and of the same types as they are defined in the function's header.
I thnk you need to get one of these books.
Last edited by RhinoBull; Jun 20th, 2007 at 07:37 AM.
-
Jun 25th, 2007, 08:45 PM
#3
Re: not understanding parameters
Try thinking of the API from the API developer's perspective. To make an API, they first must start making a library file (like gdi32.dll) that does something general (gdi32 is a graphics library file, with functions to draw shapes and such). Each API call in that library is like a function call in your own program, where the API developer decides what arguments and memory structures are needed for the function to succeed.
For example, take the Pie API call:
Code:
Declare Function Pie Lib "gdi32" Alias "Pie" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long, ByVal X4 As Long, ByVal Y4 As Long) As Long
This function is used to draw a pie-shape to a picture box, printer, etc (it'll work with anything with an hdc property, which means the control or object been specially programmed to work with these gdi32 functions).
All the X's and Y's have special meanings to the API call, just like if you wrote the function in your own program. For example, the X1 argument "specifies the x-coordinate of the upper-left corner of the bounding rectangle."
How do people who use the API's know how to declare them, and what each argument is for? And how do they know how to use them? They look up the API call's documentation. Personally, I use the API-Guide since I'm used to it. You can get it from the now-defunct Allapi. Also, you can look them up on MSDN. The Api-Guide includes many examples and code that can be modified slightly for your own purposes, and you can also get code from here or other sources online.
Some API's are really complex, like the PrintDialog call:
Code:
Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
It has only one argument, but it's a user-defined type that needs to be defined in a very specific manner in your program for any variables you pass to the PrintDialog function to work. Actually using this function effectively takes dozens of lines of code and several extra API calls. To be honest, I never get into programming calls that are that complex by myself, since someone will probably have written it already.
But yeah, that book is probably a good idea for really understanding the API. Hope this helps some.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|