Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27336

Is there better ways to write this logic?

$
0
0
Hi

i cant help but wander, am i doing this right!!
i have a feeling im stuck in my old VB6 way of writing functions and find it time consuming coming up with logic all the time
a little background on what im doing.....

im loading data from files which is in a certain format.
to keep it simple theres a name (im treating like a column), and different values associated to that name

what ive done so far is create a class with 2 variables.....

MyClass()
string(for the name)
list of string(for all the variables)

and this class is also being put into a list for each file i read
so i have...
MyClassList as list(of MyClass)


what im doing now is merging them together sorting duplicates

if 2 of the same name strings are found then i have to merge the variables from both under 1 name(class instance)


Classitem1 Classitem2
Name1 - var1,var2,var3,var4 Name1 - var1,var5,var6

after sort will be

Classitem3
Name1 - var1, var2,var3,var4,var1,var5,var6

and after this i will remove duplicates from classitem3 which should be easy

i have already sorted everything but is there better ways in .net to achieve the following...

Code:

'this basically uses the list of classes removes duplicate names, joins there variables together in there list and returns the fixed list (of class1)
Public Function RemoveDuplicatesInNames(tmpList As List(Of Class1)) As List(Of Class1)
        Dim tmpstrings As New List(Of String) ' this is helping with the problem accessing variable inside the class
        RemoveDuplicatesInNames = New List(Of Class1)

        For Each item As Class1 In tmpList  'items in the list passed to the function
            If Not tmpstrings.Contains(item.MacroString) Then
                tmpstrings.Add(item.MacroString)
                RemoveDuplicatesInNames .Add(item)
            Else
                For Each item2 As Class1 In RemoveDuplicatesInNames
                    If item2.MacroString = item.MacroString Then
                        item2.ControlNameList.AddRange(item.ControlNameList)
                        Exit For
                    End If
                Next
            End If
        Next
        Return RemoveDuplicatesInNames
    End Function

can i not use list.fornext and lambda expressions or something to make this smaller, or some part of .net im not aware of



another one

this sorts the string(delimited) from the files into seperate strings, is this ok in .net standards

Code:

Private Function ParseVars(VarStringToParse As String) As List(Of String)
        ParseVars= New List(Of String)
        Dim StringPos As Long = 0

        Do While StringPos <= VarStringToParse.Length

            Select Case VarStringToParse.Chars(StringPos)
                Case "#"
                    VarStringToParse= VarStringToParse.Remove(StringPos, 1)
                Case "|"
                    ParseVars.Add(VarStringToParse.Substring(0, StringPos))
                    VarStringToParse= VarStringToParse.Remove(0, StringPos + 1)
                    StringPos = 0
                Case Else
                    StringPos += 1
            End Select

            If VarStringToParse.Contains("#") = False Then
                If VarStringToParse.Contains("|") = False Then
                    ParseVars.Add(VarStringToParse)
                    StringPos = VarStringToParse.Length + 1
                End If
            End If

        Loop
        Return ParseVars
    End Function

typical format of the vars string in the file is "#var|var|var|var"


any suggestions :)

Viewing all articles
Browse latest Browse all 27336

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>