Implicit conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azzurit
    New Member
    • May 2010
    • 4

    Implicit conversion

    Hello

    I need the following code to work:
    int a = "1234"

    How can I modify the int type to accept string (or in ideal case to accept both string and int)???

    Can I somehow modify it?
    (I tried to inherite int to change its properties, but the compiler returns an error becouse of inheriting (I can't derive int)).

    I would appreciate any help.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You don't.
    An int holds a number. A string holds text.

    You need to convert your text to a number before you can assign it to an int.

    Comment

    • azzurit
      New Member
      • May 2010
      • 4

      #3
      It's clear. But instead of
      int a = Int32.Parse("12 34");
      I need to write only
      int a = "1234";

      Maybe the operator overriding could help... What do you think?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I think you can either do it the way the C# language is meant to do it... or you can fight it.

        *WHY* do you think you need to write it only as
        Code:
        int a = Int32.Parse("1234");
        What is the reason you can't do
        Code:
        int a = Int32.Parse("1234");
        Why do you need to do it differently than the other 3 million C# programmers?

        Comment

        • azzurit
          New Member
          • May 2010
          • 4

          #5
          Becouse my prof wants me to do this if I want A:-D

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Your professor wants you to do what isn't defined in the C# language?
            Hmmm....
            You can't do what the language doesn't support.
            Code:
            int a = "123";
            simply cannot be done in C#.
            an int cannot hold a string. Period. That's the laws of physics as far as the C# language is concerned.

            MSDN for int
            MSDN for string

            If you look at how memory is allocated for the different datatypes you can see why. An int is 4 bytes big. "1234" at its smallest is 5 bytes: 4 for the numbers plus one for the null termination.

            Comment

            • azzurit
              New Member
              • May 2010
              • 4

              #7
              I don't want int to hold a string.
              I want it to convert string to int automatically (without using Parse), when assigned...

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                I want my VW beetle to haul 20 tons of cargo like an 18 wheeler; but it ain't gonna happen.

                What you *want* and what you *can accomplish* are often very different things.

                Comment

                • artov
                  New Member
                  • Jul 2008
                  • 40

                  #9
                  Have you thought that your answer could be "I cannot do it, since there is no implicit conversion from string to int, since strings can contain characters that are not numbers"?

                  Comment

                  Working...