|
-
May 12th, 2006, 05:30 PM
#1
Thread Starter
Frenzied Member
is Strict On for c#?
In vb.net, if Strict On, this code won't compile:
VB Code:
dim x as string = ""
dim num as integer = 1
x = y
'this would be an error
in c#.net, this code WILL compile:
Code:
string x = "";
int num = 1;
x = num;
is there a way to make it so that the c# code DOES NOT compile.
-
May 12th, 2006, 06:44 PM
#2
Re: is Strict On for c#?
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
Code:
Error 1: Cannot implicitly convert type 'int' to 'string'
Last edited by sunburnt; May 12th, 2006 at 06:48 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
May 13th, 2006, 06:58 AM
#3
Re: is Strict On for c#?
C# is always strict. As it should be.
I don't live here any more.
-
May 15th, 2006, 04:10 PM
#4
Thread Starter
Frenzied Member
Re: is Strict On for c#?
really? I just tried this:
Code:
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:
Code:
int y = 1;
string x;
x = "hey" + y; //added "hey" with an append
and it compiles
-
May 15th, 2006, 04:33 PM
#5
Re: is Strict On for c#?
 Originally Posted by benmartin101
really? I just tried this:
Code:
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:
Code:
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 15th, 2006, 09:07 PM
#6
Re: is Strict On for c#?
The first case is assignment of a variable at runtime. The second case is assignment of a literal at compile time.
-
May 15th, 2006, 09:28 PM
#7
Re: is Strict On for c#?
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:
Code:
string x;
x = (new DataTable("Table")) + "hey";
MessageBox.Show(x);
This will display "Tablehey".
-
May 16th, 2006, 06:11 AM
#8
Re: is Strict On for c#?
When string concatenation occurs, the ToString() method of everything being concatenated is called implicitly. Thats why it is legal.
I don't live here any more.
-
May 19th, 2006, 01:31 PM
#9
Thread Starter
Frenzied Member
-
May 24th, 2006, 10:55 AM
#10
New Member
Re: is Strict On for c#?
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
-
May 24th, 2006, 08:58 PM
#11
Re: is Strict On for c#?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|