πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #95

Re: Indexed properties

#95
From: Joanna Carter [TeamB]
Date: Sat, 08 Apr 2006 07:54
57 lines
2496 bytes
"Marc Rohloff [TeamB]" <"on request"> a οΏ½crit dans le message de news: 
1sewcl5nubmpn.dlg@dlg.marcrohloff.com...

| On 7 Apr 2006 03:01:54 -0700, Murdoc wrote:
|
| > So only initialize the collection instance if it is necessary.
| >
| > public void add(Object o)
| > {
| >     if (_collection == null)
| >         _collection = new Collection();
| >
| >     _collection.add(o);
| > }
|
| You could also do this in the property which retrieves your collection
| object.

And if you write a private read-only property, you don't have to repeat the 
same checking code everywhere.

private Collection InnerCollection
{
  get
  {
    if (collection == null)
      collection = new Collection();
    return collection;
  }
}

Then your code does things like this :

public void Add(object o)
{
  InnerCollection.Add(o);
}

public object this[int index]
{
  return InnerCollection[index];
}

public void Remove(object o)
{
  InnerCollection.Remove(o);
}

Of course, the problem that this approach brings is that you would be 
creating a collection just to find out that the indexer or the Remove method 
ends up talking to an empty, newly created, collection.

Joanna

-- 
Joanna Carter [TeamB]
Consultant Software Engineer 
Technical Details
Message-ID: <44376c4a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.code-werk.net!open-news-network.org!news.soft-gems.net!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail
References: <442dc6a5$1@newsgroups.borland.com> <442e3f8a$1@newsgroups.borland.com> <1gx6nf1imch9z$.dlg@dlg.marcrohloff.com> <44363557$1@newsgroups.borland.com> <xn0ekpeax9w6l000@newsgroups.borland.com> <1sewcl5nubmpn.dlg@dlg.marcrohloff.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,8168306bbf9f2550
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII
From: "Joanna Carter [TeamB]" <joanna@not.for.spam>
Newsgroups: borland.public.csharpbuilder.language.csharp
References: <442dc6a5$1@newsgroups.borland.com> <442e3f8a$1@newsgroups.borland.com> <1gx6nf1imch9z$.dlg@dlg.marcrohloff.com> <44363557$1@newsgroups.borland.com> <xn0ekpeax9w6l000@newsgroups.borland.com> <1sewcl5nubmpn.dlg@dlg.marcrohloff.com>
Subject: Re: Indexed properties
Date: Sat, 8 Apr 2006 08:54:42 +0100
Lines: 59
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 82.69.239.102
Message-ID: <44376c4a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1144482890 82.69.239.102 (8 Apr 2006 00:54:50 -0700)
X-Authenticated-User: jcarter
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.code-werk.net!open-news-network.org!news.soft-gems.net!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail