2

How can I write two functions with the same name with different parameter types like this:

  public
    { Public declarations }
    function MsgW(const Msg:String):Integer;
    function MsgW(const Msg:String;title:String):Integer;


function MsgW(const Msg:String;title:String):Integer;
Begin
  Result := MessageboxW(0,Pchar(Msg),Pchar(title),MB_OK);
End;

function MsgW(const Msg:String):Integer;
Begin
  Result := MessageboxW(0,Pchar(Msg),'MessageBoxW',MB_OK);
End;
dummzeuch
  • 10,975
  • 4
  • 51
  • 158
RepeatUntil
  • 2,272
  • 4
  • 32
  • 57

1 Answers1

12

Use overload directive

function MsgW(const Msg:String):Integer; overload;
function MsgW(const Msg:String;title:String):Integer; overload;

Overloading Methods

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • 1
    It seems strange that this rather perfunctory answer has received so many upvotes. – MartynA Jun 30 '15 at 19:52
  • @MartynA SO works in mysterious ways. Sometimes you bend over backwards in helping someone to get only his acceptance, sometimes you write answer in few seconds and whoa... – Dalija Prasnikar Jun 30 '15 at 19:58
  • 1
    I realise that SO works in mysterious (and increasingly depressing, imo) ways. I wasn't meaning to imply anything untoward about the +1s, I just thought the general idea of SO was that good qs and good answers percolate to the top, by dint of upvotes, and in the case of an answer like this one, I'd have hoped that voters could have maybe tempered their enthusiasm a bit more than they have. Maybe one for meta, but I'm beginning to think that what ends up on meta is a sign that users should get out more. – MartynA Jun 30 '15 at 20:07
  • @MartynA Problem begins with defining what constitutes good question and/or good answer. Some of super popular questions and answers on SO are really simple ones, so simple definitively has its place here. For instance http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java – Dalija Prasnikar Jun 30 '15 at 20:19
  • "Problem begins ..." I'm not arguing, but actually I'm not sure that it's really that hard to get a handle on how much an answer contributes to the "stock of human knowledge." Other regular Delphi contributors criticise the "sympathy upvote" (where someone seeks to cancel out a -1 in support of the questioner), quite rightly imo. This instance is just an example of another kind of problem that does no credit to SO. But anyway, I've had my say; "Correspondence closed", as they say on The Times letters page. – MartynA Jun 30 '15 at 20:33
  • 6
    FWIW, I would simply write this as one function, with a default parameter: `function MsgW(const Msg: string; const Title: string = ''): Integer;` and do without the overload. – Rudy Velthuis Jun 30 '15 at 20:53
  • 2
    @MartynA It's a well known phenomenon. People will upvote what they know to be accurate. A question like this whose answer is quite simple because the question was simple, and the answer is widely know, gets plenty upvotes because there is a large population of people who trust themselves to judge it to be correct. On the other hand, accurate answers to obscure tricky questions where there are few people with the requisite knowledge to answer tend to get fewer upvotes. – David Heffernan Jun 30 '15 at 22:37