Solved

System.Collections

Posted on 2006-07-14
8
153 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

896 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now