Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make this function available from my library? 2

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Ok, here's what's going on:

I've made a library of functions which all return either true or false, simple stuff like: is it a date, is a file in use that sort of thing, now if also written a function IsConnected >>>

Code:
function IsConnected: Boolean;
var flags: DWORD;
begin
Result := InternetGetConnectedState(@flags, 0);
end;

What I am wondering is, since this is going to be in a library (so I can add it to my uses list when I need it)
should I make it available like it is up there or like this:

Code:
function IsConnected(): Boolean;
var flags: DWORD;
begin
Result := InternetGetConnectedState(@flags, 0);
end;

Or doesn't it matter?

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
Heya Bobba
If I got you right then all you need is to declare your function in interface section.
Code:
unit ...
interface
...
function IsConnected(): Boolean;
...

implementation
...
function IsConnected(): Boolean;
var 
  flags: DWORD;
begin
  Result := InternetGetConnectedState(@flags, 0);
end;
...

HTH
--- McMerfy
 
yeah but i was wondering about the "()" because when I browse the net I come across people that do add them to function which dont require parametres and people who do.
Both seem to work just fine, I was wondering which is the correct one to use?

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
I suspect that people who add () to their functions have previously coded in C style languages where it is required.

Either style of coding works in Delphi and, I believe, compiles to identical code.

The functions in Borland's VCL don't seem to use ().

My recommendation would be not to include () because I think it looks ugly and adds extra clutter to your coding -but it's up to you.

Andrew
Hampshire, UK
 
It's up to you. As you've correctly noticed both ways do the job. It's just a way of organizing your code, I always ad "()" to the declarations and calls to procedures and functions when they don't have paramenters so that when listing code I could see that it's a function and not a variable. Say when you see something like:
Code:
...
  Result := IsConnected;
...
you can only guess what is IsConnected - function or variable while
Code:
...
  Result := IsConnected();
...
will have only one meaning :) Hope I've made myself clear.

Cheers
McMerfy
 
I am not convinced of McMerfy's reason for using IsConnected() in preference to IsConnected.

Suppose you have IsConnected() used in lots of places in your program and then you decide that you would rather implement IsConnected as a variable or a property (or even a constant - perhaps for testing purposes). You would have to change every occurence of IsConnected() to IsConnected.

By using IsConnected instead of IsConnected() you would hide the implementation detail. Implementation hiding is one of the cornerstones of Object Orientated Programming.

Andrew
Hampshire, UK
 
Well, I think I'll add them as it doesn't matter ... I'm an expert at confusing the crap out of myself so I think it'll be best then to add them (for me).

Suppose you have IsConnected() used in lots of places in your program and then you decide that you would rather implement IsConnected as a variable or a property (or even a constant - perhaps for testing purposes). You would have to change every occurence of IsConnected() to IsConnected.

That's not that much of a hassle with the Replace All button :)

Thank you both for your answers though! It's good to hear someone else's opinion on such matters :)

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
Bit late with my 2p but, I agree with Andrew, I never use brackets when there are no parameters, although I also code in 'C'.
I think this way is correct Pascal syntax?

Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top