473,385 Members | 1,606 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,385 software developers and data experts.

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["aTestColumn"];

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

Chris LaJoie
Nov 15 '05 #1
2 1198
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***@etriptrader.com> wrote in message
news:eB**************@TK2MSFTNGP10.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["aTestColumn"];

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

Chris LaJoie

Nov 15 '05 #2
"Chris LaJoie" <ch***@etriptrader.com> wrote in message
news:eB**************@TK2MSFTNGP10.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["aTestColumn"];

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
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...
1
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...
27
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...
3
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...
0
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...
2
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...
2
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...
18
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.