Solved

Return a Type from a collection reference

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

13 Experts available now in Live!

Get 1:1 Help Now