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

Is it possible to implement template

witnes
4
Hello. I want to have something like this:

CollectionCIass<int> numbers = new CollectionCIass<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

but I don't know how to implement this collection.

I can do either like this:
internal class CollectionCIass : System.Collections.Generic.IList<int>

but then I can't declare variable like CollectionCIass<int> numbers = new CollectionCIass<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
I need to declare it like
CollectionCIass numbers = new CollectionCIass { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
but I want to specify type in parenthesis.
Mar 9 '10 #1

✓ answered by witnes

After some investigations I've found answer:

public class CollectionCIass<T> : System.Collections.IEnumerable
{
private List<T> listOfData = new List<T>();
public void Add(T item)
{
listOfData.Add(item);
}

#region IEnumerable Members

public System.Collections.IEnumerator GetEnumerator()
{
foreach (T data in listOfData)
{
yield return data;
}
}

#endregion
}

1 1093
witnes
4
After some investigations I've found answer:

public class CollectionCIass<T> : System.Collections.IEnumerable
{
private List<T> listOfData = new List<T>();
public void Add(T item)
{
listOfData.Add(item);
}

#region IEnumerable Members

public System.Collections.IEnumerator GetEnumerator()
{
foreach (T data in listOfData)
{
yield return data;
}
}

#endregion
}
Mar 9 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
3
by: chris | last post by:
I am currently writing a templated function and want it to be able to implement it differently based on if the input class has implemented an overload of swap. However, I can't figure out how I can...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
11
by: Kai-Uwe Bux | last post by:
Hi folks, does anyone see a way to implement the following enum_limits template: template < typename EnumType > struct enum_limits { // first element in range of EnumType: static...
3
by: Atlas | last post by:
I tried the following in MSVC7.1 but was told "unable to deduce template parameter". template <int L, int M, int T> class Quantity; template <int L, int M, int T> Quantity<L/2,M/2,T/2> sqrt(const...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
7
by: Cerebrus99 | last post by:
Hi all, I am confused about how to sort an XML file. I mean how to *actually* sort the data in the physical file, not how to display sorted data. I am using a large XML file as a back-end...
1
by: midnight madness | last post by:
I tried but failed to implement a template class that support IEnumerator<T> interface using C++/CLI in VS 2005 Professional version. I could not figure out the proper syntax to implement the...
1
by: Frederick Gotham | last post by:
If we want to copy an array of POD's, we can simply do: SomePODType src = { ... }, dest; memcpy(&dest,&src,sizeof dest); We can assume that this method is definitely faster than (if not at...
3
by: Aspidus | last post by:
I wrote a simple new template class for a C++ project operating on quaternions. I would like to define 2 new particular operators The first I would like to be (*) for cross product (vector...
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
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.