Why does the " " below dlginfo.txtInfo.text always show a syntax error in Visual Basic? How can I fix it?
dlgInfo.Show()
dlgInfo.txtInfo.Text = " Nama : " txtnama.Text vbCrLf
" Tanggal Lahir : " timTanggalLahir.Text vbCrLf " Jenis Kelamin : " jenis_kelamin vbCrLf " Agama : " boAgama.Text vbCrLf " Pekerjaan : " lstPekerjaan.Text vbCrLf " Hobi : " hobi vbCrLf " Kendaraan : " lstKendaraan.Text vbCrLf " Motto Hidup : " txtMottoHidup.Text
CodePudding user response:
Of the three lines in the question (including dlgInfo.Show()
), the problem is between the end of the second line and the beginning of the third. Put them together and you get this:
vbCrLf " Tanggal Lahir : "
There's no operator between those two expressions. Since that would be invalid syntax, the compiler interprets the second line as a completed statement, and then begins processing the third line as a new statement.
So now let's look at just the first two terms of this new statement:
" Tanggal Lahir : " timTanggalLahir.Text
This is a valid expression, that concatenates a new string from two smaller strings. However, it's not valid as an opening to a statement. And so we see a syntax error at the
operator, because it's the operation that is invalid. We need an assignment, function call, or variable declaration here; not addition or concatenation that goes nowhere.
To fix it, we can add another concatenation operator to the end of the second line:
dlgInfo.txtInfo.Text = " Nama : " txtnama.Text vbCrLf
This will let VB.Net know to continue processing the next line as part of the same statement.
Or we can compact this to use string interpolation, so it (almost) fits on one line:
dlgInfo.txtInfo.Text = $" Nama : {txtnama.Text}{vbCrLf} Tanggal Lahir : {timTanggalLahir.Text}{vbCrLf} Jenis Kelamin : {jenis_kelamin}{vbCrLf} Agama : {boAgama.Text}{vbCrLf} Pekerjaan : {lstPekerjaan.Text}{vbCrLf} Hobi : {hobi}{vbCrLf} Kendaraan : {lstKendaraan.Text}{vbCrLf} Motto Hidup : {txtMottoHidup.Text}"
But that's still kind of long. What I would probably do is use an interpolated multi-line string literal to naturally separate the code on the line breaks, so the code more closely resembles the target string:
dlgInfo.txtInfo.Text =
$" Nama : {txtnama.Text}
Tanggal Lahir : {timTanggalLahir.Text}
Jenis Kelamin : {jenis_kelamin}
Agama : {boAgama.Text}
Pekerjaan : {lstPekerjaan.Text}
Hobi : {hobi}
Kendaraan : {lstKendaraan.Text}
Motto Hidup : {txtMottoHidup.Text}"
CodePudding user response:
The problem is that there is nothing to suggest that the third line is part of the second line shown in the question - there is no binary operator, such as &
, at the end of the second line.
The preferred string concatenation operator in VB.NET is &
, which does a bit more than the
operator between operands.
However, in this case, it would make the code easier to read and maintain if it used a StringBuilder to create the string, especially as that has an AppendLine method that means you don't need to keep appending VbCrLf, something like this:
Imports System.Text
`....
Dim sb As New StringBuilder
sb.AppendLine($" Nama : {txtnama.Text}")
sb.AppendLine($" Tanggal Lahir : {timTanggalLahir.Text}")
sb.AppendLine($" Jenis Kelamin : {jenis_kelamin}")
sb.AppendLine($" Agama : {boAgama.Text}")
sb.AppendLine($" Pekerjaan : {lstPekerjaan.Text}")
sb.AppendLine($" Hobi : {hobi}")
sb.AppendLine($" Kendaraan : {lstKendaraan.Text}")
sb.Append($" Motto Hidup : {txtMottoHidup.Text}")
dlgInfo.txtInfo.Text = sb.ToString()
You can see how much easier that would be to modify in the future; writing code which is easy to maintain is important. Let the compiler sort out any optimisations to the code, that is its job.
If you are using a version of Visual Studio before interpolated strings were introduced (VB14, which was in VS2015):
Dim sb As New StringBuilder
sb.AppendLine(" Nama : " & txtnama.Text)
sb.AppendLine(" Tanggal Lahir : " & timTanggalLahir.Text)
sb.AppendLine(" Jenis Kelamin : " & jenis_kelamin)
sb.AppendLine(" Agama : " & boAgama.Text)
sb.AppendLine(" Pekerjaan " & lstPekerjaan.Text)
sb.AppendLine(" Hobi : " & hobi)
sb.AppendLine(" Kendaraan " & lstKendaraan.Text)
sb.Append(" Motto Hidup : " & txtMottoHidup.Text)