Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am working in Visual Basic to create code that includes a special message for parts depending on if an excel file indicates they need it. The excel file consists of 3 columns: characters a part may start with, contain, or end with. The code needs to detect if the part starts with a character combination included in the "starts with" column, contains one from the contains column, or ends with one from the ends with column, and if it does then the corresponding message to those specifications will be displayed elsewhere in the code.

To get the data out of the excel (csv) file I use a streamreader and put each column into an array as follows:

Dim SData() As String
        Dim SWith, Contain, EWith, Message As New List(Of String)()

        Using sr As New StreamReader("L:\Mfg-Eng\SystemID\DOWNLOADS\SCSpecialNotes.csv")
            While Not sr.EndOfStream
                SData = sr.ReadLine().Split(","c)

                SWith.Add(SData(0).Trim())
                Contain.Add(SData(1).Trim())
                EWith.Add(SData(2).Trim())
                Message.Add(SData(3).Trim())
            End While
        End Using


I am new to visual basic, and I am having trouble coming up with an "If" statement to follow the creation of these arrays in which the statement executes code when the string in question (not written in above code, I'll refer to it as "partID") starts with any member of the "SWith" array, or ends with any member of the "EWith" array.

What I have tried:

I have tried creating a separate string to utilize the "Contains" method for searching the array in which the If statement executs when the partID starts with a string that the SWith array contains, but this doesn't work when I haven't given the placeholder string a value, and feels like a cumbersome and confusing way to tackle this. I have searched extensively online for a way to write this code but I have been unable to find one, and I am very new to using Visual Basic and its commands. Any help is greatly appreciated.
Posted
Updated 6-Aug-19 5:43am

1 solution

You haven't said if the "3 arrays" have to be searched in parallel, or not.

Depending which, you either:

1) tokenize the string and use the first, "middle" and last tokens to search the arrays.

2) Or, test the string with "StartsWith" (table entry), "EndsWith" and "Contains" if using the table(s) as the driver.
 
Share this answer
 
Comments
andrew898701 6-Aug-19 16:11pm    
Thank you for the response, I ended up using a for loop that tests if each row has cells that qualify the part for its message, they are not searched in parallel.

Thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900