Question

I have this program, with a list of actions that you can make. These actions are stored in a local SQLCe Database (it will be in a real SQL Server once it works) and each actions can have multiple arguments. This code currently works but I'd like to know the best way to shrink it down with loops or jagged arrays, which I can't figure out.

Here's the code:

Imports System.Data.SqlServerCe
Imports System.Text
Imports Microsoft.Win32

Public Class Form2
Dim conn As SqlCeConnection = New SqlCeConnection("Data Source=Database.sdf")

    Public Function Split(ByVal source As String) As Array

    Dim results() As String

    results = source.Split(",")

    Return results
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim indexname As String = "Jagged Test"
    Dim scriptrestriction As String = ""
    Dim scriptid As String = ""
    Dim scripttype As String = ""

    Dim tempresult As String = ""

    Dim Arg0 As New List(Of String)()
    Dim Arg1 As New List(Of String)()
    Dim Arg2 As New List(Of String)()
    Dim Arg3 As New List(Of String)()
    Dim Arg4 As New List(Of String)()
    Dim Arg5 As New List(Of String)()
    Dim Arg6 As New List(Of String)()

    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If

    Dim sql As New SqlCeCommand("SELECT * FROM ScriptInfo WHERE Name = '" & indexname & "'", conn)
    Dim sdr As SqlCeDataReader = sql.ExecuteReader()

    While sdr.Read = True
        If Not IsDBNull(sdr.Item("Restriction")) Then
            scriptrestriction = sdr.Item("Restriction")
        End If
        If Not IsDBNull(sdr.Item("ID")) Then
            scriptid = sdr.Item("ID")
        End If
        If Not IsDBNull(sdr.Item("ScriptType")) Then
            scripttype = sdr.Item("ScriptType")
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg0")) Then
            tempresult = (sdr.Item("Arg0"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg0.Add(s1)
                Next
            Else
                Arg0.Add(tempresult)
            End If
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg1")) Then
            tempresult = (sdr.Item("Arg1"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg1.Add(s1)
                Next
            Else
                Arg1.Add(tempresult)
            End If
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg2")) Then
            tempresult = (sdr.Item("Arg2"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg2.Add(s1)
                Next
            Else
                Arg2.Add(tempresult)
            End If
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg3")) Then
            tempresult = (sdr.Item("Arg3"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg3.Add(s1)
                Next
            Else
                Arg3.Add(tempresult)
            End If
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg4")) Then
            tempresult = (sdr.Item("Arg4"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg4.Add(s1)
                Next
            Else
                Arg4.Add(tempresult)
            End If
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg5")) Then
            tempresult = (sdr.Item("Arg5"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg5.Add(s1)
                Next
            Else
                Arg5.Add(tempresult)
            End If
        End If

        tempresult = ""
        If Not IsDBNull(sdr.Item("Arg6")) Then
            tempresult = (sdr.Item("Arg6"))
            If tempresult.Contains(",") Then
                For Each s1 In (Split(tempresult))
                    Arg6.Add(s1)
                Next
            Else
                Arg6.Add(tempresult)
            End If
        End If

    End While

    If Not conn.State = ConnectionState.Closed Then
        conn.Close()
    End If

End Sub
End Class

Could you help me make this more optimized please?

Was it helpful?

Solution

One thing you can do is to not repeat the same code endlessly:

    tempresult = ""
    If Not IsDBNull(sdr.Item("Arg0")) Then
        tempresult = (sdr.Item("Arg0"))
        If tempresult.Contains(",") Then
            For Each s1 In (Split(tempresult))
                Arg0.Add(s1)
            Next
        Else
            Arg0.Add(tempresult)
        End If
    End If

Make this into a function like this:

Function SplitResults(ByVal Arg As String) As List(Of String)
    Dim Result As List(Of String) = New List(Of String)()
    If Not IsDbNull(Arg) Then
        If Arg.Contains(",") Then
            Result.AddRange(Split(Arg, ","))
        Else
            Result.Add(Arg)
        End If
    End If
    Return Result
End Function

You would call it like this:

Arg1 = SplitResults(sdr.Item("Arg1"))

You could also convert the individual List(Of String) variables to a List(Of List(Of String)), which would still result in a ragged list that would be easier to loop through or search using LINQ.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top