3 Replies - 1171 Views - Last Post: 04 August 2016 - 12:33 PM Rate Topic: -----

#1 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 26-March 12

VB6 to VB.Net

Posted 01 July 2016 - 12:02 PM

I am converting VB6 to VB.Net.
The code below shows the VB6 Class code, shortened to save space, it read from a flat file.
Then half way down, I show how I would write it in VB.Net, using a MySQL table.
Is there a better way to write it in VB.Net? Is it good to use a structure, but if you have to add fields to the MySQL table, then you always need to add to the structure.
Also, can you do this VB6 statement in one line in VB.NET: Get fn_FIE1, SampleNumber, Fie1
Or do you have to move the fields individually: Fie1.Growername = .item("growername")
Fie1.Address1 = .Item("address1")
Fie1.Address2 = .Item("address2")


Option Explicit
Private Type Fie1Record
    GrowerName As String * 25
    GrowerAddress1 As String * 20
    GrowerAddress2 As String * 20
End Type

Private Fie1 As Fie1Record
Private GotSample As Boolean

Public Sub GetSampleInfo(ByVal SampleNumber As Long, ByVal YearExtension As String, Optional ByVal DataFilePath As String = DefaultDataFilePath)
    Dim FileName As String
            
    On Error GoTo Problem
    
    If GotSample Then
        MsgBox "You cannot use GetSampleInfo again with this instance of clsFie. Please destroy the object first and create a new one if you want a new sample.", vbExclamation, "Not Allowed!"
        Exit Sub
    End If
    
    GotSample = True
    
    On Error GoTo OpenError
    FileName = "FIE189"
    Open DataFilePath & "FIE189" & YearExtension For Random Shared As fn_FIE1 Len = 300
    
    On Error GoTo Problem
    
    Get fn_FIE1, SampleNumber, Fie1
        
    Close fn_FIE1
        
    On Error GoTo 0
Exit Sub
    
OpenError:
    MsgBox "Error in clsFilesFie.GetSampleInfo while opening " & FileName & "! File could not be opened!" & vbCrLf & "Error: " & Err.Number & " " & Err.Description, vbExclamation, "ERROR!"
    GoTo CloseFiles
Exit Sub

Problem:
    MsgBox "Error in clsFileFie.GetSampleInfo" & vbCrLf & "Error: " & Err.Number & " " & Err.Description, vbExclamation, "FILE ERROR!"
    GoTo CloseFiles
Exit Sub

CloseFiles:
    Close fn_FIE1
    Err.Raise Err.Number

End Sub

Public Property Get GrowerName() As String: GrowerName = Trim(Fie1.GrowerName): End Property
Public Property Let GrowerName(ByVal NewValue As String): Fie1.GrowerName = Trim(NewValue): End Property

Public Property Get GrowerAddress(ByVal Index As Integer) As String
    Select Case Index
        Case 1: GrowerAddress = Trim(Fie1.GrowerAddress1)
        Case 2: GrowerAddress = Trim(Fie1.GrowerAddress2)
        Case Else: GrowerAddress = ""
    End Select
End Property
Public Property Let GrowerAddress(ByVal Index As Integer, ByVal NewValue As String)
    Select Case Index
        Case 1: Fie1.GrowerAddress1 = Trim(NewValue)
        Case 2: Fie1.GrowerAddress2 = Trim(NewValue)
    End Select
End Property


_______________________________________________________________________________________________
VB.NET
Option Strict Off
Option Explicit On
Imports MySql.Data.MySqlClient

Private Structure Fie1Record
    Public Growername As String
    Public Address1 As String
    Public Address2 As String
End Structure

Private Fie1 As Fie1Record

Public Sub GetSampleInfo(ByVal SampleNumber As Integer, ByVal YearFour As Integer, ByVal myConnStr As String)
    Try
        Using myconnection = New MySqlConnection(myConnStr)
            myconnection.Open()
            Using mycommand As MySqlCommand = myconnection.CreateCommand()
                With mycommand
                    .CommandText = "SELECT Year, LabNo, GrowerName, Address1, Address2 " & _
                                   "FROM FIE " & _
                                   "WHERE year = @year and labno = @labno"
                    With .Parameters
                        .AddWithValue("@labno", SampleNumber)
                        .AddWithValue("@year", YearFour) 
                    End With
                End With
                Using myreader As MySqlDataReader = mycommand.ExecuteReader()
                    With myreader
                        If .HasRows Then
                            .Read()
                            Fie1.Growername = .item("growername")
                            Fie1.Address1 = .Item("address1")
                            Fie1.Address2 = .Item("address2")
                        End If
                    End With
                End Using
            End Using
        End Using
    Catch ex As Exception
        MsgBox("Error in GetSampleInfo." & vbCrLf & ex.ToString)
    End Try
End Sub

Public Property GrowerName() As String
        Get
            GrowerName = Trim(Fie1.Growername)
        End Get
        Set(ByVal Value As String)
            Fie1.Growername = Trim(Value)
        End Set
End Property

Public Property Address(ByVal Index As Short) As Short
    Get
        Select Case Index
            Case 1 : Address = Fie1.Address1
            Case 2 : Address = Fie1.Address2
            Case Else : Address = 0
        End Select
    End Get
    Set(ByVal Value As Short)
        Select Case Index
            Case 1 : Fie1.Address1 = Value
            Case 2 : Fie1.Address2 = Value
        End Select
    End Set
End Property


This post has been edited by modi123_1: 01 July 2016 - 12:07 PM
Reason for edit:: broke the code into two sections for better comparison. Moved to vb.net


Is This A Good Question/Topic? 0
  • +

Replies To: VB6 to VB.Net

#2 thecoat  Icon User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: VB6 to VB.Net

Posted 12 July 2016 - 12:05 AM

note on code examples... they are just that to give you some ideas. Typically you wouldn't want a data tier throwing up message boxes for example.

I imagine no one has touched this because it can be a rather broad subject. Simply converting VB6 code to VB.NET will gain you some benefit in being able to maintain a new app with new tools, but it doesn't really leverage everything, and ideally you want to redesign with an object oriented approach, which appears to be the path you are on so you are on the right track.

You may want to look into n-tier concepts, although you are pretty close to that in some ways with what you are doing. I would eliminate your structure in this case and use private fields exposed with public properties. Something like the following:

Public Class Grower
    Private m_growername As String
    Public Property GrowerName() As String
        Get
            Return m_growername
        End Get
        Set(ByVal Value As String)
            m_growername = Trim(Value)
        End Set
    End Property

    Private m_address1 As String
    Public Property Address1() As String
        Get
            Return m_address1
        End Get
        Set(ByVal value As String)
            m_address1 = value
        End Set
    End Property

    Private m_address2 As String
    Public Property Address2() As String
        Get
            Return m_address2
        End Get
        Set(ByVal value As String)
            m_address2 = value
        End Set
    End Property

    Sub New() 'Default Implicit constructor

    End Sub

    Sub New(ByVal strGrowerName As String)
        GrowerName = strGrowerName
    End Sub

    Public Function GetAddress(ByVal intIndex As Integer) As String
        Select Case intIndex
            Case 1
                Return Address1
            Case 2
                Return Address2
            Case Else
                Return Address1
        End Select
    End Function
End Class


A data tier can manage your connection and provide function calls for CRUD operations.
Public Class DataTier
    Private m_connection As MySqlConnection
    Public ReadOnly Property Connection() As MySqlConnection
        Get
            Return m_connection
        End Get
    End Property

    Sub New(ByVal strConnStr As String)
        m_connection = New MySqlConnection(strConnStr)
    End Sub
    Public Function GetSampleInfo(ByVal SampleNumber As Integer, ByVal YearFour As Integer) As Grower
        Dim g As New Grower 'doing new here in that at least if there is an error getting data we return an instatiated object.
        Try

            Connection.Open()
            Using mycommand As MySqlCommand = Connection.CreateCommand()
                With mycommand
                    .CommandText = "SELECT Year, LabNo, GrowerName, Address1, Address2 " &
                                       "FROM FIE " &
                                       "WHERE year = @year and labno = @labno"
                    With .Parameters
                        .AddWithValue("@labno", SampleNumber)
                        .AddWithValue("@year", YearFour)
                    End With
                End With
                Using myreader As MySqlDataReader = mycommand.ExecuteReader()
                    With myreader
                        If .HasRows Then
                            .Read()
                            g = New Grower(.item("growername"))
                            g.Address1 = .Item("address1")
                            g.Address2 = .Item("address2")
                        End If
                    End With
                End Using
            End Using
        Catch ex As Exception
            MsgBox("Error in GetSampleInfo." & vbCrLf & ex.ToString)
        Finally
           Connection.Close()
        End Try
        Return g
    End Function
End Class



Just like with structures changes to the data may require you change the class object and from that stand point it's more work, but it also means you can make "smart" objects that can react to data plugged into them. You can bind the objects to UI elements, validate user input etc.

Want to implement unit tests? You can unit test your objects independent of a data source or you could even create a mock data tier.
Was This Post Helpful? 1
  • +
  • -

#3 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 26-March 12

Re: VB6 to VB.Net

Posted 04 August 2016 - 12:31 PM

Thank you for giving me some ideas. I had given up on anyone answering me! I had already removed the structure and added properties but I will take a look at the rest of your suggestions.
Thanks again.

This post has been edited by [email protected]: 04 August 2016 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: VB6 to VB.Net

Posted 04 August 2016 - 12:33 PM

@OP There is no need to quote the previous post, especially in full, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1