Solved

System.Collections

Posted on 2006-07-14
8
151 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 24

Accepted Solution

by:
Jeff Certain earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

7 Experts available now in Live!

Get 1:1 Help Now