Properties

  • Thread starter Thread starter Nak
  • Start date Start date
N

Nak

Hi there,

I have only just started using C# after becoming more than competent
with VB.NET. I have come across something that has confused me slightly, it
regards properties.

In VB.NET it is possible to pass a value to a property (similar to an
Indexer) but without it actually being an indexer, for example

Public Property layers(ByVal iKey As Object) As Byte(,)
Get
'get an item out of a hastable object, using iKey as the key
End Get
Set(ByVal Value As Byte(,))
'set an item in a hashtable, using iKey as the key
End Set
End Property

I would have thought that you could write something very similar in C#,
by writing,

public Byte[,] layers(Object iKey)
{
get
{
'get an item out of a hastable object, using iKey as the key
}
set
{
'set an item in a hashtable, using iKey as the key
}
}

But it seems that I am very wrong, and indeed even the Hashtable class
is used slightly differently in C#, rather than using a property that would
be layed out very similar to the above you have to use an Indexer, this is
all very well but you can only have 1 indexer per class as you can't even
name the indexer.

Is the only way to get around this to have 2 methods to access the
object, 1 to set and 1 to get? Would that be why some classes have
setMyValue rather than MyValue? Thanks loads in advance

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Nak said:
In VB.NET it is possible to pass a value to a property (similar to an
Indexer) but without it actually being an indexer, for example

<snip>

Basically, this (named indexers) isn't possible in C#, unfortunately.
You can give the indexer a name for use in other languages, but you
can't have more than one indexer with a given signature, and you can't
use the names within C# code.
But it seems that I am very wrong, and indeed even the Hashtable class
is used slightly differently in C#, rather than using a property that would
be layed out very similar to the above you have to use an Indexer, this is
all very well but you can only have 1 indexer per class as you can't even
name the indexer.

Is the only way to get around this to have 2 methods to access the
object, 1 to set and 1 to get? Would that be why some classes have
setMyValue rather than MyValue? Thanks loads in advance

One thing you can do is have a property which returns something *else*
which has an indexer. It's very ugly though.
 
Hi there,
Basically, this (named indexers) isn't possible in C#, unfortunately.
You can give the indexer a name for use in other languages, but you
can't have more than one indexer with a given signature, and you can't
use the names within C# code.

Cool, well, not so cool, but I was thinking for a moment that it might have
been SharpDevelop, thanks for the help, I'll use 2 methods per read/write
indexed property. Thanks again :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Back
Top