Generics

H

Hoop

Hi,

I am in the process of trying to create a Generic Class. Getting a
complier error that I am not sure how to deal with.
Here is the class:

using System;
using System.Collections.Generic;
using System.Text;
using DeviceInterface;
using CanDevice;
using CanInterface;

namespace SimulatorApplication
{
//Generic class to store CanDevice's
class CanDeviceCollection<T> : IEnumerable<T>
{

private List<T> arDevices = new List<T>();


//A generic method just to display the data
public static void DisplayBaseClass()
{
Console.WriteLine("Base class of {0} is: {1}.",
typeof(T), typeof(T).BaseType);
}


public T GetDevice(int pos)
{ return arDevices[pos]; }

public void AddDevice(T d)
{ arDevices.Add(d); }
public void ClearDevice()
{ arDevices.Clear(); }
public int Count
{ get { return arDevices.Count; } }

// IEnumerable<T> extends IEnumerable, therefore
// we need to implement both versions of GetEnumerator().
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{ return arDevices.GetEnumerator(); }

IEnumerator IEnumerable.GetEnumerator() //Compiler Error Here
{ return arDevices.GetEnumerator(); }
}
}

I get the error, Using generic type
'System.Collections.Generic.IEnumerator<T> 'requires '1' type
arguments.

I have followed an example from the text "Pro C# 2005 and the .NET 2.0
Platform" for this code.
Any help or direction would be appreciated.
Thanks
Jeff
 
J

Jon Skeet [C# MVP]

Hoop said:
I am in the process of trying to create a Generic Class. Getting a
complier error that I am not sure how to deal with.
Here is the class:

using System;
using System.Collections.Generic;
using System.Text;
using DeviceInterface;
using CanDevice;
using CanInterface;

<snip>

Surprisingly enough, this is where your problem lies. You need

using System.Collections;

as well, otherwise the compiler doesn't know about the non-generic form
of IEnumerable.
 
H

Hoop

Hi Jon,
That fixed it.
Thanks alot.
Jeff
<snip>

Surprisingly enough, this is where your problem lies. You need

using System.Collections;

as well, otherwise the compiler doesn't know about the non-generic form
of IEnumerable.
 

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