Subclassing ArrayList

  • Thread starter Thread starter Guinness Mann
  • Start date Start date
G

Guinness Mann

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?
 
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.
 
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; }
set { base = 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 = "";
}
}
 
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 said:
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; }
set { base = 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
(e-mail address removed)
 
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; }
set { base = 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
 
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 said:
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; }
set { base = 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



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");
}

}
 
:-D You are very welcome!

Take care
100

Guinness Mann said:
You're the man, 100. Thanks again.

--Rick


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

news:[email protected]...
 
Back
Top