C# properties... no parameters

  • Thread starter Thread starter MC D
  • Start date Start date
M

MC D

Hi,

I'm trying to write a class that inherits from
System.Collections.CollectionBase, and I want to expose the Item property
for both Get and Set. However because C# doesn't support property
parameters, I can NOT do the following:

public TabData Item(int index)
{
get
{
return (TabData)List[index];
}
set
{
(TabData)List[index] = value;
}
}

In my code I want to be able to write "myCollection = x" , as well as "x
= myCollection", so how can this be done?

I know that you could create a function that sets it, OR returns it, but the
above methodology is so common there must be a way...

-D
(a former VB guy)
 
Rob Windsor said:
You need to use an indexer. This is KIND OF the same as a default property
in Classic VB. Here's an example

public TabData this[int index]
{
get {return (TabData)this.List[index];}
set {(TabData)this.List[index] = value;}
}

For more information check out
http://www.csharphelp.com/archives/archive140.html


In addition to Rob's comments, it's worth pointing out that you cannot have
properties with multiple parameters in C#, although you can in VB. If you
need to consume such a property from C# then you must use the get_ and set_
methods directly.
 
Thanks a bunch for the help! FYI, it is:

set{this.List[index] = (TabData)value;}

not:

set {(TabData)this.List[index] = value;}

unless I'm mistaken about that too! ;o)

Rob Windsor said:
D,

You need to use an indexer. This is KIND OF the same as a default property
in Classic VB. Here's an example

public TabData this[int index]
{
get {return (TabData)this.List[index];}
set {(TabData)this.List[index] = value;}
}

For more information check out
http://www.csharphelp.com/archives/archive140.html

--
Rob Windsor
G6 Consulting
Toronto, Canada



MC D said:
Hi,

I'm trying to write a class that inherits from
System.Collections.CollectionBase, and I want to expose the Item property
for both Get and Set. However because C# doesn't support property
parameters, I can NOT do the following:

public TabData Item(int index)
{
get
{
return (TabData)List[index];
}
set
{
(TabData)List[index] = value;
}
}

In my code I want to be able to write "myCollection = x" , as well as "x
= myCollection", so how can this be done?

I know that you could create a function that sets it, OR returns it, but the
above methodology is so common there must be a way...

-D
(a former VB guy)

 
I don't understand what you said but that inspire me the following comment
nonetheless.

1st property have no parameter.
2nd: indexer could have as many paramenter as you wish, even a variable
number

Kevin Westhead said:
Rob Windsor said:
You need to use an indexer. This is KIND OF the same as a default property
in Classic VB. Here's an example

public TabData this[int index]
{
get {return (TabData)this.List[index];}
set {(TabData)this.List[index] = value;}
}

For more information check out
http://www.csharphelp.com/archives/archive140.html


In addition to Rob's comments, it's worth pointing out that you cannot have
properties with multiple parameters in C#, although you can in VB. If you
need to consume such a property from C# then you must use the get_ and set_
methods directly.
 
Kevin Westhead said:
In addition to Rob's comments, it's worth pointing out that you cannot have
properties with multiple parameters in C#, although you can in VB. If you
need to consume such a property from C# then you must use the get_ and set_
methods directly.

Just to expand on Lloyd's comments, here is a program demonstrating an
indexer with multiple parameters:

using System;

public class Test
{
static void Main()
{
Test t = new Test();
Console.WriteLine (t[5, 6]);
}

int this[int x, int y]
{
get { return x*y; }
}
}

What you *can't* do in C# is have multiple indexers with the *same*
parameter list, which you can in VB.NET by giving them different names.
 
Jon Skeet said:
Just to expand on Lloyd's comments, here is a program demonstrating an
indexer with multiple parameters:

using System;

public class Test
{
static void Main()
{
Test t = new Test();
Console.WriteLine (t[5, 6]);
}

int this[int x, int y]
{
get { return x*y; }
}
}

What you *can't* do in C# is have multiple indexers with the *same*
parameter list, which you can in VB.NET by giving them different names.


Yes, I should have stressed that I was just talking about plain properties
rather than indexers, which was slightly OT. In otherwords I can write the
following property in VB but not in C# (although I can consume it from C#):

Public ReadOnly Property ItemIterator(mask As ItemMask, startIndex As
Integer) As MaskedItemIterator
Get
Return m_itemIterator.GetIteratorFromMask(mask, startIndex)
End Get
End Property

I'm assuming VB.NET has this feature for compatibility with VB6, whereas in
C# they are enforcing a stricter relationship between properties and class
members, hence you have to call the above property as if it was a method.
 
Kevin Westhead said:
Yes, I should have stressed that I was just talking about plain properties
rather than indexers, which was slightly OT.

In that case the word "multiple" is entirely superfluous - you can't
have properties which take parameters *at all* in C#, because a
property with a parameter basically *is* called an indexer.
In otherwords I can write the
following property in VB but not in C# (although I can consume it from C#):

<snip>

Well, you can write it in C# - you'd just call it an indexer instead. The IL
generated could be pretty much exactly the same, I believe.
I'm assuming VB.NET has this feature for compatibility with VB6, whereas in
C# they are enforcing a stricter relationship between properties and class
members, hence you have to call the above property as if it was a method.

Nope - you could make it an indexer with no problems. As I said before,
the only problem is that although you can name indexers, you can't
*use* them by name.
 
That's what you get when you try to write code in your news reader :-)
Actually you don't need to cast the object in the set so the could would
normally be

set {this.List[index] = value;}

Rob

MC D said:
Thanks a bunch for the help! FYI, it is:

set{this.List[index] = (TabData)value;}

not:

set {(TabData)this.List[index] = value;}

unless I'm mistaken about that too! ;o)

Rob Windsor said:
D,

You need to use an indexer. This is KIND OF the same as a default property
in Classic VB. Here's an example

public TabData this[int index]
{
get {return (TabData)this.List[index];}
set {(TabData)this.List[index] = value;}
}

For more information check out
http://www.csharphelp.com/archives/archive140.html

--
Rob Windsor
G6 Consulting
Toronto, Canada



MC D said:
Hi,

I'm trying to write a class that inherits from
System.Collections.CollectionBase, and I want to expose the Item property
for both Get and Set. However because C# doesn't support property
parameters, I can NOT do the following:

public TabData Item(int index)
{
get
{
return (TabData)List[index];
}
set
{
(TabData)List[index] = value;
}
}

In my code I want to be able to write "myCollection = x" , as well
as
"x
= myCollection", so how can this be done?

I know that you could create a function that sets it, OR returns it,
but
the
above methodology is so common there must be a way...

-D
(a former VB guy)

 
Back
Top