Subject: Optional prameters and C#?
Posted By: NewTitle2007 Post Date: 8/5/2007 7:35:34 AM
Hi,

In the first chapter there is a metho with the signture
Public Shared Function GetContactsByFirstLetterAsCollection(ByVal DiaryId As Integer, Optional ByVal FirstLetterOfSurname As Char = "*") As ContactCollection

There is no such a behavior in C# so I have written
public static ContactCollection GetContactsByFirstLetterAsCollection(int diaryId, char FirstLetterOfSurname)
    {
        if (FirstLetterOfSurname == 0)
            FirstLetterOfSurname = '*';

I'm not sure that this is the best solution, any ideas?

Reply By: Imar Reply Date: 8/5/2007 11:07:31 AM
Hi there,

IMO, it's cleaner to create an overload that calls the initial method passing in the default value. E.g.:

public static ContactCollection GetContactsByFirstLetterAsCollection(int diaryId)
{
  return GetContactsByFirstLetterAsCollection(diaryId, '*');
}

This way, calling code can skip the firstLetterOfSurname param by simply calling something like:

  GetContactsByFirstLetterAsCollection(5);

while you can still provide a firstLetterOfSurname by calling the other overload:

  GetContactsByFirstLetterAsCollection(5, 's');


Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: NewTitle2007 Reply Date: 8/5/2007 12:17:52 PM
Yes it's better :) thanks


Go to topic 63407

Return to index page 2
Return to index page 1