Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Return a Type from a collection reference

Posted on 2007-03-29
4
Medium Priority
?
201 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

604 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