473,657 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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+as sigment) while loosing the
type information. Avoidable, IMO.
Armin

Dec 11 '07 #1
15 1773
On 2007-12-11, Armin Zingler <az*******@free net.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+as sigment) 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*******@free net.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+as sigment) 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***@kellyeth ridge.comschrie b
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*******@free net.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+as sigment) 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***@kellyeth ridge.comschrie b
>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*******@free net.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+as sigment) 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***@kellyeth ridge.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*******@free net.deschreef in bericht
news:uO******** ******@TK2MSFTN GP06.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+as sigment) while loosing the
type information. Avoidable, IMO.
Armin

Dec 12 '07 #8
"Cor Ligthert[MVP]" <no************ @planet.nlschri eb
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*******@free net.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

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

Similar topics

11
2109
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. if i have class CCar and interface ICar when im doing this : ICar = new CCar
8
368
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 up. I have seen the recommendation for 'option strict' many times and realized that I had not set it on in my current project. When I did I discovered a number of small things and one that has me stumped. I am sure that is a result of my lack...
4
3007
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 use WMI with InvokeMember and Option Strict On. I'm getting an "Unknown Name" error in the following statement in the code below. objProp = typeObjProps.InvokeMember("Item", _
17
4480
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 binding. What should I do? Public Sub MyMnuHandler(ByVal sender As Object, ByVal e As System.EventArgs) If sender.checked = True Then sender.checked = False Else sender.checked = True
4
1350
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 with a compiler error (invalid type conversion). Is there a way to use a collection of structures WITH the Option Strict On?
15
1543
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 a good thing. i have now been asked to debug a vb2005 web app for 3 weeks and there is no mention of option strict in it, (there are also no classes defined, just a couple of structures) everything is define as 'as object', data coming back...
13
2314
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 of each and every single code module. Am I missing something here? -- -C. Moya www.cmoya.com
1
3133
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" strict="true" explicit="true" urlLinePragmas="true"/> And also in the preferences:
8
2418
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 man in question, in "Option Strict On" by default. Actually I don't believe I have code where this is not the case.
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.