1

I have a form that has 3 Tabs and a user will populate dropdowns on the form and then press save.

I then write the string to a text file with a separator between each answer, is there a better way of doing it other than what I have done below?

Dim tesstring As String = " test " & _
         Separator & _
         answer1 & _
         Separator & _
         answer2 & _
         Separator & _
         answer 3 & _
         Separator & _
         answer4 & _
         Separator & _
         answer5 & _
         Separator & _
         Combo_BS.SelectedItem.ToString.Substring(0, 6) & _
         Separator & _
         answer6 & _
         Separator & _
         answer7 & _
         Separator & _
         answer8 & _
         Separator & _
         answer9 & _
         Separator & _
         answer10 & _
         Separator & _
         answer11 & _
         Separator & _
         answer12 & _
         Separator & _
         answer13 & _
         Separator & _
         answer14 & _
         Separator & _
         answer15 & _
         Separator & _
         answer16 & _
         Separator & _
         answer17 & _
         Separator & _
         answer18 & _
         vbCrLf
user1295053
  • 303
  • 1
  • 7
  • 17

2 Answers2

3

is there a better way of doing it other than what I have done below?

Yes. Create an array of all the values, and then use String.Join:

Dim tesstring As String = String.Join(Separator, values) & vbCrlf

Ideally, don't have 18 different answer variables to start with - can't those be in a collection?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The compiler does this for you: http://stackoverflow.com/questions/290527/does-vb-net-optimize-the-concatenation-of-string-literals – Polity Dec 07 '12 at 08:31
  • @Polity: Not quite - the compiler uses `String.Concat`. Note that the values here aren't constants, so the compiler can't perform the full concatenation at compile-time. But the point is that the code using `String.Join` would be *simpler* here than using string concatenation, because you only need to specify the separator once. – Jon Skeet Dec 07 '12 at 08:31
  • Hi Jon, I'm quite new to VB.Net, could you give a bit more detail re creating an array and also what you mean re collection? – user1295053 Dec 07 '12 at 08:38
  • `dim arr as String() = { answer1, answer2, answer3 ... }`. Collection -> usually an array or a list. – Teejay Dec 07 '12 at 08:59
  • 1
    @user1295053: As Teejay says... but really learning that sort of thing is something you should get from a good book. Stack Overflow is good for answering specific questions, but it's not good for learning a language overall. – Jon Skeet Dec 07 '12 at 09:03
0

To expand on Jon's answer, create a collection of answers. For example, if all the answers are in textboxes in a panel called 'Panel1':

    Dim answers = From x In Me.Panel1.Controls.OfType(Of TextBox)() Select x.Text
    Dim tesstring As String = String.Join(Separator, answers)
Steve
  • 20,703
  • 5
  • 41
  • 67
  • dunno if it would compile, probably you need a `answers.ToArray()` – Teejay Dec 07 '12 at 10:15
  • compiles fine, answers is IEnumerable(Of String) – Steve Dec 07 '12 at 11:45
  • `String.Join` takes `String[]`, not `IEnumerable` – Teejay Dec 07 '12 at 11:46
  • Just tried. It compiles but gives runtime error: `Unable to cast object of type 'WhereSelectArrayIterator\`2[System.String,System.String]' to type 'System.String[]'`. With `.ToArray()` works fine! – Teejay Dec 07 '12 at 11:49
  • odd, string.join has an overload that accepts IEnumerable(Of String): http://msdn.microsoft.com/en-us/library/system.string.join.aspx i just wrote a test app in vs2010 .net4 option strict that compiles and runs. – Steve Dec 07 '12 at 11:54
  • My fault, I use .net 3.5 by default! In .net 4 it has more overloads. – Teejay Dec 07 '12 at 12:07