473,320 Members | 2,122 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,320 software developers and data experts.

operator question

Hi

is there a way todo this in C#

class MyClass {
protected MyType1[] m_MyTypes1;
protected MyType2[] m_MyTypes2;

public MyClass() { . . . }

public MyType1 this[ int position ] {
get {
return m_MyTypes1[ position ];
}
}
public MyType2 this[ int position ] {
get {
return m_MyTypes2[ position ];
}
}
}

void Foo() {
MyClass m = new MyClass();
MyType1 m1 = m[ 1 ];
MyType2 m2 = m[ 1 ];
}

This won´t compile because there can´t be 2 this-operator overloads that
take the same arguments.

Any ideas how i can do this without using direct (SetMyType/GetMyType)
methods?
thx

Kimmo Laine


Nov 16 '05 #1
2 934
Kimmo,

What you want to do is expose typed collections which will have indexers
that return the appropriate type. You can not produce an overload that
returns a different type (with the same parameter list).

I would recommend using a strongly typed collection, and storing those
in your class, exposing them through different properties which return
collection objects where the indexer will return the appropriate type.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:uN**************@TK2MSFTNGP09.phx.gbl...
Hi

is there a way todo this in C#

class MyClass {
protected MyType1[] m_MyTypes1;
protected MyType2[] m_MyTypes2;

public MyClass() { . . . }

public MyType1 this[ int position ] {
get {
return m_MyTypes1[ position ];
}
}
public MyType2 this[ int position ] {
get {
return m_MyTypes2[ position ];
}
}
}

void Foo() {
MyClass m = new MyClass();
MyType1 m1 = m[ 1 ];
MyType2 m2 = m[ 1 ];
}

This won´t compile because there can´t be 2 this-operator overloads that
take the same arguments.

Any ideas how i can do this without using direct (SetMyType/GetMyType)
methods?
thx

Kimmo Laine

Nov 16 '05 #2
Well, in C++, where you can overload operator=, it could be done. But in
C#, you cannot overload the assignment operator, so it can't be done. Which
is all for the best, as Nicholas's suggestion is the better way to go.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:uN**************@TK2MSFTNGP09.phx.gbl...
Hi

is there a way todo this in C#

class MyClass {
protected MyType1[] m_MyTypes1;
protected MyType2[] m_MyTypes2;

public MyClass() { . . . }

public MyType1 this[ int position ] {
get {
return m_MyTypes1[ position ];
}
}
public MyType2 this[ int position ] {
get {
return m_MyTypes2[ position ];
}
}
}

void Foo() {
MyClass m = new MyClass();
MyType1 m1 = m[ 1 ];
MyType2 m2 = m[ 1 ];
}

This won´t compile because there can´t be 2 this-operator overloads that
take the same arguments.

Any ideas how i can do this without using direct (SetMyType/GetMyType)
methods?
thx

Kimmo Laine

Nov 16 '05 #3

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
2
by: wongjoekmeu | last post by:
Hello All, I have a question about a C++ listing that I don't understand from a book that I use to learn C++. In the listing a class String is declared and defined. The beginning look like this...
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.