473,795 Members | 2,968 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 CustomCollectio n I would like to ensure that it will
only accept objects of type CustomObject. How would I do this? Thanks in
advance.

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

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

Public Class CustomObjectCol lection
Inherits DictionaryBase

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

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.I nnerHashtable.I tem(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.d e> 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 CustomCollectio n I would like to ensure that it
will
only accept objects of type CustomObject. How would I do this? Thanks in
advance.

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

' this is how I would add new objects
Dim customCollectio n As New CustomCollectio n
'Now, fill it with some custom objects
customCollectio n.Add(1, New CustomObject("t he 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******** *****@TK2MSFTNG P12.phx.gbl...
Rufus,
Rather then inherit from HashTable, inherit from DictionaryBase.

Public Class CustomObjectCol lection
Inherits DictionaryBase

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

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.I nnerHashtable.I tem(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.d e> 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 CustomCollectio n I would like to ensure that it
will
only accept objects of type CustomObject. How would I do this? Thanks in advance.

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

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


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

Return DirectCast(Me.I nnerHashtable.I tem(key), CustomObject)

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


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

Public Class CustomObjectCol lection
Inherits DictionaryBase

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

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.I nnerHashtable.I tem(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.d e> 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 CustomCollectio n I would like to ensure that it
will
only accept objects of type CustomObject. How would I do this? Thanks in advance.

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

' this is how I would add new objects
Dim customCollectio n As New CustomCollectio n
'Now, fill it with some custom objects
customCollectio n.Add(1, New CustomObject("t he 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.d e> wrote in message news:ck******** **@online.de...
Just one question though...why this line:

Return DirectCast(Me.I nnerHashtable.I tem(key), CustomObject)

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


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

Public Class CustomObjectCol lection
Inherits DictionaryBase

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

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.I nnerHashtable.I tem(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.d e> 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 CustomCollectio n I would like to ensure that it
> will
> only accept objects of type CustomObject. How would I do this? Thanks in > advance.
>
> Public Class CustomCollectio n
> Inherits Hashtable
> ' default constructor
> Public Sub New()
> End Sub
> End Class
>
> ' this is how I would add new objects
> Dim customCollectio n As New CustomCollectio n
> 'Now, fill it with some custom objects
> customCollectio n.Add(1, New CustomObject("t he key", "the value"))
>
>



Jul 21 '05 #5
Rufus,
You are correct the object being returned by Me.InnerHashtab le is actually a
CustomObject, however the return type of Me.InnerHashtab le is Object. The
DirectCast is telling the compiler that even though the return type of
Me.InnerHashtab le is Object, you know its really a CustomObject being
returned. If you managed to get an object other then CustomObject into
Me.InnerHashtab le then the DirectCast would cause an InvalidCastExce ption 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 CustomObjectCol lection
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.K ey, Integer)
Dim value As CustomObject = DirectCast(de.V alue, CustomObject)
Debug.WriteLine (value, key.ToString)
Next

You will get an exception on the "Dim key As Integer = DirectCast(de.K ey,
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 ArgumentExcepti on on the "dictionary.Add " line,
showing exactly where the problem is.

Public Class CustomObjectCol lection
Inherits DictionaryBase

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

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

Protected Overrides Sub OnValidate(ByVa l key As Object, ByVal value
As Object)
If Not TypeOf key Is Integer Then Throw New
ArgumentExcepti on("Key must be type Integer", "key")
If Not TypeOf value Is CustomObject Then Throw New
ArgumentExcepti on("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.d e> wrote in message news:ck******** **@online.de...
Just one question though...why this line:

Return DirectCast(Me.I nnerHashtable.I tem(key), CustomObject)

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


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

Public Class CustomObjectCol lection
Inherits DictionaryBase

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

Default Public ReadOnly Property Item(ByVal key As Integer) As
CustomObject
Get
Return DirectCast(Me.I nnerHashtable.I tem(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.d e> 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 CustomCollectio n I would like to ensure that it
> will
> only accept objects of type CustomObject. How would I do this? Thanks in > advance.
>
> Public Class CustomCollectio n
> Inherits Hashtable
> ' default constructor
> Public Sub New()
> End Sub
> End Class
>
> ' this is how I would add new objects
> Dim customCollectio n As New CustomCollectio n
> 'Now, fill it with some custom objects
> customCollectio n.Add(1, New CustomObject("t he 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
4833
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 a winforms datagrid (must implement the IList interface). What would be a good way to create this class? 1. The CollectionBase abstract base class can be used, but it is not
3
2243
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 layer. The data is stored in memory (fetched from DB only once). The collection is dynamic in the sense that it can change. My personal opinion is that using dataset makes you code take much more memory than a custom collection. Datasets have...
0
1454
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 the 2nd property is a strongly typed collection class (myCollectionProperty). The collection contains a simple class (myCustomClass) which has 1 text property (TextProperty).
6
2337
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 having to do a httpwebreqeust call.
4
2158
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 over simpler objects like datareaders or datatables? John Dalberg
9
3806
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
0
1428
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 the web.config. When I add a key-value to the custom type, I can see that its being saved in the database (aspnet_profiles table - PropertyValuesBinary column). But, when I want to read the information in the profile, my custom type object is...
0
1153
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 EntitySet is used to bind the entity collections to windows form bindingsource in design time. After 2 weeks of bloody coding and reading really "Helpfull" msdn docs, thanks to http://www.freeweb.hu/noiseehc/ finally I've managed to create the...
4
2699
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, I've come unstuck with one scenario that I'm trying to figure out the code for: Consider the following configuration: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="DummySection"...
1
4386
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 grid doesn't show colloections i.e. if I have a List<Objectthen the Collection is shown but when I click on the "..." button next to it nothing comes up. If I say CustomPropertyGrid p = new CustomPropertyGrid(); p.SelectedObject = new...
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7541
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.