?
Solved

Return a Type from a collection reference

Posted on 2007-03-29
4
Medium Priority
?
199 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
[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
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:syscomdev
ID: 18819475
2005
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

770 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