473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# properties... no parameters

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[i] = x" , as well as "x
= myCollection[i]", 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)
Nov 15 '05 #1
8 5596
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" <no****@earthtalk.com> wrote in message
news:vl************@corp.supernews.com...
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[i] = x" , as well as "x = myCollection[i]", 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)

Nov 15 '05 #2
"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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
Nov 15 '05 #3
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" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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" <no****@earthtalk.com> wrote in message
news:vl************@corp.supernews.com...
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[i] = x" , as well as

"x
= myCollection[i]", 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)


Nov 15 '05 #4
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" <ke****@NOSPAM.i2DOTcoDOTuk> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #5
Kevin Westhead <ke****@NOSPAM.i2DOTcoDOTuk> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
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
Nov 15 '05 #7
Kevin Westhead <ke****@NOSPAM.i2DOTcoDOTuk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #8
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" <no****@earthtalk.com> wrote in message
news:vl************@corp.supernews.com...
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" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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" <no****@earthtalk.com> wrote in message
news:vl************@corp.supernews.com...
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[i] = x" , as well

as "x
= myCollection[i]", 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)



Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Marcin Grzębski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
1
by: News.microsoft.com.DotNet | last post by:
Hi, Hope someone can help me out with this. I have a CC3 sharp app that needs to export data to an excel file. Problem is the version of excel can be 97 to 2003. Anyway, have been able to use...
1
by: Pablo Ricco | last post by:
Hi! I have this problem... In Visual Basic I am work with ADODB and the next code work perfectly... Dim cmd as New ADODB.Command cmd.Properties(15).Value = 2 .... But in .NET..
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
12
by: Perre Van Wilrijk | last post by:
Hi there, When I started using VB6, I used to write classes with properties and functions as following ... Private lngf1 As Long Private strf2 As String Public Property Get f1() As Long...
7
by: Rene | last post by:
We all know that we can't call custom methods or properties form generic type parameters (<T>) by default because the compiler will complain about his. For example, the following won't normally...
21
by: VMI | last post by:
WHy are the get/set properties so useful? I know they're used so that I don't directly access a variable, but why is that so useful? What would the difference be between using it directly and using...
0
by: fiaolle | last post by:
Hi I have a combobox wich I set the Datasource,Displaymember and Valuemember at design time. I'm using a Dataset's table. But when I run the form the combobox isn't filled. Can't I set properties...
5
by: Larry Bud | last post by:
I'm writing a class to create a specifically formatted fixed width file. It's 800 characters wide, consisting of approx 30 fields. So I need to pass 30 variables, maybe 10 are required. Should...
1
by: tshad | last post by:
In VB 2008, I have a user control added to the page in the PageLoad event - but the properties are causing me an error. The program (TakeSurveyTest.aspx) using the control (ContactInfo): <%@...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.