Results 1 to 15 of 15

Thread: Is there anything really wrong with this code?

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Is there anything really wrong with this code?

    I'm a VB6 developer myself mostly, and am just starting with .Net
    I've no formal training in .Net whatsoever, but have just forged ahead and tried to write an application.

    Is there anything really wrong or stupid with this app so far?
    i.e. dodgy coding practices that have carreid over from vb6 etc. ?

    Form1.vb
    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class Form1
    4.  
    5.     Inherits System.Windows.Forms.Form
    6.     Private WithEvents mySqlConnection As sqlConnection
    7.     Private mySQLReader As SqlDataReader
    8.     Private blnHaveCleared As Boolean = False
    9.     Private lngLineCount As Long, lngCurrLine As Long
    10.     Private colHeaders As New Collection()
    11.  
    12. #Region " Windows Form Designer generated code "
    13.    . . . just adds a listbox and a button
    14. #End Region
    15.  
    16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    17.  
    18.     End Sub
    19.  
    20.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    21.         mySqlConnection = New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;User ID=usrShop;Initial Catalog=dbShop;Data Source=JAMIETOSH;")
    22.         mySqlConnection.Open()
    23.     End Sub
    24.  
    25.     Private Sub mySqlConnection_StateChange(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles mySqlConnection.StateChange
    26.         If e.CurrentState = ConnectionState.Open Then
    27.             Dim intFileNumber As Integer = FreeFile(), strFileContent As String
    28.             Dim strArr() As String, strFields() As String
    29.             Dim strHeaders() As String, lngColIndex As Long, i As Long
    30.             FileOpen(intFileNumber, "c:\test.txt", OpenMode.Input)
    31.             strFileContent = InputString(intFileNumber, LOF(intFileNumber))
    32.             strArr = Split(strFileContent, vbCrLf)
    33.             lngLineCount = UBound(strArr)
    34.             For lngCurrLine = 0 To 100 'lngLineCount
    35.                 If Not lngCurrLine = 0 Then
    36.                     strFields = Split(strArr(lngCurrLine), vbTab)
    37.                     sqlAddHeaders(strFields(1), strFields(0), mySqlConnection)
    38.                     'sqlAddProductToDB(strArr(lngCurrLine), mySqlConnection)
    39.                 End If
    40.             Next
    41.             FileClose(intFileNumber)
    42.             mySqlConnection.Close()
    43.         End If
    44.     End Sub
    45.  
    46. End Class
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    modMisc.vb
    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Module modMisc
    4.  
    5. #Region "Declarations ..."
    6.     Private blnHaveCleared(1) As Boolean
    7.  
    8.     Private colMasterHeaders() As clsColumnReference
    9.     Private colSecondHeaders() As clsColumnReference
    10.     Private colThirdHeaders() As clsColumnReference
    11.  
    12.     Private Const constMasterHeader As Long = 0
    13.     Private Const constSecondaryHeader As Long = 1
    14.     Private Const constThirdHeader As Long = 2
    15.  
    16. #End Region
    17.  
    18.  
    19. #Region "clsColumnReference()"
    20.     Class clsColumnReference
    21.         Public strColumnText As String
    22.         Public lngParentIndex As Long
    23.         Public blnIsMasterParent As Boolean
    24.         Public strChildSAPCodes As String
    25.     End Class
    26. #End Region
    27.  
    28.  
    29. #Region "sqlAddProductToDB()"
    30.     Public Sub sqlAddProductToDB(ByVal strData As String, ByVal mySqlConnection As SqlConnection)
    31.         Dim strSQLStatement As String
    32.         If Not blnHaveCleared(0) Then
    33.             strSQLStatement = "DELETE FROM tblProducts WHERE strProductReference = strProductReference;"
    34.         Else
    35.             strData = Replace$(strData, "'", vbNullString)
    36.             strData = Replace$(strData, vbTab, "','")
    37.             strSQLStatement = _
    38.                 "INSERT INTO tblProducts " & _
    39.                 "(strProductReference, strSectionText, strShortDescription, strDetailURL, strTradePrice, strPrice, strDetailLink, strFullDescription, strRRP, strStock) VALUES " & _
    40.                 "(" & "'" & strData & "'" & ");"
    41.         End If
    42.         Dim mySQLCommand As New SqlCommand(strSQLStatement, mySqlConnection)
    43.         'ListBox1.Items.Add(strPrepare(strSQLStatement))        
    44.         Try
    45.             mySQLCommand.ExecuteNonQuery()
    46.         Catch ex As Exception
    47.             MsgBox(ex.Message & vbCrLf & ex.StackTrace & vbCrLf & vbCrLf & strSQLStatement)
    48.         End Try
    49.         If Not blnHaveCleared(0) Then
    50.             blnHaveCleared(0) = True
    51.             sqlAddProductToDB(strData, mySqlConnection)
    52.         End If
    53.     End Sub
    54. #End Region
    55.  
    56.  
    57. #Region "doInitHeaders()"
    58.     Private Sub doInitHeaders()
    59.         If colMasterHeaders Is Nothing Then ReDim colMasterHeaders(0) : colMasterHeaders(0) = New clsColumnReference()
    60.         If colSecondHeaders Is Nothing Then ReDim colSecondHeaders(0) : colSecondHeaders(0) = New clsColumnReference()
    61.         If colThirdHeaders Is Nothing Then ReDim colThirdHeaders(0) : colThirdHeaders(0) = New clsColumnReference()
    62.     End Sub
    63. #End Region
    64.  
    65.  
    66. #Region "sqlAddHeaders()"
    67.     Public Sub sqlAddHeaders(ByVal strData As String, ByVal strChildSAPCode As String, ByVal mySqlConnection As SqlConnection)
    68.         Dim i As Long, strHeaders() As String = Split(strData, "|"), lngParentIndex As Long
    69.         Dim lngHeaderIndex(2) As Long
    70.         Dim blnFoundText As Boolean = False : doInitHeaders()
    71.  
    72.         Dim blnAddHeader(2) As Boolean
    73.         Dim strHeaderTable(2) As String
    74.         strHeaderTable(0) = "tblMasterHeaders"
    75.         strHeaderTable(1) = "tblSecondaryHeaders"
    76.         strHeaderTable(2) = "tblThirdHeaders"
    77.         For i = 0 To 2
    78.             blnAddHeader(i) = False
    79.         Next
    80.  
    81.  
    82.         '''''''''''''''''''''''''''''
    83.         '' Split up column headers ''
    84.         '''''''''''''''''''''''''''''
    85.  
    86.         '' Master Headers
    87.         ''
    88.         For i = 0 To UBound(colMasterHeaders)
    89.             With colMasterHeaders(i)
    90.                 If strHeaders(0) = .strColumnText Then
    91.                     blnFoundText = True
    92.                     lngParentIndex = i
    93.                     Exit For
    94.                 End If
    95.             End With
    96.         Next
    97.         If Not blnFoundText Then
    98.             ReDim Preserve colMasterHeaders(UBound(colMasterHeaders) + 1)
    99.             colMasterHeaders(UBound(colMasterHeaders)) = New clsColumnReference()
    100.             With colMasterHeaders(UBound(colMasterHeaders))
    101.                 .blnIsMasterParent = True
    102.                 .lngParentIndex = -1
    103.                 .strColumnText = strHeaders(0)
    104.                 lngParentIndex = UBound(colMasterHeaders)
    105.                 blnAddHeader(constMasterHeader) = True                
    106.             End With
    107.         End If
    108.         blnFoundText = False
    109.         lngHeaderIndex(0) = lngParentIndex
    110.  
    111.  
    112.         '' Secondary Headers
    113.         ''
    114.         For i = 0 To UBound(colSecondHeaders)
    115.             With colSecondHeaders(i)
    116.                 If strHeaders(1) = .strColumnText Then
    117.                     blnFoundText = True
    118.                     lngParentIndex = i
    119.                     Exit For
    120.                 End If
    121.             End With
    122.         Next
    123.         If Not blnFoundText Then
    124.             ReDim Preserve colSecondHeaders(UBound(colSecondHeaders) + 1)
    125.             colSecondHeaders(UBound(colSecondHeaders)) = New clsColumnReference()
    126.             With colSecondHeaders(UBound(colSecondHeaders))
    127.                 .blnIsMasterParent = False
    128.                 .lngParentIndex = lngParentIndex
    129.                 .strColumnText = strHeaders(1)
    130.                 lngParentIndex = i
    131.                 blnAddHeader(constSecondaryHeader) = True
    132.             End With
    133.         End If
    134.         blnFoundText = False
    135.         lngHeaderIndex(1) = lngParentIndex
    136.  
    137.         '' Third Headers
    138.         ''
    139.         For i = 0 To UBound(colThirdHeaders)
    140.             If strHeaders(2) = colThirdHeaders(i).strColumnText Then
    141.                 colThirdHeaders(i).strChildSAPCodes += "," & strChildSAPCode
    142.                 blnFoundText = True
    143.                 Exit For
    144.             End If
    145.         Next
    146.         If Not blnFoundText Then
    147.             ReDim Preserve colThirdHeaders(UBound(colThirdHeaders) + 1)
    148.             colThirdHeaders(UBound(colThirdHeaders)) = New clsColumnReference()
    149.             With colThirdHeaders(UBound(colThirdHeaders))
    150.                 .blnIsMasterParent = False
    151.                 .lngParentIndex = lngParentIndex
    152.                 .strColumnText = strHeaders(2)
    153.                 .strChildSAPCodes = strChildSAPCode
    154.                 blnAddHeader(constThirdHeader) = True
    155.             End With
    156.         End If
    157.         lngHeaderIndex(2) = lngParentIndex
    158.  
    159.         '''''''''''''''''''''''''''''
    160.         '' Insert into SQL DB      ''
    161.         '''''''''''''''''''''''''''''
    162.         Dim strSQLStatement As String, mySQLCommand As SqlCommand
    163.  
    164.         If Not blnHaveCleared(1) Then
    165.             For i = 0 To 2
    166.                 strSQLStatement = "DELETE FROM " & strHeaderTable(i) & " WHERE lngParentIndex = lngParentIndex"
    167.                 mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    168.                 mySQLCommand.ExecuteNonQuery()
    169.             Next
    170.             mySQLCommand = Nothing
    171.             blnHaveCleared(1) = True
    172.         End If
    173.         If blnAddHeader(0) Then
    174.             strSQLStatement = "INSERT INTO " & strHeaderTable(0) & vbCrLf & _
    175.                                 "VALUES ( "
    176.             strSQLStatement += lngHeaderIndex(0) & "," & -1 & "," & "'" & colMasterHeaders(lngHeaderIndex(0)).strColumnText & "'"
    177.             strSQLStatement += ");"
    178.             mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    179.             mySQLCommand.ExecuteNonQuery()
    180.             mySQLCommand = Nothing
    181.         End If
    182.         If blnAddHeader(1) Then
    183.             strSQLStatement = "INSERT INTO " & strHeaderTable(1) & vbCrLf & _
    184.                                 "VALUES ( "
    185.             strSQLStatement += lngHeaderIndex(1) & "," & colSecondHeaders(lngHeaderIndex(1)).lngParentIndex & ",'" & colSecondHeaders(lngHeaderIndex(1)).strColumnText & "'"
    186.             strSQLStatement += ");"
    187.             mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    188.             mySQLCommand.ExecuteNonQuery()
    189.             mySQLCommand = Nothing
    190.         End If
    191.         If blnAddHeader(2) Then
    192.             strSQLStatement = "INSERT INTO " & strHeaderTable(2) & vbCrLf & _
    193.                                 "VALUES ( "
    194.             strSQLStatement += colThirdHeaders(lngHeaderIndex(2)).lngParentIndex & ",'" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "','" & colThirdHeaders(lngHeaderIndex(2)).strChildSAPCodes & "'"
    195.             strSQLStatement += ");"
    196.             mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    197.             mySQLCommand.ExecuteNonQuery()
    198.             mySQLCommand = Nothing
    199.         End If
    200.     End Sub
    201. #End Region
    202. End Module
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Right. Here's my update and finished and working modMisc.
    See my post here to see why I'm going this : http://www.vbforums.com/showthread.p...hreadid=283087

    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Module modMisc
    4.  
    5. #Region "Declarations ..."
    6.     Private blnHaveCleared(1) As Boolean
    7.  
    8.     Private colMasterHeaders() As clsColumnReference
    9.     Private colSecondHeaders() As clsColumnReference
    10.     Private colThirdHeaders() As clsColumnReference
    11.  
    12.     Private Const constMasterHeader As Long = 0
    13.     Private Const constSecondaryHeader As Long = 1
    14.     Private Const constThirdHeader As Long = 2
    15.  
    16. #End Region
    17.  
    18.  
    19. #Region "clsColumnReference()"
    20.     Class clsColumnReference
    21.         Public strColumnText As String
    22.         Public lngParentIndex As Long
    23.         Public blnIsMasterParent As Boolean
    24.         Public strChildSAPCodes As String
    25.     End Class
    26. #End Region
    27.  
    28.  
    29. #Region "sqlAddProductToDB()"
    30.     Public Sub sqlAddProductToDB(ByVal strData As String, ByVal mySqlConnection As SqlConnection)
    31.         Dim strSQLStatement As String
    32.         If Not blnHaveCleared(0) Then
    33.             strSQLStatement = "DELETE FROM tblProducts WHERE strProductReference = strProductReference;"
    34.         Else
    35.             strData = Replace$(strData, "'", vbNullString)
    36.             strData = Replace$(strData, vbTab, "','")
    37.             strSQLStatement = _
    38.                 "INSERT INTO tblProducts " & _
    39.                 "(strProductReference, strSectionText, strShortDescription, strDetailURL, strTradePrice, strPrice, strDetailLink, strFullDescription, strRRP, strStock) VALUES " & _
    40.                 "(" & "'" & strData & "'" & ");"
    41.         End If
    42.         Dim mySQLCommand As New SqlCommand(strSQLStatement, mySqlConnection)
    43.         'ListBox1.Items.Add(strPrepare(strSQLStatement))        
    44.         Try
    45.             mySQLCommand.ExecuteNonQuery()
    46.         Catch ex As Exception
    47.             MsgBox(ex.Message & vbCrLf & ex.StackTrace & vbCrLf & vbCrLf & strSQLStatement)
    48.         End Try
    49.         If Not blnHaveCleared(0) Then
    50.             blnHaveCleared(0) = True
    51.             sqlAddProductToDB(strData, mySqlConnection)
    52.         End If
    53.     End Sub
    54. #End Region
    55.  
    56.  
    57. #Region "doInitHeaders()"
    58.     Private Sub doInitHeaders()
    59.         If colMasterHeaders Is Nothing Then ReDim colMasterHeaders(0) : colMasterHeaders(0) = New clsColumnReference()
    60.         If colSecondHeaders Is Nothing Then ReDim colSecondHeaders(0) : colSecondHeaders(0) = New clsColumnReference()
    61.         If colThirdHeaders Is Nothing Then ReDim colThirdHeaders(0) : colThirdHeaders(0) = New clsColumnReference()
    62.     End Sub
    63. #End Region
    64.  
    65.  
    66. #Region "sqlAddHeaders()"
    67.     Public Sub sqlAddHeaders(ByVal strData As String, ByVal strChildSAPCode As String, ByVal mySqlConnection As SqlConnection)
    68.         Dim i As Long, strHeaders() As String = Split(strData, "|"), lngParentIndex As Long
    69.         Dim lngHeaderIndex(2) As Long
    70.         Dim blnFoundText As Boolean = False : doInitHeaders()
    71.  
    72.         Dim blnAddHeader(2) As Boolean
    73.         Dim strHeaderTable(2) As String
    74.         strHeaderTable(0) = "tblMasterHeaders"
    75.         strHeaderTable(1) = "tblSecondaryHeaders"
    76.         strHeaderTable(2) = "tblThirdHeaders"
    77.         For i = 0 To 2
    78.             blnAddHeader(i) = False
    79.         Next
    80.  
    81.  
    82.         '''''''''''''''''''''''''''''
    83.         '' Split up column headers ''
    84.         '''''''''''''''''''''''''''''
    85.  
    86.         '' Master Headers
    87.         ''
    88.         For i = 0 To UBound(colMasterHeaders)
    89.             With colMasterHeaders(i)
    90.                 If strHeaders(0) = .strColumnText Then
    91.                     blnFoundText = True
    92.                     lngParentIndex = i
    93.                     Exit For
    94.                 End If
    95.             End With
    96.         Next
    97.         If Not blnFoundText Then
    98.             ReDim Preserve colMasterHeaders(UBound(colMasterHeaders) + 1)
    99.             colMasterHeaders(UBound(colMasterHeaders)) = New clsColumnReference()
    100.             With colMasterHeaders(UBound(colMasterHeaders))
    101.                 .blnIsMasterParent = True
    102.                 .lngParentIndex = -1
    103.                 .strColumnText = strHeaders(0)
    104.                 lngParentIndex = UBound(colMasterHeaders)
    105.                 blnAddHeader(constMasterHeader) = True                
    106.             End With
    107.         End If
    108.         blnFoundText = False
    109.         lngHeaderIndex(0) = lngParentIndex
    110.  
    111.  
    112.         '' Secondary Headers
    113.         ''
    114.         For i = 0 To UBound(colSecondHeaders)
    115.             With colSecondHeaders(i)
    116.                 If strHeaders(1) = .strColumnText Then
    117.                     blnFoundText = True
    118.                     lngParentIndex = i
    119.                     Exit For
    120.                 End If
    121.             End With
    122.         Next
    123.         If Not blnFoundText Then
    124.             ReDim Preserve colSecondHeaders(UBound(colSecondHeaders) + 1)
    125.             colSecondHeaders(UBound(colSecondHeaders)) = New clsColumnReference()
    126.             With colSecondHeaders(UBound(colSecondHeaders))
    127.                 .blnIsMasterParent = False
    128.                 .lngParentIndex = lngParentIndex
    129.                 .strColumnText = strHeaders(1)
    130.                 lngParentIndex = i
    131.                 blnAddHeader(constSecondaryHeader) = True
    132.             End With
    133.         End If
    134.         blnFoundText = False
    135.         lngHeaderIndex(1) = lngParentIndex
    136.  
    137.         '' Third Headers
    138.         ''
    139.         For i = 0 To UBound(colThirdHeaders)
    140.             If strHeaders(2) = colThirdHeaders(i).strColumnText Then
    141.                 colThirdHeaders(i).strChildSAPCodes += "," & strChildSAPCode
    142.                 blnFoundText = True
    143.                 Exit For
    144.             End If
    145.         Next
    146.         If Not blnFoundText Then
    147.             ReDim Preserve colThirdHeaders(UBound(colThirdHeaders) + 1)
    148.             colThirdHeaders(UBound(colThirdHeaders)) = New clsColumnReference()
    149.             With colThirdHeaders(UBound(colThirdHeaders))
    150.                 .blnIsMasterParent = False
    151.                 .lngParentIndex = lngParentIndex
    152.                 .strColumnText = strHeaders(2)
    153.                 .strChildSAPCodes = strChildSAPCode
    154.                 blnAddHeader(constThirdHeader) = True
    155.             End With
    156.         End If
    157.         lngHeaderIndex(2) = lngParentIndex
    158.  
    159.         '''''''''''''''''''''''''''''
    160.         '' Insert into SQL DB      ''
    161.         '''''''''''''''''''''''''''''
    162.         Dim strSQLStatement As String, mySQLCommand As SqlCommand
    163.  
    164.         If Not blnHaveCleared(1) Then
    165.             For i = 0 To 2
    166.                 strSQLStatement = "DELETE FROM " & strHeaderTable(i) & " WHERE lngParentIndex = lngParentIndex"
    167.                 mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    168.                 mySQLCommand.ExecuteNonQuery()
    169.             Next
    170.             mySQLCommand = Nothing
    171.             blnHaveCleared(1) = True
    172.         End If
    173.         If blnAddHeader(0) Then
    174.             strSQLStatement = "INSERT INTO " & strHeaderTable(0) & vbCrLf & _
    175.                                 "VALUES ( "
    176.             strSQLStatement += lngHeaderIndex(0) & "," & -1 & "," & "'" & colMasterHeaders(lngHeaderIndex(0)).strColumnText & "'"
    177.             strSQLStatement += ");"
    178.             mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    179.             mySQLCommand.ExecuteNonQuery()
    180.             mySQLCommand = Nothing
    181.         End If
    182.         If blnAddHeader(1) Then
    183.             strSQLStatement = "INSERT INTO " & strHeaderTable(1) & vbCrLf & _
    184.                                 "VALUES ( "
    185.             strSQLStatement += lngHeaderIndex(1) & "," & colSecondHeaders(lngHeaderIndex(1)).lngParentIndex & ",'" & colSecondHeaders(lngHeaderIndex(1)).strColumnText & "'"
    186.             strSQLStatement += ");"
    187.             mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    188.             mySQLCommand.ExecuteNonQuery()
    189.             mySQLCommand = Nothing
    190.         End If
    191.         If blnAddHeader(2) Then
    192.             strSQLStatement = "SELECT * FROM " & strHeaderTable(2) & " WHERE strColumnText = '" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "';"
    193.             mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    194.             Dim mySQLReader As SqlDataReader
    195.             mySQLReader = mySQLCommand.ExecuteReader()
    196.             If Not mySQLReader.Read Then
    197.                 mySQLReader.Close()
    198.                 strSQLStatement = "INSERT INTO " & strHeaderTable(2) & vbCrLf & _
    199.                                     "VALUES ( "
    200.                 strSQLStatement += colThirdHeaders(lngHeaderIndex(2)).lngParentIndex & ",'" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "','" & colThirdHeaders(lngHeaderIndex(2)).strChildSAPCodes & "'"
    201.                 strSQLStatement += ");"
    202.                 mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    203.                 mySQLCommand.ExecuteNonQuery()
    204.             Else
    205.                 mySQLReader.Close()
    206.                 strSQLStatement = "UPDATE " & strHeaderTable(2) & vbCrLf & _
    207.                                     "SET strChildSAPCodes = '" & colThirdHeaders(lngHeaderIndex(2)).strChildSAPCodes & "' "
    208.                 strSQLStatement += " WHERE strColumnText = '" & colThirdHeaders(lngHeaderIndex(2)).strColumnText & "';"
    209.                 mySQLCommand = New SqlCommand(strSQLStatement, mySqlConnection)
    210.                 mySQLCommand.ExecuteNonQuery()
    211.             End If
    212.             mySQLCommand = Nothing
    213.         End If
    214.     End Sub
    215. #End Region
    216. End Module
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    If my eyesight is working properly, you have listed code for the "Regions" section of a Module. As far as I have been taught, the code in "Regions" relates to instantiation of objects on forms and has no existence in a Module.

    How did it get there? Did you write it physically?
    Last edited by taxes; Mar 19th, 2004 at 05:03 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,


    By the way, if you are very knowledgible in VB6 etc, then you will find Inheritance, Classes and Database Access the hardest to assimilate. I suggest you check out these headings in MSDN Help and do searches on this forum. Also you will find the following sites helpful (although some articles MAY be too elementary for you!!)

    www.edneeis.com

    www.learnvisualstudio.net

    Also search this Forum for threads on migrating to VB.NET

    Best of luck.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Thanks for that. I also do Java development, so I know my OOP
    I'm also used to using ASP w/MySQL, so DB access isn't hard.
    But, getting used to ADO.Net took me a little while!

    And re; the regions, I typed them in manually so I could make all the code disappear nicely
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "Thanks for that. I also do Java development, so I know my OOP "

    Your'e fortunate. I came from dBase III Plus and VB6, so my brain went AWOL for a while (Still out there sometimes) as I found it difficult to get away from my Recordset mentality

    None of the code in Regions should be typed, even on a form. It should only be created by the Windows Forms Designer as you place the controls in the GUI. The exception to this is that you can amend the code e.g. when you want to replace "Friend" with "Protected" to allow derived amendments, but even that is frowned upon and the Modifiers property of the object should be used.

    Don't forget to mark the thread as "Resolved" when you have finished.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    regions are just so you can organise you code better. If you don't need to see a section of code eg coz you know it works, you can surround it by 'regions' and hide it.

    I'm not sure about using replace$...

    what's the difference between replace and replace$? you can also call the function from the actual variable name now
    eg

    myString.replace(.....)

    Don't know if one way is faster than the other?

    Hope this helps
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    I just noticed something else.

    Where did you put

    "Imports System.Data.SqlClient"

    In your posting it seems to be outside of the module.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yeah its outside the module declaration.... so what....

    Regarding replace$(), in VB6, string manipulation functions returned a variant instead of a string, unless one appended '$' to the function name
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    1) Imports must be placed above the class declaration & it's contained code - this is right.

    2) To clear up the #Regions part - regions are a way of organising your code - nothing more, using them in this way is fine.

    3) General notes at the moment which I can see from the top one Jamie (haven't properly looked through all of it)...

    (a) If you're dealing with single lists it's more efficient to use Arraylists (system.Collections.ArrayList) - this is only for single element lists but more effective than using the vb6 array.

    (b) The vb.net way of dealing with files is to use streams (like java does?) - shove a "Imports System.IO" at the top of the class & call on the File.OpenRead, File.OpenWrite methods etc.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  12. #12
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Just read through the rest of it. In VB.Net your meant to stay away from the public & constant variables more & have classes exposing these values as properties (like the properties you had in vb6 activex development)...

    Apart from that, looks generally ok - well for you anyways

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  13. #13

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by alex_read
    Apart from that, looks generally ok - well for you anyways
    Hang on just a minute. Remember when you were over last weekend? Well I hadn't started any VB.Net, ASP.Net or ADO.Net then - and here I am, end of the week, and I'm doing all this mad crap.

    So poo on you
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  14. #14
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Good point - so where'd you get it all from vbcode? planetsourcecode? hahah

    I'm only messing!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  15. #15
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    My 2 cents...

    1) Use stored procedures to speed up data processing.
    2) Store the ConnectionString in a single location, such as the App.Config file. You can use an application variable to hold the connectionstring.
    VB Code:
    1. Imports System.Configuration
    2.  
    3. Application("ConnectionString") = Configuration.AppSettings("ConnectionString")
    4.  
    5. 'Or just do this
    6.  
    7. Dim Conn As New SqlConnection(Configuration.AppSettings("ConnectionString"))
    3) Create functions to do the work for you.
    Instead of doing this
    VB Code:
    1. strData = Replace$(strData, "'", vbNullString)
    2. strData = Replace$(strData, vbTab, "','")
    3.  
    4. 'you can do this
    5.  
    6. strData = ReplaceStuff(strData)
    7.  
    8. Private Function ReplaceStuff(strValue As String)
    9.    strValue = Replace$(strValue, "'", vbNullString)
    10.    strValue = Replace$(strValue, vbTab, ''',''')
    11.  
    12.    return strValue
    13. End Function
    I am sure you are aware of the small OOP since you do Java, but these little things can save alot of time in the end.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width