VB.Net - when shall I use "New" word? - variables

In declaration of variables and objects, when exactly should I use "New" word, and when shouldn't I use it?
I know that I should declare a string without "New" word:
Dim mystring As String
I also know I should use it declaring a datatable:
Dim mytable As New Datatable()

New creates an object that is an instance of the specified class. If you just write the following then you have a reference, but the reference is Nothing as you didn't actually create a Datatable for it to refer to:
Dim mytable As Datatable
You don't typically use New for value types (Numbers, Dates, Booleans, Structures, Enums - a full list is here), as they always have a value (cannot be Nothing). For example this outputs 0:
Dim num as Int32
Console.WriteLine(num)
I wouldn't worry too much about this, but some value types (structures) can be initialised with New, which is somewhat inconsistent, for example:
Dim dec = New Decimal(2, 3, 4, True, 5)

Related

What is the difference between declare datatable class with () and without ()

I have a question about declaring a datatable variable.
Is there any difference if I declared a datatable variable as:
Dim xDt as new datatable
or
Dim xDt as new datatable()
??
Firstly, you're not declaring a class there. You are declaring a variable of type DataTable. The declaration is irrelevant anyway. What matters is that you are creating an instance of the DataTable class by invoking a constructor with the New keyword. A constructor is just a special method and, in VB, you can omit the parentheses when calling a method without arguments. This:
Dim table As New DataTable
is functionally equivalent to this:
Dim table As DataTable = New DataTable
which is functionally equivalent to this:
Dim table As DataTable
table = New DataTable
As you can see there, the last line is what matters and the variable declaration is irrelevant. That last line is equivalent to this:
table = New DataTable()
Whether you include the parentheses or not is up to you but I would suggest that you pick an option for a logical reason and stick to it consistently.
Personally, I always include parentheses on standard method calls but I always omit them from constructors. I do the former to make methods easily distinguishable from properties, so I would do this:
Dim str = obj.ToString()
rather than this:
Dim str = obj.ToString
With constructors though, there's little risk of mistaking them for properties but there is some risk of mistaking them for arrays, e.g.
Dim tables As DataTable()
Declares a variable of type DataTable array without creating an object while this:
Dim table As New DataTable()
declares a variable of type DataTable and assigns a new object to it. In my opinion, omitting the parentheses on constructors reduces the likelihood of confusion while including it on other methods does the same.

Assign datatable a value for TVP

I have had an issue I have after much stress, narrowed down to assigning a string to a datatable (cell?)
Dim dc As New DataColumn("Col1")
dc.DataType = System.Type.GetType("System.String")
ParameterTable.Columns.Add(dc)
For i = 0 To whereInValues.Count - 1
r = ParameterTable.NewRow
r.Item("Col1") = whereInValues(i).ToString
ParameterTable.Rows.Add(r)
Next
This is a small snippet of the code in question. It's a loop iterating through the whereInValues List(Of String) containing strings for the IN() values of an SQL Statement.
ParameterTable is my DataTable
This is assigned to the .value property of the parameter, which is set to Structured.
Basically, when I set to the value of the datatable using the .ToString method of the List, it shows fine in the debugger, but doesn't return any results from the database. When I set the value directly like:
r.Item(column) = "Barratt Homes"
It returns the correct results. I have tried wrapped a CStr() around the expression, but to no effect.
What is the difference between assigning the datatable value this way? Is .ToString to same as a System.String?
Sorry, I have found the answer to my question.
My code was automatically wrapping the strings of the List(Of String) with a ' character. When using parameters you obviously doesn't need that as it forms part of the string you are searching for. So when I was assign the string manually, I didn't have these characters there.
Stupid mistake...

A null reference could result in runtime

Dim policy_key() As RenewalClaim.PolicyKeyType
policy_key(0).policyEffectiveDt = date_format_string(ld_EffectiveDate)
Getting error at Line2.
An Error occured - Object reference not set to an instance of an object.
Each element of object arrays also needs to be declared as a new object too.
Dim policy_key() As RenewalClaim.PolicyKeyType
Redim policy_key(0)
policy_Key(0) = new RenewalClaim.PolicyKeyType
policy_key(0).policyEffectiveDt = date_format_string(ld_EffectiveDate)
QUICK TIP: When declaring classes structures etc, it is useful to name them so you can see what type they are....
e.g.
cls_Policy_Key for a class
str_Policy_Key for a structure etc.
When you come back to your code after a year.. you will thank yourself for doing so.
Dim policy_key() As RenewalClaim.PolicyKeyType
is part of your problem. When you are declaring policy_key() you are actually declaring it as an array with no elements. If you don't particularly need to use an array, for example, if you don't need to add objects to a particular element number, you might be better using a list and declaring it like this
Dim policy_key As New List(Of RenewalClaim.PolicyKeyType)
This way, you can add items easily without having to resize your array each time - The code is a little longer than Trevor's answer, but less prone to errors when you extend your code -
dim newPolicy_Key as RenewalClaim.PolicyKeyType
newPolicy_Key.policyEffectiveDt = date_format_string(ld_EffectiveDate)
policy_Key.add(newPolicyKey)

type var is not defined vb.net

I found an example in C# and from my understanding there is no alternative to 'var' in VB.NET. I am trying to create a datatable that will populate depending on a LINQ command further down in my code that calls this function. I have searched for a solution, but unable to find anything that works. Any assistance on what I should use would be appreciated. Note that I do have both Option Strict and Option Infer on as well.
Private Shared Function ToDataTable(rows As List(Of DataRow)) As DataTable
Dim table As New DataTable()
table.Columns.Add("Title")
table.Columns.Add("Console")
table.Columns.Add("Year")
table.Columns.Add("ESRB")
table.Columns.Add("Score")
table.Columns.Add("Publisher")
table.Columns.Add("Developer")
table.Columns.Add("Genre")
table.Columns.Add("Date")
For Each row As var In rows
table.Rows.Add(row.ItemArray)
Next
Return table
End Function
C# uses 'var' for implicit typing - VB uses Option Infer On combined with omitting the type.
The VB equivalent is:
Option Infer On
...
For Each row In rows
table.Rows.Add(row.ItemArray)
Next row
.NET already has .CopyToDataTable extension for that:
Dim table As DataTable = rows.CopyToDataTable
The VB equivalent is simply Dim, without any strong typing.
Dim sName = "John Henry"
In this example, the compiler infers type String (when Option Infer is set to On).
In your example, you may omit the As var portion. The compiler will infer type DataRow.
Tag your questions well, in this case there is no C# issue. Your problem is your are not writing an actual type on the foreach statement. This will fix it:
For Each row As DataRow In rows
table.Rows.Add(row.ItemArray)
Next

Why is new not used when a DataTable object is created?

I understand that everything in vb.net is a object. If that is the case, why is new keyword not used when creating a datatable object?
Dim dt as Datatable
dt.coloumns.add()
vs
Dim dt as Datatable = new Datatable
dt.coloumns.add()
Both seem to do the same things. However, in which scenario should I use new keyword? Are therere specific objects I don't need to use the new keyword? I understand that for common things like string, integer etc you don't need to instantiate the object. Is it the same case for DataTable too?
Dim dt as Datatable is merely declaration of the variable. It does not initialise it, so by default the value of dt is null (Nothing in VB I believe). Note that only declaring the value is not illegal at all, so you are perfectly within your rights to do so.
On the other hand, Dim dt as Datatable = new Datatable declares as well as initialises the variable. That is to say, new will initialise the declared variable with the appropriate value. If an class has a constructor which accepts parameters, then you can use new along with the constructor to create a new instance of that class and assign your chosen values to the class properties instead of using the default values.
The difference between your examples is that dt.Columns.add() will throw an error in the first example, since you are trying to call a method on a null object. In the second case, you have used new to provide an initial value to the variable. As a result, you can access the Columns property of a non-null object without any issue.
Now let's come to your other point - "I understand that for common things like string, integer etc you don't need to instantiate the object. Is it the same case for DataTable too?" Things like Integer are primitive datatypes, and so they have default non-null values. If you don't explicitly initialise with a value, they will take the default values. e.g. Dim x As Integer will automatically make x equal to 0
For objects, the default value is null, so it will cause problems if you try to do anything with that object without assigning a non-null value to it first. There are 2 ways to assign the non-null value:
Use new to initialise it.
Directly assign a value which is the result of some other processing in your code.