πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Array properties in C# ?

#649
From: Joanna Carter (TeamB)
Date: Wed, 01 Sep 2004 08:16
121 lines
3709 bytes
"Craig K" <craigkenisston@hotmail.com> a οΏ½crit dans le message de news:
413556b2@newsgroups.borland.com...

> I'm porting some Delphi code to C#.
> I have a class defined like this :
>
>   TParamsList = class(TList)
>   private
>     function GetAsInteger(i: Integer): Integer;
>     function GetAsInt64(i: Integer): Int64;
>
>     function GetExprType(i: Integer): TExprType;
>     function GetParam(i: Integer): TExpToken;
>   public
>     destructor Destroy; override;
>     property Param[i: Integer]: TExpToken read GetParam;
>     property ExprType[i: Integer]: TExprType read GetExprType;
>
>     property AsInteger[i: Integer]: Integer read GetAsInteger;
>     property AsInt64[i: Integer]: Int64 read GetAsInt64;
>   end;
>
> What is the equivalent from this to C# ?
> I tried to create an indexer, by I can't declare "this" as property more
than once.
> Isn't there any equivalent code ? I'd like to avoid an array for each
property.

Take a look at the Indexed Properties Tutorial topic in the MSDN help for
C#.

Essentially you have to create a collection class for each type that you
want to have as an indexed property and then create internal references to
those classes in the main class.

So you get a calling syntax something like:

ParamsList.ExprTypes[i]
ParamsList.Params[i]
ParamsList.AsInteger[i]
ParamsList.AsInt64[i]

Although I would also suggest that you possibly change your design here and
there...

1. do not derive from a list class; instead create a class that holds an
internal list; this way you hide all the unwanted methods/properties of the
list class from users of TParamsList.

2. create a TParam class that encapsulates the info for each item:

///////////////////////
public class Params
{
  private ExpToken exprToken;

  private ExpType exprType;

  public ExpToken ExprToken
  {
  get
    {
      return exprToken;
    }
  }

  public ExpType ExprType
  {
    get
    {
      return exprType;
    }
  }

  public int AsInteger()
  {
    // code to return integer
  }

  // be warned uint64 is not a CLS compliant type
  public UInt64 AsInt64()
  {
    // code to return UInt64
  }
}
/////////////////////////////

Then create your list class:

///////////////////////////
public class ParamList
{
  private ArrayList items = new ArrayList();

  public Params this[int Idx]
  {
    get
    {
      return (Params) items[Idx];
    }
  }
}
//////////////////////////////

This way, your calling code looks like:

ParamsList[i].ExprType
ParamsList[i].ExprToken
ParamsList[i].AsInteger
ParamsList[i].AsInt64

What do you think?

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
Technical Details
Message-ID: <41358582$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <413556b2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,c22babec1f938d90
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII
From: "Joanna Carter \(TeamB\)" <joannac@btinternetxx.com>
Newsgroups: borland.public.csharpbuilder.language.csharp
References: <413556b2@newsgroups.borland.com>
Subject: Re: Array properties in C# ?
Date: Wed, 1 Sep 2004 09:16:53 +0100
Lines: 123
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 81.152.49.87
Message-ID: <41358582$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1094026626 81.152.49.87 (1 Sep 2004 01:17:06 -0700)
X-Authenticated-User: jcarter
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail