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

Custom collections

Hi,

I have a custom collection class that inherits from HashTable. When I add a
new CustomObject to my CustomCollection I would like to ensure that it will
only accept objects of type CustomObject. How would I do this? Thanks in
advance.

Public Class CustomCollection
Inherits Hashtable
' default constructor
Public Sub New()
End Sub
End Class

' this is how I would add new objects
Dim customCollection As New CustomCollection
'Now, fill it with some custom objects
customCollection.Add(1, New CustomObject("the key", "the value"))
Jul 21 '05 #1
5 2161
Rufus,
Rather then inherit from HashTable, inherit from DictionaryBase.

Public Class CustomObjectCollection
Inherits DictionaryBase

Public Sub Add(ByVal key As Integer, ByVal value As CustomObject)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.InnerHashtable.Item(key), CustomObject)
End Get
End Property

End Class

Then add other typesafe methods as needed.

There is also a CollectionBase that wraps an ArrayList.

Hope this helps
Jay

"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
Hi,

I have a custom collection class that inherits from HashTable. When I add
a
new CustomObject to my CustomCollection I would like to ensure that it
will
only accept objects of type CustomObject. How would I do this? Thanks in
advance.

Public Class CustomCollection
Inherits Hashtable
' default constructor
Public Sub New()
End Sub
End Class

' this is how I would add new objects
Dim customCollection As New CustomCollection
'Now, fill it with some custom objects
customCollection.Add(1, New CustomObject("the key", "the value"))

Jul 21 '05 #2
Just what I was after! Many thanks.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e5*************@TK2MSFTNGP12.phx.gbl...
Rufus,
Rather then inherit from HashTable, inherit from DictionaryBase.

Public Class CustomObjectCollection
Inherits DictionaryBase

Public Sub Add(ByVal key As Integer, ByVal value As CustomObject)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.InnerHashtable.Item(key), CustomObject) End Get
End Property

End Class

Then add other typesafe methods as needed.

There is also a CollectionBase that wraps an ArrayList.

Hope this helps
Jay

"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
Hi,

I have a custom collection class that inherits from HashTable. When I add a
new CustomObject to my CustomCollection I would like to ensure that it
will
only accept objects of type CustomObject. How would I do this? Thanks in advance.

Public Class CustomCollection
Inherits Hashtable
' default constructor
Public Sub New()
End Sub
End Class

' this is how I would add new objects
Dim customCollection As New CustomCollection
'Now, fill it with some custom objects
customCollection.Add(1, New CustomObject("the key", "the value"))


Jul 21 '05 #3
Just one question though...why this line:

Return DirectCast(Me.InnerHashtable.Item(key), CustomObject)

I would have thought that 'Me.InnerHashtable.Item(key)' would always be
type CustomObject


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e5*************@TK2MSFTNGP12.phx.gbl...
Rufus,
Rather then inherit from HashTable, inherit from DictionaryBase.

Public Class CustomObjectCollection
Inherits DictionaryBase

Public Sub Add(ByVal key As Integer, ByVal value As CustomObject)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.InnerHashtable.Item(key), CustomObject) End Get
End Property

End Class

Then add other typesafe methods as needed.

There is also a CollectionBase that wraps an ArrayList.

Hope this helps
Jay

"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
Hi,

I have a custom collection class that inherits from HashTable. When I add a
new CustomObject to my CustomCollection I would like to ensure that it
will
only accept objects of type CustomObject. How would I do this? Thanks in advance.

Public Class CustomCollection
Inherits Hashtable
' default constructor
Public Sub New()
End Sub
End Class

' this is how I would add new objects
Dim customCollection As New CustomCollection
'Now, fill it with some custom objects
customCollection.Add(1, New CustomObject("the key", "the value"))


Jul 21 '05 #4
Nope - it would return an object.What you are creating is the wrapper - when
it goes inside the Hashtable, it gets cast to an Object. And when it comes
out, you need to cast it back as well

--
Sriram Krishnan

http://www.dotnetjunkies.com/weblog/sriram
"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
Just one question though...why this line:

Return DirectCast(Me.InnerHashtable.Item(key), CustomObject)

I would have thought that 'Me.InnerHashtable.Item(key)' would always be
type CustomObject


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e5*************@TK2MSFTNGP12.phx.gbl...
Rufus,
Rather then inherit from HashTable, inherit from DictionaryBase.

Public Class CustomObjectCollection
Inherits DictionaryBase

Public Sub Add(ByVal key As Integer, ByVal value As CustomObject)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.InnerHashtable.Item(key),

CustomObject)
End Get
End Property

End Class

Then add other typesafe methods as needed.

There is also a CollectionBase that wraps an ArrayList.

Hope this helps
Jay

"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
> Hi,
>
> I have a custom collection class that inherits from HashTable. When I add > a
> new CustomObject to my CustomCollection I would like to ensure that it
> will
> only accept objects of type CustomObject. How would I do this? Thanks in > advance.
>
> Public Class CustomCollection
> Inherits Hashtable
> ' default constructor
> Public Sub New()
> End Sub
> End Class
>
> ' this is how I would add new objects
> Dim customCollection As New CustomCollection
> 'Now, fill it with some custom objects
> customCollection.Add(1, New CustomObject("the key", "the value"))
>
>



Jul 21 '05 #5
Rufus,
You are correct the object being returned by Me.InnerHashtable is actually a
CustomObject, however the return type of Me.InnerHashtable is Object. The
DirectCast is telling the compiler that even though the return type of
Me.InnerHashtable is Object, you know its really a CustomObject being
returned. If you managed to get an object other then CustomObject into
Me.InnerHashtable then the DirectCast would cause an InvalidCastException at
runtime.

Without the DirectCast you would get a compile error if you are using Option
Strict On. You are using Option Strict On! As Option Strict On causes a lot
of compile time errors that are easier to identify rather then runtime
errors that may be much harder to identify & fix. Or worse cause a runtime
error that your user sees...
I should mention DictionaryBase supports the IDictionary interface. It has a
number of overridable methods such as OnInsert, OnSet, and OnValidate. The
DictionaryBase.On* methods allow you to do you own thing, such as validate
the object type. Overriding the DictionaryBase.On* methods are important if
you use the IDictionary interface to manipulate the object. For example if
you try the following with my original code:

Dim collection As New CustomObjectCollection
Dim dictionary As IDictionary = collection

collection.Add(1, New CustomObject)
collection.Add(2, New CustomObject)

dictionary.Add("3", "Hello World")

For Each de As DictionaryEntry In collection
Dim key As Integer = DirectCast(de.Key, Integer)
Dim value As CustomObject = DirectCast(de.Value, CustomObject)
Debug.WriteLine(value, key.ToString)
Next

You will get an exception on the "Dim key As Integer = DirectCast(de.Key,
Integer)" line, as the key to the third item added is a String, although we
"defined" our collection as only supporting Integer, Custom Object pairs.
The "problem" with the above code is that the "dictionary.Add" line may be
many many lines removed from the For Each making it a very hard problem to
track down.

If you override the OnValidate routine as below, then try the above code
again, you will receive an ArgumentException on the "dictionary.Add" line,
showing exactly where the problem is.

Public Class CustomObjectCollection
Inherits DictionaryBase

Public Sub Add(ByVal key As Integer, ByVal value As CustomObject)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.InnerHashtable.Item(key), CustomObject)
End Get
End Property

Protected Overrides Sub OnValidate(ByVal key As Object, ByVal value
As Object)
If Not TypeOf key Is Integer Then Throw New
ArgumentException("Key must be type Integer", "key")
If Not TypeOf value Is CustomObject Then Throw New
ArgumentException("Value must be type CustomObject", "value")
End Sub

End Class

See the on-line help for DictionaryBase on when you would use the other
DictionaryBase.On* methods. Most of the time I do not override them, however
its good to know you can for when you need them.

Hope this helps
Jay

"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
Just one question though...why this line:

Return DirectCast(Me.InnerHashtable.Item(key), CustomObject)

I would have thought that 'Me.InnerHashtable.Item(key)' would always be
type CustomObject


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:e5*************@TK2MSFTNGP12.phx.gbl...
Rufus,
Rather then inherit from HashTable, inherit from DictionaryBase.

Public Class CustomObjectCollection
Inherits DictionaryBase

Public Sub Add(ByVal key As Integer, ByVal value As CustomObject)
Me.InnerHashtable.Add(key, value)
End Sub

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.InnerHashtable.Item(key),

CustomObject)
End Get
End Property

End Class

Then add other typesafe methods as needed.

There is also a CollectionBase that wraps an ArrayList.

Hope this helps
Jay

"rufus" <he***@online.de> wrote in message news:ck**********@online.de...
> Hi,
>
> I have a custom collection class that inherits from HashTable. When I add > a
> new CustomObject to my CustomCollection I would like to ensure that it
> will
> only accept objects of type CustomObject. How would I do this? Thanks in > advance.
>
> Public Class CustomCollection
> Inherits Hashtable
> ' default constructor
> Public Sub New()
> End Sub
> End Class
>
> ' this is how I would add new objects
> Dim customCollection As New CustomCollection
> 'Now, fill it with some custom objects
> customCollection.Add(1, New CustomObject("the key", "the value"))
>
>



Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: craig | last post by:
Assume that you would like to create a custom collection class that is: 1. Strongly-typed (only holds Customer objects) 2. Read-only (user cannot add Customer objects) 3. Able to be bound to...
3
by: JimGreen | last post by:
We are designing a WinForm application ( three tiered) There is a debate in our group as to whether we should pass datasets or our custom collections from business layer to the user interface...
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
6
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without...
4
by: John Dalberg | last post by:
I noticed the starterkits timetracker & issue tracker load data from a database into custom collections (arraylists) which bind to a datagrid. What are the advantages of using custom collections...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
0
by: Rajesh | last post by:
I am trying to use a simple custom type for saving the profile information of a user. The custom class inherits from the Hashtable. I am serializing the type as "Binary" in the provider sections of...
0
by: w31chang | last post by:
Hi, I create custom entity objects, a custom collections for the entity objects and a custom object that acts like a collection of the custom entity collections which I called EntitySet Object. The...
4
balabaster
by: balabaster | last post by:
Okay, I decided that I needed to understand the whole custom configuration file bits and so I've spent some time playing around with it. It seems (in the most) relatively straight forward. However,...
1
by: asharda | last post by:
I have a custom property grid. I am using custom property grid as I do not want the error messages that the propertygrid shows when abphabets are entered in interger fields. The custom property...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.