Click to See Complete Forum and Search --> : is Strict On for c#?
benmartin101
May 12th, 2006, 05:30 PM
In vb.net, if Strict On, this code won't compile:
dim x as string = ""
dim num as integer = 1
x = y
'this would be an error
in c#.net, this code WILL compile:
string x = "";
int num = 1;
x = num;
is there a way to make it so that the c# code DOES NOT compile.
sunburnt
May 12th, 2006, 06:44 PM
I'm pretty sure that that won't compile in C#. You need to explicitly convert from an integer to a string. C# Doesn't have an "Option Strict" because C# is much more type-strict by default .
Edit: I just tried it to make sure ;)
Error 1: Cannot implicitly convert type 'int' to 'string'
wossname
May 13th, 2006, 06:58 AM
C# is always strict. As it should be. :D
benmartin101
May 15th, 2006, 04:10 PM
really? I just tried this:
int y = 1;
string x;
x = y;
and I got the error message about not being able to convert.
but if i change it to this:
int y = 1;
string x;
x = "hey" + y; //added "hey" with an append
and it compiles
ComputerJy
May 15th, 2006, 04:33 PM
really? I just tried this:
int y = 1;
string x;
x = y;
and I got the error message about not being able to convert.
but if i change it to this:
int y = 1;
string x;
x = "hey" + y; //added "hey" with an append
and it compiles
All type strict programming languages does that (maybe C doesn't) but why don't you want it to compile, a string is flixable and they designed it to be like this
penagate
May 15th, 2006, 09:07 PM
The first case is assignment of a variable at runtime. The second case is assignment of a literal at compile time.
jmcilhinney
May 15th, 2006, 09:28 PM
The first case does not work because you cannot ASSIGN an int value to a string variable. The second case works because of how the "+" operator is implemented. If one of the operands is a string and one is not the "+" operator will call ToString on the non-string operand to convert it to a string and then concatenate the two. Finally the resulting string is assigned to the string variable, so the assignment is not an issue. You can use any type of object and that second scenario will work because every object has a ToString method:string x;
x = (new DataTable("Table")) + "hey";
MessageBox.Show(x);
This will display "Tablehey".
wossname
May 16th, 2006, 06:11 AM
When string concatenation occurs, the ToString() method of everything being concatenated is called implicitly. Thats why it is legal.
benmartin101
May 19th, 2006, 01:31 PM
oh ok, thanks.
MrMM
May 24th, 2006, 10:55 AM
the normal way to convert an int to string would be
int x = 1;
string y;
y = x.ToString(); // explicit conversion
or
//comments go after the double slashes
int x = 1;
string y;
y = "" + x; // this is called implicit conversion.
the other way around:
string x = "1";
int y = int.Parse(x);
was that what you meant ?
greetings
David Anton
May 24th, 2006, 08:58 PM
VB with Option Strict On is stricter about some things than C#.
C# is stricter about some things than VB with Option Strict On.
i.e., VB with Option Strict On and C# are not identically strict.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.