473,804 Members | 3,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Indexed properties in .NET

Kor
Hi,

Am I right that indexed properties are implemented in VB
..NET, but NOT in C#. It seems that for C# you have to wrap
the property in a separate class, for emulating index
properties.

In addition in VB .NET you can use indexed property, but
with only an integer as indexer. So, a string as indexer (as
in VB6) will not work here. Is that right ?

Some help would be nice.

Thanks.

K.
Jul 21 '05 #1
2 5688
Am I right that indexed properties are implemented in VB
.NET, but NOT in C#. It seems that for C# you have to wrap
the property in a separate class, for emulating index
properties. You have indexers in on classes in C#.

The following snippet shows how to implement an numeric index on a class:
class ClassWithIndexe r {
private string[] data = new string[5];
public string this [int index] {
get {
return data[index];
}
set {
data[index] = value;
}
}
}
In addition in VB .NET you can use indexed property, but
with only an integer as indexer. So, a string as indexer (as
in VB6) will not work here. Is that right ?

The following snippet shows how to hide a hashtable behind an indexed
property in VB.NET.

Public Class ClassWithIndexe dProperty
Private m_data As New Hashtable
Public Property Data(ByVal key As String) As String
Get
Return CStr(m_data(key ))
End Get
Set
m_data(key)=Val ue
End Set
End Property
End Class

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Jul 21 '05 #2
"Anders Norås [MCAD]" wrote:
Am I right that indexed properties are implemented in VB
.NET, but NOT in C#. It seems that for C# you have to wrap
the property in a separate class, for emulating index
properties.

You have indexers in on classes in C#.


In other words Yes you are right, C# does NOT support indexed properties.
You have to wrap your property/array in a new class and use an Indexer in the
class.

I'm in the same boat, I wanted to have several indexed properties in my
class, but decided to just expose the arrays publicly rather then spend an
extra week creating all the classes needed. Rather big flaw in the design if
you ask me, (but then it's a new product really so I'm suprised it actually
works)
Jul 21 '05 #3

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

Similar topics

3
2759
by: Fin | last post by:
Index properties in C++ class libraries (.NET) apper as set_ and get_ methods when used in C# To test this out, I changed the example from section "13.2 Indexed Properties" in MSDN, and placed the Employee and Manager classes in a library. Then I wrote a C# application implement the code from main(). What happens is that everyting works fine except when trying to call the indexed properties Example The following line does not work (as it...
6
1875
by: Altman | last post by:
I would like to use an indexed property to access my private array of user controls. If I don't use an indexed property, the property will show up in my Properties Window during development. Is there a way to get this to show up in the properties window?
2
294
by: Kor | last post by:
Hi, Am I right that indexed properties are implemented in VB ..NET, but NOT in C#. It seems that for C# you have to wrap the property in a separate class, for emulating index properties. In addition in VB .NET you can use indexed property, but with only an integer as indexer. So, a string as indexer (as in VB6) will not work here. Is that right ?
0
2035
by: liko81 | last post by:
I have an Invoice class that must know, directly or indirectly, how to do anything associated with creating, reading, or otherwise processing an invoice to a customer. It is an uber-DAO object that combines the business logic and DAO layers. The invoice it handles can have one to many lines. The most logical implementation is an Invoice object with an array of InvoiceLine objects. We of course want to prevent Joe Programmer from directly...
1
3365
by: Gokul | last post by:
Hi, I'm using indexed properties in an object which acts as the binding source. How can I implement INotifyPropertyChanged for that object so that when the indexed property is updated, binding target is updated? To be more specific please find the code below.. public class ClassA : INotifyPropertyChanged
3
1522
by: Michael Matteson | last post by:
I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th property and make its type equal to ClassB. but i would also like to index this property too. BTW Class B
1
6202
by: solar | last post by:
Indexed.Yes (Duplicates OK). In my code for remote control i wanted to remove the property Indexed of a field, but i receive the date type conversion error.How can i change these properties by code ? At the end of the list of the properties of the field,just below Required, it is written Indexed.Yes (Duplicates OK). i want to turn it to No and i write False, but it says property is unkown. What is the exact property and the command to...
7
16573
by: Tom Dacon | last post by:
I'm using Reflection to iterate over the properties and fields of an arbitrary object. For non-indexed properties it's pi.GetValue(theObject, Nothing) for VB, or pi.GetValue(theObject, null) for C# For indexed properties, instead of Nothing (null) you pass an array of index values. OK, no problem so far.
17
2789
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain property of an object is just an ordinary attribute. But that applies to
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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
10321
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
10077
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
9152
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
6853
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();...
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.