473,503 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2168
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
4818
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
2225
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
1422
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
2307
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
2110
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
3771
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
1408
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
1138
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
2679
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
4363
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
7271
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
7319
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...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.