Solved

System.Collections

Posted on 2006-07-14
8
155 Views
Last Modified: 2010-04-23
Whats the system.collections equivalent of

dim x as New Collection

???

I am looking for something like New Collection but type safe (using Of T) and able to give a collection.Exists function ? I'm a bit confused by all the stuff available in system.collections and system.collections.generics. I just want a collection of a local class.

thanks
0
Comment
Question by:plq
  • 4
  • 4
8 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17107621
You're using Generics... :) Good for you! You'll love them

Imports System.Collections.Generics
Dim x as New List(of MyClass)
0
 
LVL 8

Author Comment

by:plq
ID: 17107670
thanks for this. Given that you can't specify a key in this collection type, how do you specify which class attribute forms the key to use when doing an Exists ?

0
 
LVL 8

Author Comment

by:plq
ID: 17107702
Scrub that, it takes an object.

I have a class with a key string value and other attributes, and I want to retrieve the member in that collection which has that key value. Just like you can with the old "Collection" type.




0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17107713
I just noticed that requirement, and have been working on a code sample for you.

If you simplly want "Exists" as you said, you need to do some convoluted stuff with predicates. Here's a basic sample.

Public Class Form1
  Dim values As List(Of String)
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    values = New List(Of String)
    values.Add("value1")
    MessageBox.Show(ValueExistsInList("value2").ToString)
    values.Add("value2")
    MessageBox.Show(ValueExistsInList("value2").ToString)
  End Sub

  Private Function ValueExistsInList(ByVal value As String) As Boolean
    _value = value
    Return values.Exists(AddressOf ValueExists)
  End Function

  Private _value As String
  Private Function ValueExists(ByVal input As String) As Boolean
    Return (input = _value)
  End Function
End Class
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 125 total points
ID: 17107737
Now for the easy answer.... use the Dictionary(Of T, T)

The first type is your key type, the second is your value type. So, you'd probably be using something like Dictionary(Of Integer, Record)

The Dictionary has a ContainsKey method that will tell you if it exists. Getting the value back is just like using a hashtable (but type-safe)

    Dim myDictionary As New Dictionary(Of Integer, Record)
    Dim id As Integer = 27
    Dim thisRecord As Record
    If myDictionary.ContainsKey(id) Then thisRecord = myDictionary(id)
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17107764
This is the simplest example I could think of that demonstrates what you're after:

Public Class myObject
  Private _key As String
  Public Property Key() As String
    Get
      Return _key
    End Get
    Set(ByVal value As String)
      _key = value
    End Set
  End Property
End Class

Public Class myObjectCollection
  Inherits Dictionary(Of String, myObject)

  Public Overloads Sub Add(ByVal value As myObject)
    Me.Add(value.Key, value)
  End Sub

  Public Function Exists(ByVal value As String) As Boolean
    Return Me.ContainsKey("value")
  End Function
0
 
LVL 8

Author Comment

by:plq
ID: 17107806
Yes this is looking good. Let me have a mess around with it and I will come back.

thanks
0
 
LVL 8

Author Comment

by:plq
ID: 17107991
thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question