By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Subclassing ArrayList

P: n/a
I have a class that I use throughout my application, and I store
collections of my class in an ArrayList. To avoid some really ugly
casting I'd like to subclass ArrayList to get a typed version.

Has anyone done this? Do you have any advice for me? How about some
sample code?
--
Who's better for furniture? NOOOOOOOOOOOOOOOOOOBODY!!!!

Guinness Mann
GM***@Dublin.com

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Guiness Mann,

Instead of subclassing ArrayList, you should extend the CollectionBase
class, as it is the base for expandable collections which are type safe.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. .
I have a class that I use throughout my application, and I store
collections of my class in an ArrayList. To avoid some really ugly
casting I'd like to subclass ArrayList to get a typed version.

Has anyone done this? Do you have any advice for me? How about some
sample code?
--
Who's better for furniture? NOOOOOOOOOOOOOOOOOOBODY!!!!

Guinness Mann
GM***@Dublin.com

Nov 15 '05 #2

P: n/a
In article <ez**************@tk2msftngp13.phx.gbl>,
ni**************@exisconsulting.com says...
Instead of subclassing ArrayList, you should extend the CollectionBase
class, as it is the base for expandable collections which are type safe.


I'd be interested in seeing some sample code. In the meantime, I've
done this:

public class QRowArray : ArrayList
{
public new QRow this[int i]
{
get { return (QRow)base[i]; }
set { base[i] = value; }
}
}

Perhaps you could point out to me the errors of my ways?

-- Rick

public class QRow : IComparable
{
int numDistractors = Convert.ToInt32
(UA.DistractorInfo.NUM_DISTRACTORS);

public Distractor question;
public Distractor[] dis;
public int answer;
public int placement;

public QRow()
{
dis = new Distractor[numDistractors];
answer = 0;
placement = -1;
}
#region IComparable Members

public int CompareTo(object rhs)
{
QRow r = (QRow) rhs;
return this.placement.CompareTo(r.placement);
}

#endregion
}

public class Distractor
{
public int dId;
public string text;

public Distractor()
{
dId = 0;
text = "";
}
}

--

--
Who's better for furniture? NOOOOOOOOOOOOOOOOOOBODY!!!!

Guinness Mann
GM***@Dublin.com

Nov 15 '05 #3

P: n/a
100
Hi,
This is the way I use CollectionBase when I want to create a strongly typed
collection

class MyList: CollectionBase
{
public QRow this[int index]
{
get{return (QRow)InnerList[index];}
set{InnerList[index] = value;}
}

public int Add(QRow value)
{
return InnerList.Add(value);
}

public void Remove(QRow value)
{
InnerList.Remove(value);
}

protected override void OnValidate(object value)
{
base.OnValidate (value); //if null is acceptable don't call base
implemetation
if(!(value is QRow)) throw new ArgumentException("Type of the
value is not acceptable");
}

}

B\rgds
100

"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. .
In article <ez**************@tk2msftngp13.phx.gbl>,
ni**************@exisconsulting.com says...
Instead of subclassing ArrayList, you should extend the CollectionBase class, as it is the base for expandable collections which are type safe.


I'd be interested in seeing some sample code. In the meantime, I've
done this:

public class QRowArray : ArrayList
{
public new QRow this[int i]
{
get { return (QRow)base[i]; }
set { base[i] = value; }
}
}

Perhaps you could point out to me the errors of my ways?

-- Rick

public class QRow : IComparable
{
int numDistractors = Convert.ToInt32
(UA.DistractorInfo.NUM_DISTRACTORS);

public Distractor question;
public Distractor[] dis;
public int answer;
public int placement;

public QRow()
{
dis = new Distractor[numDistractors];
answer = 0;
placement = -1;
}
#region IComparable Members

public int CompareTo(object rhs)
{
QRow r = (QRow) rhs;
return this.placement.CompareTo(r.placement);
}

#endregion
}

public class Distractor
{
public int dId;
public string text;

public Distractor()
{
dId = 0;
text = "";
}
}

--

--
Who's better for furniture? NOOOOOOOOOOOOOOOOOOBODY!!!!

Guinness Mann
GM***@Dublin.com

Nov 15 '05 #4

P: n/a
Hey, thanks, 100. I've copied your example into my snippets for
possible use on another project. In this case, though, I really want to
use ArrayList because of some of it's member functions (like Sort), and
it seems incredibly easy to simply add this:

public class QRowArray : ArrayList
{
public new QRow this[int i]
{
get { return (QRow)base[i]; }
set { base[i] = value; }
}
}

And be done with it. I understand that I, for instance, don't have type
safety on input data, but it's really just convenience code to keep all
the ugly casting out of the body of my code; it's not production code.
Have I violated any secret laws?

-- Rick

In article <eh**************@tk2msftngp13.phx.gbl>, 10*@100.com says...
Hi,
This is the way I use CollectionBase when I want to create a strongly typed
collection

class MyList: CollectionBase
{
public QRow this[int index]
{
get{return (QRow)InnerList[index];}
set{InnerList[index] = value;}
}

public int Add(QRow value)
{
return InnerList.Add(value);
}

public void Remove(QRow value)
{
InnerList.Remove(value);
}

protected override void OnValidate(object value)
{
base.OnValidate (value); //if null is acceptable don't call base
implemetation
if(!(value is QRow)) throw new ArgumentException("Type of the
value is not acceptable");
}

}

Nov 15 '05 #5

P: n/a
100
Hi Guinness,
Here is my new class supporting all sort that ArrayList has + type safety.

class MyList: CollectionBase
{
public ArrayList mInnerArrayList;

public MyList()
{
mInnerArrayList = ArrayList.Adapter(this.InnerList);
}

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

public int Add(QRow value)
{
return InnerList.Add(value);
}

public void Remove(QRow value)
{
InnerList.Remove(value);
}

public void Sort()
{
mInnerArrayList.Sort();
}

protected override void OnValidate(object value)
{
base.OnValidate (value);
if(!(value is QRow)) throw new ArgumentException("Type of the
value is not acceptable");
}
}

You can add the second overload of the *sort* method as well as other
ArrayList operations that you miss in CollectionBase.

HTH
B\rgds
100

"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. .
Hey, thanks, 100. I've copied your example into my snippets for
possible use on another project. In this case, though, I really want to
use ArrayList because of some of it's member functions (like Sort), and
it seems incredibly easy to simply add this:

public class QRowArray : ArrayList
{
public new QRow this[int i]
{
get { return (QRow)base[i]; }
set { base[i] = value; }
}
}

And be done with it. I understand that I, for instance, don't have type
safety on input data, but it's really just convenience code to keep all
the ugly casting out of the body of my code; it's not production code.
Have I violated any secret laws?

-- Rick

In article <eh**************@tk2msftngp13.phx.gbl>, 10*@100.com says...
Hi,
This is the way I use CollectionBase when I want to create a strongly typed collection

class MyList: CollectionBase
{
public QRow this[int index]
{
get{return (QRow)InnerList[index];}
set{InnerList[index] = value;}
}

public int Add(QRow value)
{
return InnerList.Add(value);
}

public void Remove(QRow value)
{
InnerList.Remove(value);
}

protected override void OnValidate(object value)
{
base.OnValidate (value); //if null is acceptable don't call base implemetation
if(!(value is QRow)) throw new ArgumentException("Type of the
value is not acceptable");
}

}

Nov 15 '05 #6

P: n/a
You're the man, 100. Thanks again.

--Rick
In article <Ol**************@TK2MSFTNGP11.phx.gbl>, 10*@100.com says...
Hi Guinness,
Here is my new class supporting all sort that ArrayList has + type safety.

class MyList: CollectionBase
{
public ArrayList mInnerArrayList;

public MyList()
{
mInnerArrayList = ArrayList.Adapter(this.InnerList);
}

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

public int Add(QRow value)
{
return InnerList.Add(value);
}

public void Remove(QRow value)
{
InnerList.Remove(value);
}

public void Sort()
{
mInnerArrayList.Sort();
}

protected override void OnValidate(object value)
{
base.OnValidate (value);
if(!(value is QRow)) throw new ArgumentException("Type of the
value is not acceptable");
}
}

You can add the second overload of the *sort* method as well as other
ArrayList operations that you miss in CollectionBase.

HTH
B\rgds
100

"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. .

Nov 15 '05 #7

P: n/a
100
:-D You are very welcome!

Take care
100

"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. .
You're the man, 100. Thanks again.

--Rick
In article <Ol**************@TK2MSFTNGP11.phx.gbl>, 10*@100.com says...
Hi Guinness,
Here is my new class supporting all sort that ArrayList has + type safety.
class MyList: CollectionBase
{
public ArrayList mInnerArrayList;

public MyList()
{
mInnerArrayList = ArrayList.Adapter(this.InnerList);
}

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

public int Add(QRow value)
{
return InnerList.Add(value);
}

public void Remove(QRow value)
{
InnerList.Remove(value);
}

public void Sort()
{
mInnerArrayList.Sort();
}

protected override void OnValidate(object value)
{
base.OnValidate (value);
if(!(value is QRow)) throw new ArgumentException("Type of the
value is not acceptable");
}
}

You can add the second overload of the *sort* method as well as other
ArrayList operations that you miss in CollectionBase.

HTH
B\rgds
100

"Guinness Mann" <GM***@dublin.com> wrote in message
news:MP************************@news.newsguy.com.. .

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.