Book a Demo

Author Topic: Add Indexer In Class  (Read 4916 times)

ttliu2000

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Add Indexer In Class
« on: October 21, 2005, 05:59:13 am »
Hi, I am a newbie.

Can somebody tell me how to add a (C#) indexer in a class?

I found where to add (C#) property, but indexer seems to be nowhere.

Thanks alot.  :)

mikewhit

  • EA User
  • **
  • Posts: 608
  • Karma: +0/-0
  • Accessing ....
    • View Profile
Re: Add Indexer In Class
« Reply #1 on: October 21, 2005, 07:01:45 am »
« Last Edit: October 21, 2005, 07:02:42 am by mikewhit »

ttliu2000

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Add Indexer In Class
« Reply #2 on: October 21, 2005, 07:25:21 am »
I still cannot find indexer.. ???

In C#, an indexer is defined like

public string this[int i]
{
  get {...}
  set {...}
}

it looks like a property.

I've found where to define a property, but I cannot find a place to define an indexer.

thanks..:)

mikewhit

  • EA User
  • **
  • Posts: 608
  • Karma: +0/-0
  • Accessing ....
    • View Profile
Re: Add Indexer In Class
« Reply #3 on: October 21, 2005, 08:05:05 am »
A property with multiplicity == indexer, surely ?
« Last Edit: October 21, 2005, 08:05:45 am by mikewhit »

ttliu2000

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Add Indexer In Class
« Reply #4 on: October 21, 2005, 08:14:05 am »
HI.. MikeWhit

indexer <> property with multiplicity, 'cause you can define a indexer like

public string this[string xxx]
{
   get { ... }
   set { ... }
}

I am trying to generate C# code from EA, but I find it is not possible to define indexer.

thanks..:)

Quote
A property with multiplicity == indexer, surely ?


Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Add Indexer In Class
« Reply #5 on: October 21, 2005, 04:24:38 pm »
Quote
Hi, I am a newbie.

Can somebody tell me how to add a (C#) indexer in a class?

I found where to add (C#) property, but indexer seems to be nowhere.

Thanks al lot.  :)
Hi ttliu2000,

You are right that Indexers are nowhere.  It's a bug.  Formally report it to the Sparx support email address: [email protected].

My recent research on Properties (particularly Microsoft languages) and documented in :  [size=13]Property Redux[/size] may be of interest.

Also, quoting from Microsoft VS2003:

Indexers and properties are very similar in concept, but differ in the following ways:

A property is identified by its name, whereas an indexer is identified by its signature.
A property is accessed through a simple-name (Section 7.5.2) or a member-access (Section 7.5.4), whereas an indexer element is accessed through an element-access (Section 7.5.6.2).
A property can be a static member, whereas an indexer is always an instance member.
A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of an indexer corresponds to a method with the same formal parameter list as the indexer.
A set accessor of a property corresponds to a method with a single parameter named value, whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.
It is a compile-time error for an indexer accessor to declare a local variable with the same name as an indexer parameter.
In an overriding property declaration, the inherited property is accessed using the syntax base.P, where P is the property name. In an overriding indexer declaration, the inherited indexer is accessed using the syntax base[E], where E is a comma separated list of expressions.

[end of quote]
I would have added that a Property is a Feature of a Class or Struct, whereas Indexers are defined at the Classifier level.  There can only be one Indexer with a specific signature per Classifier.

Therefore, if you want to define some Indexers, for the present you could create two Operations, both named this, with the requisite parameters.  Stereotype them «indexer».  You may be able to alter the CTF templates to make use of this.  Good luck.

NOTE: to Sparxians:  If you fix the Property mechanism you can re-use the same mechanism (but allowing for the additional parameters for Indexers).

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Add Indexer In Class
« Reply #6 on: October 23, 2005, 04:54:40 pm »
Adding a method with the stereotype of "indexer" is the supported way of creating indexers.  The name of the method is ignored when generating.

Indexer methods support the same tagged values as property methods.




attribute_nameThe name of the attribute that this method accesses.
readonlyOnly generate a get accessor.
writeonlyOnly generate a set accessor.

ttliu2000

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Add Indexer In Class
« Reply #7 on: October 23, 2005, 05:59:31 pm »
Thank you for your help.

You guys might want to make this software more user friendly...:)

Thanks again.



Quote
Adding a method with the stereotype of "indexer" is the supported way of creating indexers.  The name of the method is ignored when generating.

Indexer methods support the same tagged values as property methods.

attribute_nameThe name of the attribute that this method accesses.
readonlyOnly generate a get accessor.
writeonlyOnly generate a set accessor.


mikewhit

  • EA User
  • **
  • Posts: 608
  • Karma: +0/-0
  • Accessing ....
    • View Profile
Re: Add Indexer In Class
« Reply #8 on: October 24, 2005, 03:54:58 am »
Sorry about my C# ignorance - to me, "indexer" suggests stepping through [an array of] values, hence my original UML-centric comments regarding a multi-valued property.

A badly-named C# construct ?

The C# "indexer" looks more like a mapping property (like a Perl hash) than an "index" into something, unless you use the database meaning of index as a record-location speedup; maybe that's where it comes from ?
« Last Edit: October 24, 2005, 07:46:35 am by mikewhit »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Add Indexer In Class
« Reply #9 on: October 24, 2005, 03:22:23 pm »
It could be used in a way similar to a perl hash, but it isn't restricted to that kind of behaviour.

Take this example from http://www.csharphelp.com/archives/archive140.html
Code: [Select]
// C#: INDEXER
// Author: [email protected]

using System;
using System.Collections;

class MyClass
{
private string []data = new string[5];
public string this [int index]
{
 get
 {
  return data[index];
 }
 set
 {
  data[index] = value;
 }
}
}


class MyClient
{
public static void Main()
{
 MyClass mc = new MyClass();
 mc[0] = "Rajesh";
 mc[1] = "A3-126";
 mc[2] = "Snehadara";
 mc[3] = "Irla";
 mc[4] = "Mumbai";
Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}

It shows an indexer being used in a way that agrees with how the term indexer is described at http://www.sparxsystems.com.au/cgi-bin/yabb/YaBB.pl?board=UMLPRO;action=display;num=1129923972