Question SetDelimiters as variable

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I am attempting to set the delimeters for reading a csv file to a variable (in case it is not seperated by a comma) so that in

VB.NET:
Reader.SetDelimiters(","c)

"," can be set as a variable - I have tried a number of ways but the character literal is the problem.

Thanks
 
If parameter type is Char you can use a Char variable.
 
You mean

Dim vChar as Char = textbox.text
Reader.SetDelimiters(vChar)

As easy as that?

I was setting it as a string! Dah!

Thanks
 
TextBox.Text property is type String, you probably don't have Option Strict enabled so an implicit conversion occurs with that code and it takes the first Char in that String. A String is like a Char array.
If I have to make a qualified guess, you're attempting to use the TextFieldParser.SetDelimiters method, in which case the parameter type is a ParamArray of String, and that would make your Char ordeals pointless since passing a Char value to that would only convert it back to String.
 
Hi John

This is what I am doing...

VB.NET:
If CB.TSCheckBox.Checked = True Then
                Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(vFile)
                    Reader.TextFieldType = FileIO.FieldType.Delimited
                    Reader.SetDelimiters(","c)
                    Dim CurrentRow As String()
                    While Not Reader.EndOfData
                        Try
                            CurrentRow = Reader.ReadFields
                            DT.Rows.Add(CurrentRow)
                        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                            TaskDialog.Show(MainPopup, AppBoxWarning("Warning", ex.Message & " is not valid and will be skipped", "Import Warning"))
                        End Try
                    End While
                End Using

            Else
                For Each line In TextLines
                    Dim objFields = From field In line.Split(","c)
                                    Select CType(field, Object)
                    Dim NewRow = DT.Rows.Add()
                    NewRow.ItemArray = objFields.ToArray()
                Next

            End If
 
Yeah, as I said, parameter type for that is a ParamArray of String, so you pass it one or more String values.
 
So if I read you correctly what I should be doing is.

Dim vString() as String = {textbox.text}

Is that what you mean?
 
ParamArray means you can pass each item by itself without using an array object, so if you only have one string argument you can pass that. Parameter Arrays (Visual Basic)
 
Now I'm really confused! As far as I can see you can only use ParamArray in a Function or Sub.

I only need to pass one parameter, being the delimeter. Can you show me an example of what you mean?
 
So it's

Dim vChar as Char = textbox.text
Reader.SetDelimiters(vChar & c) ???

I know that comma delimited should be a comma in a csv file, but in theory it could be set to "@" or any number of other variables

Thanks


====Edit=====

I have just tried it via the third post.


Dim vChar as Char = textbox.text
Reader.SetDelimiters(vChar)

with a number of csv files and it seems to be working fine. The character literal threw me out...
 
Last edited:
What?

So it isn't
a string variable that is intended to be part of a single delimiter definition?

You do realise that you can't have a wildcard here, right?

So again, if you want a single delimiter that is not comma ","

Reader.SetDelimiters(StringVariable)
Reader.SetDelimiters(" ")
Reader.SetDelimiters(" !")
Reader.SetDelimiters(" " & StringVariable)
Reader.SetDelimiters(TextBox1.Text)
Reader.SetDelimiters(vbTab)

If you want a list of alternative delimiters ...

Reader.SetDelimiters({" ", vbTab, TextBox1.Text})
Reader.SetDelimiters(StringArray)
 
You do realise that you can't have a wildcard here, right?

First we look at the csv file and determine what the field delimiters are (usually a comma) - so "," (without quotes) would be entered into the text box. If the field delimiter is something else that would be entered into the textbox. For Tab we could use a checkbox and then set as vbTab

The purpose is to import data, exported into csv from some other accounting system, into ours. With some systems you can specify the delimiters at export time, others are chiselled into stone so we just want to make this utility as flexible as possible. The biggest hurdles were carriage returns and commas in the data but the TextFieldParser proved very useful. The interface is a little like opening a csv file into Excel, except we are opening initially into a DataGridView.
 
Alright, so

Dim DL As String

If Textbox1.Text <> "" Then
DL = Textbox1.Text
Else
' DL = vbTab or whatever
End If

Reader.SetDelimiters(DL)
 
Thanks to both of you.

It was the character literal thown into the mix that was causing the pain - lack of sleep probably didn't help... You can bat off thousands of lines of code without an issue, then get stumped on the smallest thing.
 
Back
Top