?
Solved

System.Collections

Posted on 2006-07-14
8
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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