Revisiter - VB.NET strings

How many times you wish you can construct strings not using the " (quote) and + (plus).

In our day to day programming, we usually do stuff like:

Sub Query(table_name As String, idx As Integer)
  Dim sql As String = "SELECT * FROM " + table_name + "WHERE PKID=" + idx.ToString()
EndSub

Or maybe you can use "string.format" or StringBuilder

Dim sql As String = String.Format("SELECT * FROM {0} WHERE PKID={1}", table_name, idx)

Or

Dim sql As New StringBuilder
sql.Append("SELECT * FROM ").Append(table_name).Append(" WHERE PKID=").Append(idx.ToString())

As you can see, you can't get away from using the " and + sign. angry.jpeg

Fortunately, there is another way to write above code and you can only get in VB.NET, sorry C# guys...

Dim sql As String = <span>SELECT * FROM <%= table_name %> WHERE PKID=<%= idx.ToString() %>; <span>.Value

So elegant...

Try it out. Happy coding...

Introduction

Since my employer make it mandatory to use VB.NET as the development language for all of our projects, I shall be writing articles mostly related to VB.NET for my readers.

Having said that, I'll also occasionally write about other stuff related or not related to programming too. I've other interest like embedded device programming, operating systems kernel (windows and linux), design patterns, bayesian networks, machine learning and also other programming language like Go, Erlang and Clojure to name a few.

Thanks.

Please stay tune....

GitHub – stanleytktan

Stanley Tan

Malaysia

I'm a very "senior" software developer. tbc...