Reputation: 48568
I come from a C# background and have to now code in VB.Net (new job)
I am writing a code in VB.Net which works fine in C# (after syntax changes) but in VB.Net it gives error of Array bounds cannot appear in type specifiers
.
C# Code
TimeSpan yesterday = new TimeSpan(1, 19, 0);
DateTime today = new DateTime(2012, 9, 4, 8, 48, 0);
DateTime ts = today.Add(new TimeSpan(9, 0, 0)).Subtract(yesterday);
VB.Net Code
Dim yesterday As New TimeSpan(1, 19, 0)
Dim today As New DateTime(2012, 9, 4, 8, 48, 0)
Dim ts As today.Add(New TimeSpan(9, 0, 0)).Subtract(yesterday)
It gives this error under New
of 3rd line of VB code. Where am I wrong?
Upvotes: 6
Views: 207
Reputation: 263723
you must explicitly declare the data type especially for "known" data types. Remember that Visual Basic is CASE INSENSITIVE
Dim ts As datetime = today.Add(New TimeSpan(9, 0, 0)).Subtract(yesterday)
but you can omit the datatype of the variable if have set
Option Infer ON
by default, it's ON
Upvotes: 1
Reputation: 101052
Dim ts As today.Add(New TimeSpan(9, 0, 0)).Subtract(yesterday)
should be
Dim ts = today.Add(New TimeSpan(9, 0, 0)).Subtract(yesterday)
or
Dim ts As DateTime = today.Add(New TimeSpan(9, 0, 0)).Subtract(yesterday)
When declaring a variable, you use As
as type specifier.
Dim x As Int32
x = 10
or
Dim x As Int32 = 10
When assigning a value to the variable on the same line, you can omit the type specifier.
Dim x = 10
Because of this, I generally don't mix up As
and New
like this
Dim x As New FooBar()
as I think it is somewhat confusing. I prefer
Dim x = New Foobar()
Upvotes: 10