473,778 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do I make brackets [] take specific args (like the DataRow class)

I'd like to impliment the same type of functionality the DataRow has as far
as how it uses brackets. I'll give you an example of what I'm looking for.

DataRow dr = dt.Rows[0];

string aTestVar = (string)dr[0];
....OR...
string aTestVar = (string)dr["aTestColum n"];

How would I impliment this in my own custom types? Thanks.

Chris LaJoie
Nov 15 '05 #1
2 1208
Hi,

What you are refering to is called a indexer. In C# the syntax for an
indexer is like that of a property. The following is the indexer that takes
a int as the index and returns an object. Note that the indexer is named
this in C#. The type of the index and the return value can be what ever make
sense for your implemetation.

class MyData
{
public virtual object this[int index]
{
get {...}
set {...}
}
}

To use this

MyData d = new MyData()
string s = (string)d[2];

Hope this helps

Chris Taylor

"Chris LaJoie" <ch***@etriptra der.com> wrote in message
news:eB******** ******@TK2MSFTN GP10.phx.gbl...
I'd like to impliment the same type of functionality the DataRow has as far as how it uses brackets. I'll give you an example of what I'm looking for.
DataRow dr = dt.Rows[0];

string aTestVar = (string)dr[0];
...OR...
string aTestVar = (string)dr["aTestColum n"];

How would I impliment this in my own custom types? Thanks.

Chris LaJoie

Nov 15 '05 #2
"Chris LaJoie" <ch***@etriptra der.com> wrote in message
news:eB******** ******@TK2MSFTN GP10.phx.gbl...
I'd like to impliment the same type of functionality the DataRow has as far as how it uses brackets. I'll give you an example of what I'm looking for.
DataRow dr = dt.Rows[0];

string aTestVar = (string)dr[0];
...OR...
string aTestVar = (string)dr["aTestColum n"];

How would I impliment this in my own custom types? Thanks.


Use indexers:

public class MyClass
{
public string this[int index]
{
get { return // whatever you need to return based on index }
set { whatever = value; }
}
public string this[string match]
{
get { return // whatever you need to return based on match } // etc.
}
}
Nov 15 '05 #3

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

Similar topics

17
43467
by: Behrang Dadsetan | last post by:
Hi all, I would like deleting specific characters from a string. As an example, I would like to delete all of the '@' '&' in the string 'You are ben@orange?enter&your&code' so that it becomes 'benorange?enteryourcode'. So far I have been doing it like: str = 'You are ben@orange?enter&your&code' str = ''.join()
1
6437
by: Veverita | last post by:
Hi there I'm hoping that someone can help me with a question I have about javascript syntax. I got an html page that uploads an image and some text field to a database. What I'd like to do is modify the content of one of the textfields prior to it being submitted to the database. Specifically, I need to
27
5929
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's gcc specific. Is there a way to do it in a standard c++ way? TIA
3
5590
by: | last post by:
Hello, I am trying to get add a product to a cart from a gridview control when a button in the gridview is clicked. I have a book on how to do this in asp.net 2.0 but it is done by specifying the "DataTextField" as the productID of the product that I want to add. This works but the button displays the productID as the text instead of "Add to Cart". What is the best way to get this to work?
0
1543
by: Leeor Chernov | last post by:
Hi , I am using the method: DSSap.ReadXml( XmlPath,XmlReadMode.InferSchema ); And as I expected I get an error when the xml does not matching my DataSet that contains already a schema , The problem is that I get the following to-much detailed message: System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s,...
2
2989
by: Gabriele Farina | last post by:
Hi, I'm tring to implement a Singleton object that should be specific for every thread who create it, not global. I tried a solution that seems to work, but I have a very poor knowledge of concurrent programming, so I'd like someone to help me find some problems in my implementation. Here is the code:
2
2064
by: tony | last post by:
Hello! I have this template class with a default type (please do not give too much importance to the actual code, it's just an example, it's not a useful implementation of anything nor a good example of use of void*): template <typename Type = void> class MyClass { int Var; Type* Get() { return((Type*)Var);
18
2758
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and useful tracebacks on the other. I want both! How do you guys do it? What's the pythonic way? Are...
5
1466
by: alan4cast | last post by:
I posted this in the VB forum several days ago, and got no reply. Since it's specific to VB.Net I thought I'd try it here. I'm a relatively-experienced VB programmer, but I'm still working on learning all of the .net things that I should know. So when I came across this one, I started to dig into it so see if I could figure it out... Here's what I know about brackets - If they are used around a reserved word then that becomes a usable...
0
9465
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
10127
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
10068
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
9923
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
8954
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...
1
7474
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5370
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...
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2863
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.