473,320 Members | 1,957 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.

Indexer on generic argument

Hi,
why does the first Console::Write give error "indexer needs array or
pointer" and the second not?

generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;
Console::Write("{0}, ", list2[0]); // ok
}
};

Cheers,
Christian
Aug 5 '08 #1
6 1275
On Aug 5, 9:22*pm, christian2.schm...@gmx.de wrote:
why does the first Console::Write give error "indexer needs array or
pointer" and the second not?

generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
* * * * MyClass() {}
* * * * void Method(TList list1)
* * * * {
* * * * * * * * Console::Write("{0}, ", list1[0]); // error C2109 ??
* * * * * * * * IList<ItemType>^ list2 = list1;
* * * * * * * * Console::Write("{0}, ", list2[0]); // ok
* * * * }

};
I can't figure out why it should be any different, but specifying the
indexer property explicitly works:

Console::Write("{0}, ", list1->default[0]);

Since there's no real reason why it shouldn't be able to figure out
the default property when the short form is used (after all, it can do
it for types which are not generic type parameters just fine!), it's
probably worth opening a suggestion on VS Connect regarding this.
Aug 5 '08 #2
Pavel Minaev wrote:
On Aug 5, 9:22 pm, christian2.schm...@gmx.de wrote:
>why does the first Console::Write give error "indexer needs array or
pointer" and the second not?

generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;
This line shouldn't have worked -- list1 has stack semantics (tracking
reference), list2 is a tracking handle. You need an address-of operator to
make a handle from a reference.

Maybe changing the formal parameter to a handle "TList^ list1" would resolve
your problem?
>Console::Write("{0}, ", list2[0]); // ok
}

};

I can't figure out why it should be any different, but specifying the
indexer property explicitly works:

Console::Write("{0}, ", list1->default[0]);

Since there's no real reason why it shouldn't be able to figure out
the default property when the short form is used (after all, it can do
it for types which are not generic type parameters just fine!), it's
probably worth opening a suggestion on VS Connect regarding this.

Aug 7 '08 #3
On 7 Aug., 16:47, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Pavel Minaev wrote:
On Aug 5, 9:22 pm, christian2.schm...@gmx.de wrote:
why does the first Console::Write give error "indexer needs array or
pointer" and the second not?
generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;

This line shouldn't have worked -- list1 has stack semantics (tracking
reference), list2 is a tracking handle. You need an address-of operator to
make a handle from a reference.
Isn't this some kind of autocasting magic?
Maybe changing the formal parameter to a handle "TList^ list1" would resolve
your problem?
No, this gives error C3229: dereferencing a generic type parameter is
not allowed.

Thanks,
Christian
Aug 7 '08 #4
ch****************@gmx.de wrote:
On 7 Aug., 16:47, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>Pavel Minaev wrote:
>>On Aug 5, 9:22 pm, christian2.schm...@gmx.de wrote:
why does the first Console::Write give error "indexer needs array
or pointer" and the second not?
>>>generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;

This line shouldn't have worked -- list1 has stack semantics
(tracking reference), list2 is a tracking handle. You need an
address-of operator to make a handle from a reference.

Isn't this some kind of autocasting magic?
>Maybe changing the formal parameter to a handle "TList^ list1" would
resolve your problem?

No, this gives error C3229: dereferencing a generic type parameter is
not allowed.
Are you instantiating as MyClass<TListor MyClass<TList^>?
>
Thanks,
Christian

Aug 8 '08 #5
On Aug 7, 6:47*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;

This line shouldn't have worked -- list1 has stack semantics (tracking
reference), list2 is a tracking handle. *You need an address-of operator to
make a handle from a reference.
It works because it's a generic declaration, not a template
declaration. Generic type parameters can only be instantiated with
handles for ref types - so, for a specific instantiation, TList would
be something like List<T>^, and not just plain List<T>.
Aug 12 '08 #6
Pavel Minaev wrote:
On Aug 7, 6:47 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>>>generic <typename TList, typename ItemTypewhere TList :
IList<ItemType>
ref class MyClass
{
public:
MyClass() {}
void Method(TList list1)
{
Console::Write("{0}, ", list1[0]); // error C2109 ??
IList<ItemType>^ list2 = list1;

This line shouldn't have worked -- list1 has stack semantics
(tracking reference), list2 is a tracking handle. You need an
address-of operator to make a handle from a reference.

It works because it's a generic declaration, not a template
declaration. Generic type parameters can only be instantiated with
handles for ref types - so, for a specific instantiation, TList would
be something like List<T>^, and not just plain List<T>.
Hmmm. Then the constraint line makes little sense. A tracking handle
doesn't implement an interface, the type of the referenced object does. I
realize this is connected to the .NET behavior of following different rules
when instantiating generics using ref types vs value types, and there
probably is no perfectly consistent syntax which expresses the behavior.
Aug 25 '08 #7

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

Similar topics

0
by: Brian Takita | last post by:
Hello, I am able to test an object to see if it is an indexer (IsIndexer function), however I don't know how to call the indexer without Unboxing to the object's class. Do I need to use...
3
by: Sam Martin | last post by:
Basically, i've got a class called RegularExpressions which contains a private Hashtable of RegExps used in my apps. I obviously don't want to give full public access to the Hashtable, so I'd...
5
by: SpotNet | last post by:
Hello NewsGroup, I have a custom class and a collection for that custom class that inherits CollectionBase. As such; public class MyClass { private string datamember1 = string.Empty,...
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
19
by: Brett Romero | last post by:
Here's a table of data I'm putting into a collection: CodeId CodeGroup CodeSubGroup Type 1 K K.1 Shar1 2 K ...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
2
by: hufaunder | last post by:
SITUATION: I have a class "MainClass" that has an indexer "SubClass1Indexer". This indexer returns an instance that implements a particular interface "ISubClass1". This interface defines another...
2
by: rsdev | last post by:
Hi Guys, I have assigned a variable to a generic list class and would like to access the propertied dynamically. Do I need to create an indexer for this? And how do I do that with a strongly...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
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...
0
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...
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...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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.