Results 1 to 11 of 11

Thread: is Strict On for c#?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    is Strict On for c#?

    In vb.net, if Strict On, this code won't compile:

    VB Code:
    1. dim x as string = ""
    2. dim num as integer = 1
    3. x = y
    4. '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.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: is Strict On for c#?

    C# is always strict. As it should be.
    I don't live here any more.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: is Strict On for c#?

    Quote 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

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: is Strict On for c#?

    oh ok, thanks.

  10. #10
    New Member
    Join Date
    May 2006
    Posts
    6

    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

  11. #11
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width