Solved

System.Collections

Posted on 2006-07-14
8
156 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
Independent Software Vendors: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Very Large data in MYSQL 7 95
VB.NET Textbox input validation 4 66
ASP.NET Calendar Control 5 31
Cannot kill Excel from taskmgr after Excel Export in VB 2014 6 39
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

697 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