Solved

Return a Type from a collection reference

Posted on 2007-03-29
4
195 Views
Last Modified: 2010-04-23
I have the following

Private Class test

Private displayValue As String
Private dbType As Integer
Property Display() As String
      Get
            Return displayValue
      End Get
      Set(ByVal value As String)
            displayValue = value
      End Set
End Property

Property DataType() As Integer
      Get
            Return dbType
      End Get
      Set(ByVal value As Integer)
            dbType = value
      End Set
End Property

Public Sub New(ByVal dspl As String, ByVal dt As Integer)
      Display = dspl
      DataType = dt
End Sub
End Class

dim tests as Collection

I can successfully add items to the collection
   tests.add(new test("steve",1), "steve")
   tests.add(new test("don",2), "don")

I am trying to do the following:
  tests("Steve").DataType = 4

which fails because Option Strict prohibits late binding.

I could do ctype(tests("Steve"), test).DataType = 4 but that gets cumbersome.

Would I have to create a new class which inherits the collection class to implement this? If so, is there an FAQ showing how to do this?

So far, the two problems I have had with this are:
1. When implementing the item property, List()  only accepts numeric
2. It appears I have to add every feature of a collection (contains, add, etc.)

I would have thought it would be simpler to do, but maybe its wishful thinking
0
Comment
Question by:syscomdev
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18819455
What version VB.Net?...
0
 

Author Comment

by:syscomdev
ID: 18819475
2005
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18819540
Glad you said VB.Net 2005...

You can use the new Generics Dictionary to achieve exactly what you want.

Look CLOSELY though, the Add() method has the key and value parameters REVERSED from what you were using before:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tests As New Dictionary(Of String, Test)

        tests.Add("steve", New Test("steve", 1))
        tests.Add("don", New Test("don", 2))

        tests("Steve").DataType = 4
    End Sub

    Private Class Test

        Private displayValue As String
        Private dbType As Integer

        Property Display() As String
            Get
                Return displayValue
            End Get
            Set(ByVal value As String)
                displayValue = value
            End Set
        End Property

        Property DataType() As Integer
            Get
                Return dbType
            End Get
            Set(ByVal value As Integer)
                dbType = value
            End Set
        End Property

        Public Sub New(ByVal dspl As String, ByVal dt As Integer)
            Me.Display = dspl
            Me.DataType = dt
        End Sub

    End Class

End Class
0
 

Author Comment

by:syscomdev
ID: 18819798
thank you very much.

0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…

803 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