How can I set values for variables that are declared on one line? - vb.net

I have a list of variables that are being created on one line.
Dim strFirstname, strMiddleName, strLastName As String
Is it possible to set the values for all of them at once? I know this doesn't work, but this is what I'm trying to do :
Dim strFirstname, strMiddleName, strLastName As String = ""

I liked the examples, but I just needed to set the values so that the compiler would leave me alone. I have another function that sets all the values that I'm passing these strings into. I wanted to set them to pretty much nothing, just so it would leave me alone. I ended up using this:
Dim strFirstname, strMiddleName, strLastName As New String(String.Empty)

If you want different values, this should work:
Dim strFirstname As String = "First name", strMiddleName As String = "middle name", strLastName As String = "last name"

If you truely want one line instantiating them all, create an object to hold this data instead, e.g.
...
person.Firstname
person.MiddleName
person.LastName
...
Then populate them in the constructor, e.g.
Dim person As New Person("first","middle","last")
Seems like a cleaner option if your objects are all related to a specific idea/entity.

Related

vb.net Interpolated Strings

I was chastised by a professional developer with a lot of years of experience for Hard Coding my DB name
OK I get it we sometimes carry our bad codding habits with us till we learn the correct way to code
I have finally learned to use Interpolated Strings (personal view they are not pretty)
My Question involves the two Sub's posted below GetDB runs first then HowMany is called from GetDB
Sorry for stating the obvious my reason is I think that NewWord.db gets declared in GetDB and works in HowMany without the same construction Just a Wild Guess
Notice NO $ or quotation used in HowMany
Both Sub's produce desired results
The question is Why don't both statements need to be constructed the same?
Public Sub HowMany()
'Dim dbName As String = "NewWord.db"
Dim conn As New SQLiteConnection("Data Source ='{NewWord.db}';Version=3;")
tot = dgvOne.RowCount ' - 1
tbMessage.Text = "DGV has " & tot.ToString & " Rows"
End Sub
Private Sub GetDB()
Dim str2 As String
Dim s1 As Integer
'Dim dbName As String = "NewWord.db"
Using conn As New SQLiteConnection($"Data Source = '{"NewWord.db"}' ;Version=3;")
conn.Open()
That second method is a ridiculous and pointless use of string interpolation. What could possibly be the point of inserting a literal String into a literal String? The whole point is that you can insert values determined at run time. That second code is equivalent to using:
"Data Source = '" & "NewWord.db" & "' ;Version=3;"
What's the point of that? The idea is that you retrieve your database name from somewhere at run time, e.g. your config file, and then insert that into the template String, e.g.
Dim dbName = GetDbNameFromExternalFile()
Using conn As New SQLiteConnection($"Data Source = '{dbName}' ;Version=3;")
Now the user can edit that external file to change the database name after deploying the application. How could they change the name in your code?
To be clear, string interpolation is just native language support for the String.Format method. You can see that if you make a mistake that generates an exception and the that exception will refer to the String.Format method. In turn, String.Format is a way to make code that multiple values into a long template easier to read than if multiple concatenation operators were used.
Having lots of quotes and ampersands makes code hard to read and error-prone. I've lost count of the number of times people miss a single quote or a space or the like in a String because they couldn't read there messy code. Personally, I'll rarely use two concatenation operators in the same expression and never three. I'll do this:
Dim str = "some text" & someVar
but I'll rarely do this:
Dim str = "some text" & someVar & "some more text"
and I'll never do this:
Dim str = "some text" & someVar & "some more text" & someOtherVar
Before string interpolation, I would use String.Format:
Dim str = String.Format("some text{0}some more text{1}", someVar, someOtherVar)
Nowadays, I'll generally use string interpolation:
Dim str = $"some text{someVar}some more text{someOtherVar}"
Where I may still use String.Format over string interpolation is if one value is getting inserted in multiple places and/or where the text template and/or the expressions are long so that I can break the whole thing over multiple lines, e.g.
Dim str = String.Format("some text{0}some more text{1}yet more text{0}",
someVar,
someOtherVar)
I have no idea what NewWord.db is so I made a class to represent it.
Public Class NewWord
Public Shared Property db As String = "The db Name"
End Class
HowMany is not a very good name for your sub. Try to use more descriptive names.
The first sub doesn't even use the connection. The connection string in that code is a literal string. It will not consider NewWord.db as a variable. You will not notice this because you never attempt to open the connection. In my version you check the connection string with a Debug.Print.
I changed the last line to use and interpolated string. It is not necessary to call .ToString on tot.
Private Sub DisplayGridCount()
Dim conn As New SQLiteConnection("Data Source ='{NewWord.db}';Version=3;")
Debug.Print(conn.ConnectionString)
Dim tot = DataGridView1.RowCount
TextBox1.Text = $"DGV has {tot} Rows"
End Sub
The second snippet starts off with 2 unused variables. I deleted them. Again, the Debug.Print to show the difference in the 2 strings.
Private Sub TestConnection()
Using conn As New SQLiteConnection($"Data Source = '{NewWord.db}' ;Version=3;")
Debug.Print(conn.ConnectionString)
'conn.Open()
End Using
End Sub
As to where to store connection strings see https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/protecting-connection-information and Where to store Connection String

Casting objects to strings

I have recently set OPTION STRICT ON for a solution. There is code like this:
dim strTest as string = objDR("FirstName") & " Smith"
objDR is a datareader. I realise the quality of this code is not particularly good e.g. concatenating strings without stringbuilder and naming veriables objDR (I did not write this code).
I now have to do this (now that option strict is on):
dim strTest as string = cstr(objDR("FirstName")) & " Smith"
However, an exception is thrown if objDR("FirstName") is null. When OPTION STRICT was set to OFF, objDR("FirstName") would be implicitly casted to an empty string.
I can resolve this problem like this:
dim strTest as string = "Smith"
If not dbnull(cstr(objDR("FirstName"))) then
strTest=strTest & " Smith"
end if
Is there a better way to approach this? I came accross TRYCAST (http://msdn.microsoft.com/en-gb/library/zyy863x8.aspx), but it always seems to CAST objDR("FirstName") to a blank string. I believe this is because it only works with reference types?
Given you know it's a string value then you should use the GetString method
Dim strTest As String = objDR.GetString(objDR.GetOrdinal("FirstName")) & " Smith"
This would eliminate any need for casting.
If the 'null' is a DBNull then try this
Dim strTest As String = Convert.ToString(objDR("FirstName")) & " Smith"

Insert variable into middle of url

how could I insert a variable into the middle of a URL using vb.net?
e.g.
Dim ver As String = "variable"
http://testurl.co.uk/folder/next_folder/" & ver & "/test.exe
(not sure if this is correct above)
Note: I know how to set a variable but not sure how to insert into the middle of a URL
You could do that (as #SysDragon has pointed out, you need to store the result in a variable). Another alternative would be:
Dim ver As String = "variable"
Dim url As String = String.format("http://testurl.co.uk/folder/next_folder/{1}/test.exe", ver)
It probably doesn't matter for something as trivial as this, but strings are immutable, so doing:
Dim myString as String = "a" & "b" & "c"
effectively destroys and recreates the string twice. StringBuilder (which I believe string.format uses internally) prevents this.
Almost, I think you want this:
Dim ver As String = "variable"
Dim url As String = "http://testurl.co.uk/folder/next_folder/" & ver & "/test.exe"
To browse the url in the form do something like this:
Dim wb1 As New Net.WebBrowser()
Form1.Controls.Add(wb1)
wb1.Navigate(url)

get a specific variable's field value to display in a textbox

I have a list of declared string variables that are name codes who's field values are full names. Can I get a specific variable's field value to display in a textbox if I know what the variable name is?
Example variables
Dim GLAN01 As String = "Langer Georg"
Dim BEDW01 As String = "Edwards Brian"
Dim MRIG01 As String = "Riggins Michael"
Not sure I understand the question correctly. How about this pattern? :
textBoxName.Text = variableNameToDisplay
for example :
textBox1.Text = GLAN01
to display Langer Georg text in textBox1
UPDATE :
If I understand what you are after correctly, one possible way is using dictionary to store mapping of code name to full name (instead of using multiple variables). With dictionary you can get full name string given any code name string easily, for example :
Dim dict As New Dictionary(Of String, String) From _
{{"GLAN01", "Langer Georg"},
{"BEDW01", "Edwards Brian"},
{"MRIG01", "Riggins Michael"}}
textBox1.Text = dict("GLAN01")
For reference, in case you are not familiar yet with collection initializer syntax as in above example :
MSDN : Collection Initializers (particularly "Nesting Collection Initializer" section)

How can I get the URL and Querystring? vb.net

I am refactoring some legacy code. The app was not using querystrings. The previous developer was hard coding some variables that the app uses in other places.
Like this using VB.NET
so.Cpage = "ContractChange.aspx"
My question is can I programatically set this value and include the current querystring?
I want so.Cpage to be something like ContractChange.aspx?d=1&b=2
Can I do this with the request object or something? Note, I don't need the domain.
To get the current query string you would simply do something like the following:
Dim query as String = Request.QueryString("d")
This will assign the value of the "d" querystring to the string variable "query". Note that all query string values are strings, so if you're passing numbers around, you'll need to "cast" or convert those string values to numerics (be careful of exceptions when casting, though). For example:
Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)
The QueryString property of the Request object is a collection of name/value key pairs. Specifically, it's of type System.Collections.Specialized.NameValueCollection, and you can iterate through each of the name/value pairs as so:
Dim coll As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim value As String
For Each key As String In coll.AllKeys
value = coll(key)
Next
Using either of these mechanisms (or something very similar) should enable you to construct a string variable which contains the full url (page and querystrings) that you wish to navigate to.
Try this:
so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
In VB.Net you can do it with the following.
Dim id As String = Request.Params("RequestId")
If you want to process this in as an integer, you can do the following:
Dim id As Integer
If Integer.TryParse(Request.Params("RequestId"), id) Then
DoProcessingStuff()
End If
try this
Dim name As String = System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
Dim qrystring As String = Request.ServerVariables("QUERY_STRING")
Dim fullname As String = name & "/" & qrystring
Not sure about the syntax in VB.NET but in C# you would just need to do
StringId = Request.QueryString.Get("d");
Hope this helps.