Solved

Return a Type from a collection reference

Posted on 2007-03-29
4
194 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

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

12 Experts available now in Live!

Get 1:1 Help Now