I'm more familiar with C#, but I'll take a stab at it.

I think Test1 is attempting to create new Object and store it's reference in
obj, then create a new dateformatter and overwrite the ref previously stored
in obj.  This is not a cast.  The type of obj is still Object.  I think you
can just do:

Dim obj as Object
obj = New DateFormatter.YearFormatter

In Test2, you are dimensioning the same var twice.  I think what you want
is:

        'Test2
        'Dim obj As New DateFormatter.YearFormatter

          'Lets just pretend that there is logic that could cause either one
to be hit.
          'For this sample, I'm forcing it to the first case.
        Dim int As Integer = 0
          Dim str as String
        Select Case int
            Case 0
                str = obj.PreY2K.YY
            Case 1
                str = obj.PostY2K.YYYY
        End Select

But, your code does not know that your object has PreY2K and PostY2K
properties since you only have an Object reference.  Since you only have the
one type you can just change the declaration to be that type:

Dim dtFmter as DateFormatter.YearFormatter

If you have more than one time and that is the reason you think you need to
Dim as an Object, then you might need to introduce a class hierarchy or an
interface.  Give us more details on what you are trying to do.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Jon Rothlander
Sent: Friday, September 08, 2006 10:06 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Need help with a simple VB.Net Problem

I'm hoping that someone can take a few minutes and take a look at the
following code for me.  It may look complex at first, but it actually is
very simple.  What I have here is a VB.Net WinForm with no controls on the
form.  When it executes, it runs through a sample app that I set up that
recreates the problem that I am having, but it's not the same application.
I wrote a sample that is based on a Y2K sample that I just made up because
what I am actually working on is a compiler and there's just too much in the
code to post the real thing.  So this sample recreates the same problem, but
keep in mind that it's not the same code so my solutions may be limited.

If you create a WinForm and paste the code and close at the bottom of this
post, you will see the following in a Test() function at the bottom of the
code...

          'Test1
        Dim obj As New Object
        obj = New DateFormatter.YearFormatter

        'Test2
        'Dim obj As New DateFormatter.YearFormatter

          'Lets just pretend that there is logic that could cause either one
to be hit.
          'For this sample, I'm forcing it to the first case.
        Dim int As Integer = 0
        Select Case int
            Case 0
                Dim str As String = obj.PreY2K.YY
            Case 1
                Dim str As String = obj.PostY2K.YYYY
        End Select

To see what my problem is.  Use the code below and execute with a breakpoint
on the line after the Test1 comment.  You will get an error on the "dim str"
line.  Then comment out the Test1 code and uncomment the Test2 code.  Run it
again and note that you do not get the error.  What's the difference?

Note that Test1 uses a generic object and then casts it as the YearFormatter
class.  This causes an error when it hits the code that looks at the
PreY2K.YY property.  However in Test2 if I just go ahead and create it obj
as that type, then I do not get an error.  I do not see what the difference
between the two is, but apparently I am overlooking something significant.

The problem is that I have been using logic like Test2 and I need to change
it to Test1 using a generic object.  I need to make it generic because I
have a number of different class that would be equivalent to YearFormatter.
Maybe it would be sort of like having YearFormmater, YearFormatterY2K, and
YearFormatterPreY2K classes, then needed the logic of the code to pick which
one to use.  So I need to be able to control which class becomes the obj
object and set it accordingly.

In the end, I just need to figure out how to get Test1 to work like Test2.
Any ideas?  Any suggestions or ideas would be very much appreciated.  I
think I am over looking something simple here.

My sample code is below.

Best regards,
Greg Rothlander


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim obj As New SomeClass
        obj.Test()
    End Sub

End Class


Class DateFormatter

    Class YearFormatter

        Class PreY2K
            ' Definition for pre y2k file.
            ' YY
            Private Shared _YY As String = ""
            Public Shared Property YY() As String
                Get
                    Return _YY
                End Get
                Set(ByVal Value As String)
                    _YY = Value
                End Set
            End Property

            Public Shared Function ConvertToY2K() As String
                If CInt(YY > 50) Then
                    Return "19" & YY
                Else
                    Return "20" & YY
                End If
            End Function
        End Class

        Class PostY2K
            ' Definition for post y2k file.
            ' YYYY
            Private Shared _YYYY As String = ""
            Public Shared Property YYYY() As String
                Get
                    Return _YYYY
                End Get
                Set(ByVal Value As String)
                    _YYYY = Value
                End Set
            End Property

            Public Shared Function ConvertToPreY2K() As String
                Return Mid(YYYY, 3, 2)
            End Function
        End Class

    End Class

End Class

Class SomeClass

    Public Function Test() As String

        'Test1
        Dim obj As New Object
        obj = New DateFormatter.YearFormatter

        'Test2
        'Dim obj As New DateFormatter.YearFormatter

        Dim int As Integer = 0

        Select Case int
            Case 0
                Dim str As String = obj.PreY2K.YY
            Case 1
                Dim str As String = obj.PostY2K.YYYY
        End Select

    End Function

End Class

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to