8 Replies - 1340 Views - Last Post: 16 December 2011 - 04:47 PM Rate Topic: -----

#1 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Accumulating and concatenating string variables

Posted 16 December 2011 - 03:18 PM

I searched through the previous questions and didn't see this listed anywhere. What I need is a way to concatenate and accumulate what is in several text fields into a main textbox. They need to be separated by a comma as well. The problem I'm having is that if something isn't entered into a particular text field obviously that should not appear in the main textbox. But with the code I have, if I enter, for example, data into field 1 and field 3 and click the button to concatenate the data into one field, only the data from field 3 appears. Any ideas on what I may be doing wrong?

Thanks

Private Function PutTogether(ByVal Field1 As String, ByVal Field2 As String, ByVal Field3 As String) As String

        Dim strPutTogether As String
        Dim strField1 As String = txtField1.Text
        Dim strField2 As String = txtField2.Text
        Dim strField3 As String = txtField3.Text


        If txtField1.Text = "" Then
            strPutTogether = ""
        Else : strPutTogether = txtField1.Text
        End If

        If txtField2.Text = "" Then
            strPutTogether = ""
        Else : strPutTogether += txtField2.Text
        End If

        If txtField3.Text = "" Then
            strPutTogether = ""
        Else : strPutTogether += txtField3.Text
        End If
        
        Return strPutTogether

    End Function




Is This A Good Question/Topic? 0
  • +

Replies To: Accumulating and concatenating string variables

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 03:34 PM

If t1.Text = "" or t2.Text = "" or t3.Text = "" Then
'something doesn't have text in it
Else
Maintext.Text = t1.Text & "," & t2.Text & "," & t3.Text
End If


The way I understand your question, this is a better way to achieve your goal. It's rather self-explanatory.
Was This Post Helpful? 0
  • +
  • -

#3 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 03:42 PM

When I try it that way nothing happens if I happen to have data in only field1 and field3. It works if there is data in all of them but not if data is in some of them. Any other ideas?
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 03:46 PM

If t1.Text = "" then
Else
Output += t1.Text
End If

Repeat this check.
Was This Post Helpful? 0
  • +
  • -

#5 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 03:46 PM

To be precise, what you're doing wrong is wiping out your accumulated string (strPutTogether) any time an empty textbox is encountered. So the text that was stored from txtField1 is getting erased when it enters the txtField2 if condition. jimmyBo's got a good suggestion, or another way to go about it would be to replace your current if conditions with something along the lines of
If txtField1.Text <> "" Then strPutTogether &= txtField1.Text & ","

Was This Post Helpful? 0
  • +
  • -

#6 oldnewbie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 16-December 11

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 04:07 PM

Thank you...that worked. I did have the += in there to try to accumulate it. Not sure why that didn't work. Just started working with strings, as we've always done numbers. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 04:07 PM

Sound like a job, that's perfect for Aggregate
Dim TextEntries = {"A", "B", "", "D", "", "F"}
Dim a = TextEntries.Aggregate(Function(p, c) If(c = "", p, p & "," & c))


Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 04:44 PM

But an issue could be that at the end of your text you may have a leftover comma. After all the ifs, check the last character of the text to see if it is a comma and if so, remove it.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Accumulating and concatenating string variables

Posted 16 December 2011 - 04:47 PM

Nope does produce an extra comma
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1