469,613 Members | 1,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Type Casting With Generics

I've been reading a discussion thread at
http://groups.google.com/group/micro...9f8362a9f5ff52
regarding typecasting generic collections to classical collections and
vice-a-versa

I faced a similar problem and solved it slightly differently... The
apporach i seems to work but i am sure someone has a better apporach
for solving this problem.

In my case i was using nHibernate which was returning Classic IList
collections and I needed to convert those to Generic driven
collection... so i wrote up a General class that will convert all
ILists to IList<T>... Below is my code:

/// <summary>
/// Converts All Non Generic Lists of n-Hibernate to Generics
/// </summary>
/// <typeparam name="T"></typeparam>
class NSList<T> : IList<T>
{

public NSList(T users)
{

}
public NSList(List NonGenericItems)
{
//TODO: Better ways of doing this same thing? ConvertAll
function of
//dotnet ? (rajiv popat)
for (int ncounter = 0; ncounter < NonGenericItems.Count;
ncounter++)
{
this.Add((T)NonGenericItems[ncounter]);
}
}

}

While this seems to do the job the Ugly for loop concerns me... Would
it be a problem if the collection had more objects in future? Any
better ways of doing it?

Bruce / Lebesgue / Anyone else, When this thread talks about Creating
an interface - does it mean
mean an interface that would do somoething similar to what i do in the
class here?

I've just started playing around with the Beta 2.0 so my questions
might be slightly stupid :) and strangely enough i could not reply to
that thread so i started a new one.

regards,
rajiv.

Nov 17 '05 #1
2 2724
rajivpopat,

This code will work, but it will create a copy of the collection which
could get out of synch.

If this is a concern, what I would do is create a class which acts as a
wrapper to the IList implementation. It would take the IList in it's
constructor, and the type would take a generic type parameter for the
IList<T> implementation. You then just forward the calls to the underlying
IList implementation.

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

"rajivpopat" <ra********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I've been reading a discussion thread at
http://groups.google.com/group/micro...9f8362a9f5ff52
regarding typecasting generic collections to classical collections and
vice-a-versa

I faced a similar problem and solved it slightly differently... The
apporach i seems to work but i am sure someone has a better apporach
for solving this problem.

In my case i was using nHibernate which was returning Classic IList
collections and I needed to convert those to Generic driven
collection... so i wrote up a General class that will convert all
ILists to IList<T>... Below is my code:

/// <summary>
/// Converts All Non Generic Lists of n-Hibernate to Generics
/// </summary>
/// <typeparam name="T"></typeparam>
class NSList<T> : IList<T>
{

public NSList(T users)
{

}
public NSList(List NonGenericItems)
{
//TODO: Better ways of doing this same thing? ConvertAll
function of
//dotnet ? (rajiv popat)
for (int ncounter = 0; ncounter < NonGenericItems.Count;
ncounter++)
{
this.Add((T)NonGenericItems[ncounter]);
}
}

}

While this seems to do the job the Ugly for loop concerns me... Would
it be a problem if the collection had more objects in future? Any
better ways of doing it?

Bruce / Lebesgue / Anyone else, When this thread talks about Creating
an interface - does it mean
mean an interface that would do somoething similar to what i do in the
class here?

I've just started playing around with the Beta 2.0 so my questions
might be slightly stupid :) and strangely enough i could not reply to
that thread so i started a new one.

regards,
rajiv.

Nov 17 '05 #2
Hi Nicholas,

You're 100% correct in saying that the copy of the collection could get
out of sync. And now that i think about it - i realize how ... crappy
my code was :) becuase it just made a one time snap shot copy of the
list and then doesn't do anything! Thanks for pointing this out.

So, what i understand from your suggestion is that i have just one
non-generic list which is actually maintained. The Class that inherits
off IList<T> just implements methods and ensures that any updates on
the generic list are actually passed back to the non-generic list by
invoking it's native methods. Here's the code i wrote up based on your
idea:
(Again, this code 'seems to work' but PLEASE do let me know what you
think about this... just want to be sure i am on the right learning
track).

/// <summary>
/// Converts All Non Generic Lists of n-Hibernate to Generics
/// </summary>
/// <typeparam name="T"></typeparam>
public class NSList<T> : IList<T>
{
IList _NonGenericItems = null;

public NSList(T GenericItems)
{

}
public NSList()
{

}
public NSList(IList NonGenericItems)
{
_NonGenericItems = NonGenericItems;
// If the 'wrapping has occurred successfully then
technically this
// crappy code is not required!
/*
for (int ncounter = 0; ncounter < NonGenericItems.Count;
ncounter++)
{
this.Add((T)NonGenericItems[ncounter]);
}
*/

}
#region IList<T> Members

public int IndexOf(T item)
{
return _NonGenericItems.IndexOf(item);
}

public void Insert(int index, T item)
{
_NonGenericItems.Insert(index, item);
}

public void RemoveAt(int index)
{
_NonGenericItems.RemoveAt(index);
}

public T this[int index]
{
get
{
return (T) _NonGenericItems[index];
}
set
{
_NonGenericItems[index] = value;
}
}

#endregion

#region ICollection<T> Members

public void Add(T item)
{
_NonGenericItems.Add(item);
}

public void Clear()
{
_NonGenericItems.Clear();
}

public bool Contains(T item)
{
return _NonGenericItems.Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
_NonGenericItems.CopyTo(array, arrayIndex);
}

public int Count
{
get { return _NonGenericItems.Count; }
}

public bool IsReadOnly
{
get { return _NonGenericItems.IsReadOnly; }
}

public bool Remove(T item)
{
// This Remove always returned a void so i am always going
to
// return a true?
_NonGenericItems.Remove(item);
return true;
}

#endregion

#region IEnumerable<T> Members

public IEnumerator<T> GetEnumerator()
{
// I am not sure if this is Correct.
return (IEnumerator < T >)
_NonGenericItems.GetEnumerator();
}

#endregion

#region IEnumerable Members

IEnumerator IEnumerable.GetEnumerator()
{
// Not sure if this is correct
return (IEnumerator<T>)_NonGenericItems.GetEnumerator();
}

#endregion
}

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by MattC | last post: by
8 posts views Thread by Ashish | last post: by
4 posts views Thread by krahenbuhl | last post: by
5 posts views Thread by anders.forsgren | last post: by
8 posts views Thread by Kris Jennings | last post: by
7 posts views Thread by Ajeet | last post: by
4 posts views Thread by Random | last post: by
3 posts views Thread by BombDrop | last post: by
4 posts views Thread by =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.