473,782 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(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 12356
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******** ********@TK2MSF TNGP11.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.WriteLi ne(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******** ********@TK2MSF TNGP11.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.WriteLi ne(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
4780
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 XHTML1.0 Strict one, the media-handheld CSS is _ignored_. Only with the Nokia Doctype, the CSS is used. I find this really annoying as it goes against the whole idea of media-independent XHTML Strict along with stylesheets. On the good side, WML...
2
5769
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 javascript, but mostly CSS class & id elements. Mozilla & Netscape work fine handling the toolbar submenus & contents, but IE6 misbehaves on several areas. On loading the page, the target div (id='content') will not display. When a menu item is...
9
2147
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 about Option Strict? What advantages do I get with Option Strict On? Does better type statement make my code run faster? If anyone knows THE ANSWERS! please fill me in. I have ideas and belief but I would once and for all like to know what the...
7
2819
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 ? <?xml version="1.0" encoding="ISO-8859-1"?>
1
6817
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%"> <TR> <TD class="gridItemStyleNormal" align="left" width="50%"><%# Databinder.Eval(Container.DataItem, "CustomerName") %></TD> <TD class="gridItemStyleNormal" align="left" width="30%"><%# Databinder.Eval(Container.DataItem, "BusinessCategoryDesc")...
11
2119
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
1832
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 record in a sql Server view) .... Dim entry As Domino.NotesViewEntry Dim obj As Object str1 = entry.ColumnValues(0)
13
2516
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 original value...To accommodate all variable types I made the arguments in ChangeValue() of type object...I then check the typecode and do the correct comparison etc... With Option Strict ON I have to cast the arguments to the generic object...
50
6081
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> <title>Strict kills my widths!</title> </head> <body> <table style="width:400px; table-layout:fixed;">
0
9639
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
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
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...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
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
7486
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
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
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...
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.