Tip Concatenation

jjdickow

Member
Joined
Aug 19, 2010
Messages
8
Programming Experience
Beginner
Concatenation is pretty much the addition of strings. This is frequently used in many programs and also a very simple concept.

Here is an example:
1. Open a new project
2. Add one button and two textboxes
3. Double click your button and add this code:
VB.NET:
 Dim strOne As String
        Dim strTwo As String
        strOne = TextBox1.Text
        strTwo = TextBox2.Text

        MessageBox.Show(strOne & " " & strTwo)

Explanation:
-If you didn't know dim is the same thing as "define" it defines your variables. We defined two different strings here (strOne and strTwo).
-We then set each string equal to a textbox. It is set to textbox1/2.TEXT because you are setting it equal to what was entered into the textbox.
-After defining variables we use messagebox.show (pretty much a pop up which you can add extra functions but I will not use those) and we are making it show strOne, a space, and strTwo.
-Notice we use the ampersand (&). This is what you use to connect any variable with anything in quotes. We connect what ever strOne is equal to, a space (between quotes is the space), and what ever strTwo is equal to.
-Run your application (F5) and enter "Hello" in the first textbox and "Bob" in the second. click your button and a messagebox should pop up saying "Hello Bob"


Mess around with random uses like this until you get the hang of it. Then keep moving forward in your Visual Basic Career :p
 
Dim strOne As String
Dim strTwo As String
strOne = TextBox1.Text
strTwo = TextBox2.Text
You can assign these variable values when declaring them:
VB.NET:
Dim strOne As String = TextBox1.Text
Dim strTwo As String = TextBox2.Text
Also in one line is possible:
VB.NET:
Dim strOne As String = TextBox1.Text, strTwo As String = TextBox2.Text
You don't have to use variables, the Text properties can be used directly. If a variable doesn't add value in readability/usability don't use one.
VB.NET:
MessageBox.Show(TextBox1.Text & " " & TextBox1.Text)
In some cases there could be more values and all those & " & " gets messy, so String.Format can be used with a formatting string with placeholders:
VB.NET:
Dim newstring = String.Format("{0}, {1}, {2} and {3}", strA, strB, strC, strD)
Then you have the String.Concat method, one of the overloads is a ParamArray which allow you to put as many arguments you like:
VB.NET:
Dim newstring = String.Concat("here, ", "there, ", "and everywhere")
String class also have another method I often use when producing strings from arrays/lists when I need any kind of separator between the values, the Join method:
VB.NET:
Dim newstring = String.Join(" ", theStrings)
I would also like to mention the StringBuilder class, essential when building up lots of strings. Since adding any two string values together will create a new string instance this can get VERY slow. The StringBuilder does not have the immutable limitation that String class have, so when adding lots of strings in memory use this.
 
you know this is for beginners right? lol
Would a beginner have trouble understanding anything in these brief examples? I don't think so. However, if you are writing a beginner tutorial about adding strings, only mentioning the & operator is inadequate. If the purpose was to write a tutorial about the & operator you should not give the impression you're actually introducing concatenation, because you did not. You didn't even explain that & is called an operator, for which there also exists others, in direct relation to adding strings why didn't you also add a line about the &= operator? Calling things by their appropriate name is something that is important to not cause confusion, even more so when teaching people that has not learnt it yet, and this also enables them so find more resources when they know what it is called.

About variable declaration and value assignment, you may not realize, but what you posted there is considered bad coding practice (not a good start for a beginner, eh?). VB does allow it, and in early versions of the language this was required because VB simply wasn't good enough. There are languages where you must assign a value when declaring a variable, even if that means assigning an explicit Nothing, but very often one have a value available exactly when the need for a variable placeholder arises. So why write two lines of code when you can, and should, only write one? As programming gets more complex scattering detached code fragments like that all over the place will lead to more abstraction and less readability. Declaring the variable in the code block it is used is also something that helps the automatic memory management in VB to optimize usage and reclaim garbage.
 
Another lesser-known fact about concatenation is that using the & operator will implicitly convert a number or a numeric expression into a string for output. For example:

"2 + 3 = " & 2 + 3

will display:
2 + 3 = 5
 
Back
Top