473,387 Members | 1,398 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,387 software developers and data experts.

strict data type check

Hello!

I studied C# a little bit and am trying to compare it with VB.Net.

There's 'Option Strict' in VB.Net.
I thought that if I turn it on, it is as strict as C# when checking types.

See the following codes.

<vb.net>
Option Strict On
Imports System
Module Main
Sub Main()
Dim S As Short = 0
S += 1 '*******Error!!! It should be S += 1S.
Console.WriteLine(S.ToString())
End Sub
End Module
</vb.net>

The above code generates an error because in the line of S+=1, 1 is integer
and S is Short.

In C#, the same code was compiled ok.

<csharp>
short s = 0;
s += 1; //okay
</csharp>

What is the rule for that?
An integer can be assigned to a short?

If so, VB.Net with 'Option Strict On' is more strict than C#.
What can I do to make C# compiler check very strictly?

Thanks in advance!

Sam

Nov 15 '05 #1
2 12326
Well, C# compiler is checking very strictly in this case.
The reason it doesn't report any error is that C# defines some Implicit
constant expression conversion rules.

See C# language spec 6.1.6: A constant-expression (Section 7.15) of type int
can be converted to type sbyte, byte, short, ushort, uint, or ulong,
provided the value of the constant-expression is within the range of the
destination type.
So the interger 1 in your code is safely converted into 1S. But if the
number in your code is 65536, which is out of the range of a "short", the
compiler will complain.

Is there any way to make the C# compiler act just like the VB.net one? I
don't know. :(
Provided the C# compiler's behavior is pretty safe, I see no point of doing
so.

Hope it helps.
Ming Chen

"Sam Sungshik Kong" <ss*@chol.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hello!

I studied C# a little bit and am trying to compare it with VB.Net.

There's 'Option Strict' in VB.Net.
I thought that if I turn it on, it is as strict as C# when checking types.

See the following codes.

<vb.net>
Option Strict On
Imports System
Module Main
Sub Main()
Dim S As Short = 0
S += 1 '*******Error!!! It should be S += 1S.
Console.WriteLine(S.ToString())
End Sub
End Module
</vb.net>

The above code generates an error because in the line of S+=1, 1 is integer and S is Short.

In C#, the same code was compiled ok.

<csharp>
short s = 0;
s += 1; //okay
</csharp>

What is the rule for that?
An integer can be assigned to a short?

If so, VB.Net with 'Option Strict On' is more strict than C#.
What can I do to make C# compiler check very strictly?

Thanks in advance!

Sam

Nov 15 '05 #2
Sam,

What you're seeing is by desing. Section "6.1.6 Implicit constant expression
conversions" of C# specs states:

"An implicit constant expression conversion permits the following
conversions:
a.. A constant-expression (Section 7.15) of type int can be converted to
type sbyte, byte, short, ushort, uint, or ulong, provided the value of the
constant-expression is within the range of the destination type.
................."

The mentioned range checking makes such implicit convertions safe, so
there's really no need for "strict" option analogue. In fact I would say
that C# has that options always on compared to VB....

I'd suggest looking through the entire chapter "6. Conversions" to get
better understanding of conversions...

Regards,
Val.
"Sam Sungshik Kong" <ss*@chol.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hello!

I studied C# a little bit and am trying to compare it with VB.Net.

There's 'Option Strict' in VB.Net.
I thought that if I turn it on, it is as strict as C# when checking types.

See the following codes.

<vb.net>
Option Strict On
Imports System
Module Main
Sub Main()
Dim S As Short = 0
S += 1 '*******Error!!! It should be S += 1S.
Console.WriteLine(S.ToString())
End Sub
End Module
</vb.net>

The above code generates an error because in the line of S+=1, 1 is integer and S is Short.

In C#, the same code was compiled ok.

<csharp>
short s = 0;
s += 1; //okay
</csharp>

What is the rule for that?
An integer can be assigned to a short?

If so, VB.Net with 'Option Strict On' is more strict than C#.
What can I do to make C# compiler check very strictly?

Thanks in advance!

Sam

Nov 15 '05 #3

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

Similar topics

59
by: Philipp Lenssen | last post by:
I've tested some of the new Nokia 6600 functionality. It ships with WAP2 and XHTML Support (it says). What it does is check the Doctype -- if it's not the XHTML Mobile Profile Doctype, but a...
2
by: Brian Idzik | last post by:
I've successfully setup a xhtml 1.0 strict page with Mozilla & Netscape to display links in a toolbar into an internal <div id='content'> within the same document. The toolbar uses some...
9
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
7
by: Simon Strandgaard | last post by:
There are no <iframe> tag in xhtml strict, instead I should use <object>. If I change <iframe> to <object> then my javascript stops working. I am curious to how to use <object> with javascript...
1
by: Andy Crawford | last post by:
Even though I have set Strict Off, it is still on, thus causing this error. Here is the code in a templated Column of a DataGrid: (XPpro, Framework 1.1) <ItemTemplate> <TABLE width="100%">...
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: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a...
13
by: Shannon Richards | last post by:
Hello: I have a problem using ByRef arguments with Option Strict ON. I have built a generic sub procedure "ChangeValue()" to change the value of an argument if the new value is not the same as the...
50
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.