1

I have this code which gives an error:

'declaration
Dim strFieldValues As String
'split
strFieldValues = strRecord.Split(",") 'field are separated by commas

2 Answers 2

3

Well, the error seems to be pretty self-explanatory to me. You've declared a variable of type String - i.e. it can hold a value of a single String reference:

Dim strFieldValues As String

You've then tried to assign a value to it returned from String.Split():

strFieldValues = strRecord.Split(",")

Now String.Split() returns a String array, not a single string value.

So you have two courses of action open to you:

  • Change strFieldValues to an array variable
  • Change the value you assign to it

My guess is that you want the first, but we don't know what you're trying to achieve. The simplest approach would be to combine declaration and initialization:

Dim strFieldValues = strRecord.Split(",")

You may also need to change the arguments to Split - I don't know how VB will sort out that call.

Sign up to request clarification or add additional context in comments.

Comments

0

If all you want to do is just retrieve either side of the resulting string array, you could just invoke the left or right part like this:

strFieldValues = strRecord.Split(",")(0) ' Text to the left of the delimiter character

Or

strFieldValues = strRecord.Split(",")(1) ' Text to the right of the delimiter character

Of course, this assumes that the delimiter character does exist, so you should take the necessary precautions to ensure that you won't run into a runtime exception if said character is not found on the string you are splitting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.