473,795 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# properties... no parameters

Hi,

I'm trying to write a class that inherits from
System.Collecti ons.CollectionB ase, 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 "myCollecti on[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 5610
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.L ist[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****@earthta lk.com> wrote in message
news:vl******** ****@corp.super news.com...
Hi,

I'm trying to write a class that inherits from
System.Collecti ons.CollectionB ase, 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 "myCollecti on[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.MO RE.SPAM.bigfoot .com> wrote in message
news:eU******** ******@TK2MSFTN GP10.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.L ist[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.MO RE.SPAM.bigfoot .com> wrote in message
news:eU******** ******@TK2MSFTN GP10.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.L ist[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****@earthta lk.com> wrote in message
news:vl******** ****@corp.super news.com...
Hi,

I'm trying to write a class that inherits from
System.Collecti ons.CollectionB ase, 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 "myCollecti on[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******** ******@TK2MSFTN GP10.phx.gbl...
"Rob Windsor" <rw******@NO.MO RE.SPAM.bigfoot .com> wrote in message
news:eU******** ******@TK2MSFTN GP10.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.L ist[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.WriteLi ne (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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.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.WriteLi ne (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(ma sk As ItemMask, startIndex As
Integer) As MaskedItemItera tor
Get
Return m_itemIterator. GetIteratorFrom Mask(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.co m>
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****@earthta lk.com> wrote in message
news:vl******** ****@corp.super news.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.MO RE.SPAM.bigfoot .com> wrote in message
news:eU******** ******@TK2MSFTN GP10.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.L ist[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****@earthta lk.com> wrote in message
news:vl******** ****@corp.super news.com...
Hi,

I'm trying to write a class that inherits from
System.Collecti ons.CollectionB ase, 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 "myCollecti on[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
391
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
3729
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 InvokeMember for ranges and values, ie: Parameters = new Object;
1
1683
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
1670
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 populated by a data-driven loop. I need a few pointers to get me started in the right direction. I'm pretty sure that I need a default Item property but I'm not sure how to create that or index the other properties. A short code example is below ...
12
1765
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 f1 = lngf1
7
1336
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 compile: T.MyCustomMethod() The compiler will tell me that it's not sure that the generic type parameters (<T>) will contain such method and will requires me to constrain the generic type parameters.
21
23991
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 get/set? Thanks.
0
1276
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 at design time This is the code for the sub InitializeComponent() <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent() Me.OleDbDADVD = New System.Data.OleDb.OleDbDataAdapter Me.OleDbDeleteCommand1 = New...
5
1456
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 I write the function to accept these as parameters in a method, or should I make them properties? Can I make certain properties required?
1
1898
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): <%@ Page Language="VB" AutoEventWireup="true" Trace="true" CodeFile="TakeSurveyTest.aspx.vb" Inherits="TakeSurveyTest" %> .... <div visible="true" runat="server"> <asp:PlaceHolder ID="ContactInfo" runat="server"/<br />
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.