Implementing generic IEnumerable

G

Guest

I can't seem to figure this one out... I've searched MSDN and Goog, and made
my best guesses to no avail,, so help would be much appreciated!

public ref class T sealed
: public System::Collections::IEnumerable
, public System::Collections::Generic::IEnumerable<int>
{
// How to implement GetEnumerator() for both interfaces?
// Compiler complains that functions differ only in return type
// which I understand, but can't get the right syntax for explicit
// interface implementations...
};

Explicit interface implementations in .NET SDK show the following syntax:

private: virtual System::Collections::IEnumerator^
System.Collections.IEnumerable.GetEnumerator() sealed =
System::Collections::IEnumerable::GetEnumerator
{ /*function body*/ }

Note the 'dot' scope operator in specifying the method name - it causes a
syntax error when I compile. Changing it to :: the compiler says that's old
syntax. Removing it compiles fine if I don't try to implement the generic
IEnumerable too. Stumped! ;)

Thanks, KH
 
T

Tomas Restrepo \(MVP\)

KH,

I can't seem to figure this one out... I've searched MSDN and Goog, and
made
my best guesses to no avail,, so help would be much appreciated!

public ref class T sealed
: public System::Collections::IEnumerable
, public System::Collections::Generic::IEnumerable<int>
{
// How to implement GetEnumerator() for both interfaces?
// Compiler complains that functions differ only in return type
// which I understand, but can't get the right syntax for explicit
// interface implementations...
};

Explicit interface implementations in .NET SDK show the following syntax:

private: virtual System::Collections::IEnumerator^
System.Collections.IEnumerable.GetEnumerator() sealed =
System::Collections::IEnumerable::GetEnumerator
{ /*function body*/ }

Note the 'dot' scope operator in specifying the method name - it causes a
syntax error when I compile. Changing it to :: the compiler says that's
old
syntax. Removing it compiles fine if I don't try to implement the generic
IEnumerable too. Stumped! ;)


You can rename one of the methods and use an explicit interface
implementation to tie it to the actual method of the interface it
implements. The following example renames IEnumerable<T>::GetEnumerator to
GetEnumeratorGen():


using namespace System;
using namespace System::Collections;

public ref class T sealed
: public IEnumerable
, public System::Collections::Generic::IEnumerable<int>
{
public:
virtual IEnumerator^ GetEnumerator() = IEnumerable::GetEnumerator
{
return nullptr;
}

virtual System::Collections::Generic::IEnumerator<int>^
GetEnumeratorGen()
= System::Collections::Generic::IEnumerable<int>::GetEnumerator
{
return nullptr;
}

};


You can find this documented on your VS2005 documentation under the heading:
Explicit Override of an interface member:
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vccore/html/46f1f536-bf43-4311-9a17-ff2282e528a9.htm
 
H

Holger Grund

KH said:
I can't seem to figure this one out... I've searched MSDN and Goog, and
made
my best guesses to no avail,, so help would be much appreciated!

public ref class T sealed
: public System::Collections::IEnumerable
, public System::Collections::Generic::IEnumerable<int>
{
// How to implement GetEnumerator() for both interfaces?
// Compiler complains that functions differ only in return type
// which I understand, but can't get the right syntax for explicit
// interface implementations...
};

Explicit interface implementations in .NET SDK show the following syntax:

private: virtual System::Collections::IEnumerator^
System.Collections.IEnumerable.GetEnumerator() sealed =
System::Collections::IEnumerable::GetEnumerator
{ /*function body*/ }
I don't think that works with ambigious virtual functions. Just
rename both to different names and use explicit override specifications:

IEnumerator^ GetNongenericEnumerator() sealed =
IEnumerable::GetEnumerator { ... }
Generic::IEnumerator<int>^ GetGenericEnumerator() sealed =
Generic::IEnumerable<int>::GetEnumerator { ... }

-hg
 
G

Guest

Thanks! both of you


Tomas Restrepo (MVP) said:
KH,




You can rename one of the methods and use an explicit interface
implementation to tie it to the actual method of the interface it
implements. The following example renames IEnumerable<T>::GetEnumerator to
GetEnumeratorGen():


using namespace System;
using namespace System::Collections;

public ref class T sealed
: public IEnumerable
, public System::Collections::Generic::IEnumerable<int>
{
public:
virtual IEnumerator^ GetEnumerator() = IEnumerable::GetEnumerator
{
return nullptr;
}

virtual System::Collections::Generic::IEnumerator<int>^
GetEnumeratorGen()
= System::Collections::Generic::IEnumerable<int>::GetEnumerator
{
return nullptr;
}

};


You can find this documented on your VS2005 documentation under the heading:
Explicit Override of an interface member:
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vccore/html/46f1f536-bf43-4311-9a17-ff2282e528a9.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top