473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

VB 2008: Option Strict On + Infer On at class level

Hi,

after dealing with the new possiblities of the current VB version, I again
and still wonder why Option Infer can not be used with Option Strict On
at class level, i.e. when declaring a field:

Dim x = 17

Now I found out that the same line does work within a procedure. Actually I
wanted to ask why, but now I see that it is probably because the line is
realy split into declaration and assignment:

private x

sub new
x = 17
end sub

This partially explains the error message, but shouldn't the compiler still
be clever enough to implicitly declare variable x As Integer? It should be
able to infer the type from the expression just like within a procedure.
That's what Option Infer is all about. The compiler does have all the
information. I don't see the problem.

The only explanation I have is that it internally first splits the source
code line into these two parts (declaration+assigment) while loosing the
type information. Avoidable, IMO.
Armin

Dec 11 '07 #1
15 1759
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:
Hi,

after dealing with the new possiblities of the current VB version, I again
and still wonder why Option Infer can not be used with Option Strict On
at class level, i.e. when declaring a field:

Dim x = 17

Now I found out that the same line does work within a procedure. Actually I
wanted to ask why, but now I see that it is probably because the line is
realy split into declaration and assignment:

private x

sub new
x = 17
end sub

This partially explains the error message, but shouldn't the compiler still
be clever enough to implicitly declare variable x As Integer? It should be
able to infer the type from the expression just like within a procedure.
That's what Option Infer is all about. The compiler does have all the
information. I don't see the problem.

The only explanation I have is that it internally first splits the source
code line into these two parts (declaration+assigment) while loosing the
type information. Avoidable, IMO.
Armin
Probably why they added the var keword in C#...

var x = 17;

--
Tom Shelton
Dec 11 '07 #2
I don't understand your response. The var keyword is not valid at the
class level.

As for the OP question, there is no way to determine at compile time
what Dim x at the class level should be when it could be initialized
from multiple location within the class.

Public Class Stuff
Dim x

Public Sub New()
x = 17
End Sub

Public Sub New(ByVal s As String)
x = s
End Sub

Public Sub Append(ByVal s As String)
x &= s
End Sub
End Class

Which datatype should the anonymous type be when used in Append? The
compiler can't infer it.
Tom Shelton wrote:
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:
>Hi,

after dealing with the new possiblities of the current VB version, I again
and still wonder why Option Infer can not be used with Option Strict On
at class level, i.e. when declaring a field:

Dim x = 17

Now I found out that the same line does work within a procedure. Actually I
wanted to ask why, but now I see that it is probably because the line is
realy split into declaration and assignment:

private x

sub new
x = 17
end sub

This partially explains the error message, but shouldn't the compiler still
be clever enough to implicitly declare variable x As Integer? It should be
able to infer the type from the expression just like within a procedure.
That's what Option Infer is all about. The compiler does have all the
information. I don't see the problem.

The only explanation I have is that it internally first splits the source
code line into these two parts (declaration+assigment) while loosing the
type information. Avoidable, IMO.
Armin

Probably why they added the var keword in C#...

var x = 17;
Dec 12 '07 #3
I don't understand. In your case the assignment is missing, so it can't
work. My case is unambiguous:

Public Class Stuff
dim x= "string"
End Class

What does the compiler prevent from declaring x As String? It complains that
Option Strict requires an As clause. That's true unless Option Infer is On
and also the declaration line contains an assignment. Both preconditions are
met, so I don't see a reason for the error.

In other words, everyone who reads the source code knows how x must be
declared in this case. Are there any doubts? If the compiler does not
recognize the same, it's a flaw.

"Kelly Ethridge" <ke***@kellyethridge.comschrieb
I don't understand your response. The var keyword is not valid at
the class level.

As for the OP question, there is no way to determine at compile time
what Dim x at the class level should be when it could be initialized
from multiple location within the class.

Public Class Stuff
Dim x

Public Sub New()
x = 17
End Sub

Public Sub New(ByVal s As String)
x = s
End Sub

Public Sub Append(ByVal s As String)
x &= s
End Sub
End Class

Which datatype should the anonymous type be when used in Append? The
compiler can't infer it.
Tom Shelton wrote:
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:
Hi,
>
after dealing with the new possiblities of the current VB
version, I again and still wonder why Option Infer can not be
used with Option Strict On at class level, i.e. when declaring a
field:
>
Dim x = 17
>
Now I found out that the same line does work within a procedure.
Actually I wanted to ask why, but now I see that it is probably
because the line is realy split into declaration and assignment:
>
private x
>
sub new
x = 17
end sub
>
This partially explains the error message, but shouldn't the
compiler still be clever enough to implicitly declare variable x
As Integer? It should be able to infer the type from the
expression just like within a procedure. That's what Option
Infer is all about. The compiler does have all the information.
I don't see the problem.
>
The only explanation I have is that it internally first splits
the source code line into these two parts
(declaration+assigment) while loosing the type information.
Avoidable, IMO.
>
>
Armin
>
Probably why they added the var keword in C#...

var x = 17;
Dec 12 '07 #4
Yep, I will agree with that. However, it is stated that only local
variables can be inferred, not fields, unfortunately. As for my example,
it was in response to your second example.

Armin Zingler wrote:
I don't understand. In your case the assignment is missing, so it can't
work. My case is unambiguous:

Public Class Stuff
dim x= "string"
End Class

What does the compiler prevent from declaring x As String? It complains
that
Option Strict requires an As clause. That's true unless Option Infer is On
and also the declaration line contains an assignment. Both preconditions
are
met, so I don't see a reason for the error.

In other words, everyone who reads the source code knows how x must be
declared in this case. Are there any doubts? If the compiler does not
recognize the same, it's a flaw.

"Kelly Ethridge" <ke***@kellyethridge.comschrieb
>I don't understand your response. The var keyword is not valid at
the class level.

As for the OP question, there is no way to determine at compile time
what Dim x at the class level should be when it could be initialized
from multiple location within the class.

Public Class Stuff
Dim x

Public Sub New()
x = 17
End Sub

Public Sub New(ByVal s As String)
x = s
End Sub

Public Sub Append(ByVal s As String)
x &= s
End Sub
End Class

Which datatype should the anonymous type be when used in Append? The
compiler can't infer it.
Tom Shelton wrote:
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:
Hi,

after dealing with the new possiblities of the current VB
version, I again and still wonder why Option Infer can not be
used with Option Strict On at class level, i.e. when declaring a
field:

Dim x = 17

Now I found out that the same line does work within a procedure.
Actually I wanted to ask why, but now I see that it is probably
because the line is realy split into declaration and assignment:

private x

sub new
x = 17
end sub

This partially explains the error message, but shouldn't the
compiler still be clever enough to implicitly declare variable x
As Integer? It should be able to infer the type from the
expression just like within a procedure. That's what Option
Infer is all about. The compiler does have all the information.
I don't see the problem.

The only explanation I have is that it internally first splits
the source code line into these two parts
(declaration+assigment) while loosing the type information.
Avoidable, IMO.
Armin
Probably why they added the var keword in C#...

var x = 17;
Dec 12 '07 #5
Armin,

I did not study it deeply however
>
private x

sub bla
x = 17
end sub
sub ble
x = "Armin"
end sub

What kind of type the compiler has to assign?

Cor
Dec 12 '07 #6
On 2007-12-12, Kelly Ethridge <ke***@kellyethridge.comwrote:
I don't understand your response. The var keyword is not valid at the
class level.
That's true. I guess I missed that part of the original post. Sorry.

--
Tom Shelton
Dec 12 '07 #7
Hi Armin ,
http://www.danielmoth.com/Blog/2007/...e-in-c-30.html

http://www.danielmoth.com/Blog/2007/...er-in-vb9.html

HTH

Michel
"Armin Zingler" <az*******@freenet.deschreef in bericht
news:uO**************@TK2MSFTNGP06.phx.gbl...
Hi,

after dealing with the new possiblities of the current VB version, I again
and still wonder why Option Infer can not be used with Option Strict On
at class level, i.e. when declaring a field:

Dim x = 17

Now I found out that the same line does work within a procedure. Actually
I
wanted to ask why, but now I see that it is probably because the line is
realy split into declaration and assignment:

private x

sub new
x = 17
end sub

This partially explains the error message, but shouldn't the compiler
still
be clever enough to implicitly declare variable x As Integer? It should be
able to infer the type from the expression just like within a procedure.
That's what Option Infer is all about. The compiler does have all the
information. I don't see the problem.

The only explanation I have is that it internally first splits the source
code line into these two parts (declaration+assigment) while loosing the
type information. Avoidable, IMO.
Armin

Dec 12 '07 #8
"Cor Ligthert[MVP]" <no************@planet.nlschrieb
Armin,

I did not study it deeply however

private x

sub bla
x = 17
end sub
sub ble
x = "Armin"
end sub

What kind of type the compiler has to assign?
See Kelly's and Tom's latest answers. I don't understand what's so
difficulty with my question. It seems you have changed the quoted code from
"New" to "bla". That's not the same, so please don't quote it this way as if
I had written it. Looking at the code above, I don't expect it to work. My
question was different.

Again, the problem is /not/

private x

sub new
x = 17
end sub

because it was only an attempt to find an explanation why this

Dim x = 17

does not work. It might really be the cause, but in the end, the line
"Dim x = 17" is unambiguous and the compiler was able to infer the type of
x. It is integer, just like with local variables. I do know that this
currently does not work but I don't see any reason, why. There might be
an internal one, as explained above, that I, as a programmer and user of the
compiler should not care about, but there is no logical reason.
Consequently, it is possible to change the compiler in a future version. I
was looking for an exaplanation, why not.
Armin

Dec 12 '07 #9
"Armin Zingler" <az*******@freenet.deschrieb
See Kelly's and Tom's latest answers. I don't understand what's so
difficulty with my question.
To clarify: There's no relation between these two sentences, so please don't
get me wrong! :-)
Armin

Dec 12 '07 #10
Armin,

When I had written the reply, I was me not aware that I had changed it and
therefore should have removed the quoting.

Sorry for that.

However in my idea stays the fact that you cannot declare a global type
without its type and use it twice with two types. I don't see what the
compiler can do in that situation (maybe give a signal as it is two times
used with a different type, but that seems to me real gambling because what
is meant). That it is in the constructor can be an argument, however it is
clearer for me to avoid this to get no misunderstandings.

I probably don't have to say that I always write that the IDE/compiler
should do as much logic as there is to prevent messages like we see in C#
"This is a method however declared as property" (This is not the actual text
I am just writing this here in the message).

Cor

Dec 12 '07 #11
"Cor Ligthert[MVP]" <no************@planet.nlschrieb
Armin,

When I had written the reply, I was me not aware that I had changed
it and therefore should have removed the quoting.

Sorry for that.
ok :-)
However in my idea stays the fact that you cannot declare a global
type without its type
It is not without a type. The type can be infered from the expression.
and use it twice with two types.
It is not used with two types. It has exactly one type and only one type is
assigned.

This should work:

class bla
Dim x = 17

sub test
x = 23
end sub
end class
I do NOT expect this to work:

class bla
Dim x = 17

sub test
x = "whatever"
end sub
end class
Anyways, I don't see any /logical/ reason why type inference is limited to
local variables.
Armin

Dec 12 '07 #12
"Cor Ligthert[MVP]" <no************@planet.nlschrieb:
However in my idea stays the fact that you cannot declare a global type
without its type and use it twice with two types. I don't see what the
compiler can do in that situation (maybe give a signal as it is two times
used with a different type, but that seems to me real gambling because
what is meant).
Well, then you'd have to ask what the compiler should do in the snippet
below too:

\\\
Dim i = 10
i = "Hello World"
///

IMO, in both cases the variable should be typed in the type of the
expression on the right-hand side of 'Dim' + initialization statement.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Dec 12 '07 #13
\\\
Dim i = 10
i = "Hello World"
///
In my idea too, however as it is in a method there is known at least the
sequance of processing.

Cor
Dec 13 '07 #14
>\\\
>Dim i = 10
i = "Hello World"
///
In my idea too, however as it is in a method there is known at least
the sequance of processing.
Isn't that true for fields also? If you debug into a class, you hit the initialisation
of fields (for preinitialised fields) before the constructor.
I'm not sure if the sequence matters for compile time though.

A field needs to be allocated before it can be assigned, but you need to
find an assignment (for an inferred field) before you can determine it's
type.

An initialised field has a value in it's declaration which can be used to
figure the type. To my mind, this is easy and should have been done.

An uninitialised field might need to search the entire class to locate an
assignment before this judgement could be made.
A public unassigned field would need to search the entire project for such
an assignment, and you might not find anything having done that.

At this point you'd produce a compiler error.

--
Rory
Dec 13 '07 #15
"Cor Ligthert [MVP]" <no************@planet.nlschrieb
\\\
Dim i = 10
i = "Hello World"
///
In my idea too, however as it is in a method there is known at least
the sequance of processing.
But does this matter? That's something about runtime. But we are talking
about compile time. Just like you can look at the source code and see that i
is to be declared as an Integer here, the compiler should be able to see the
same. It does not matter at all when the assignment is done at runtime.
Armin

Dec 13 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad....
8
by: Charlie Smith | last post by:
Hi all, I have been following this ng lately and appreciate the level of understanding I see as well as the dedication to helping others. Many thanks for all the useful information I have picked...
4
by: Howard Kaikow | last post by:
I am trying to retrive some WMI properties using Option Strict On. This requires the use of InvokeMember. I know that there are alternative ways to get the values, but I want to learn how to...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
4
by: zacks | last post by:
A common programming technique I use in VB is making a collection of structures. But if Option Strict is on (which I would prefer), the .Add that adds the structure to the collection is flagged...
15
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
13
by: C. Moya | last post by:
I fully expected the lack of a way to set Option Strict globally to be fixed in SP1. I can't seem to figure out if it has been fixed or not. It still seems we have to add the declaration at the top...
1
by: Jerad Rose | last post by:
I believe this issue is specific to ASP.NET. Why does VB.NET (2.0) ignore the project-level setting for Option Strict? I have the setting turned on in web.config: <compilation debug="true"...
8
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.