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

Subclassing ArrayList

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
7 3889
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: WhiteRavenEye | last post by:
Why can't I subclass any window except mine in VB? Do I have to write dll for this? I've tried to subclass it with SetWindowLong but without success... Does anyone know how to subclass window...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: David Vaughan | last post by:
I'm using v2.3, and trying to write to text files, but with a maximum line length. So, if a line is getting too long, a suitable ' ' character is replaced by a new line. I'm subclassing the file...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
3
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
3
by: alotcode | last post by:
Hello: What is 'interprocess subclassing'? To give more context, I am writing in reference to the following remark: With the advent of the Microsoft Win32 API, interprocess subclassing was...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.