Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Label.Text property

Label.Text property

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkB777
    wrote on last edited by
    #1

    I discovered an interesting occurance today. The below code creates a compile time error as expected... int i = 5; label1.Text = i; But this code works fine... int i = 5; label1.Text = "Test " + i; I had a look on Lutz Roeder's Reflector but couldn't find an obvious reason why this works. Could someone enlighten me? Mark.

    J L S M 4 Replies Last reply
    0
    • M MarkB777

      I discovered an interesting occurance today. The below code creates a compile time error as expected... int i = 5; label1.Text = i; But this code works fine... int i = 5; label1.Text = "Test " + i; I had a look on Lutz Roeder's Reflector but couldn't find an obvious reason why this works. Could someone enlighten me? Mark.

      J Offline
      J Offline
      J 0
      wrote on last edited by
      #2

      I'm pretty sure the + operator is overloaded in the System.String class to convert your int value to a string, then concatenate the 2 together.

      1 Reply Last reply
      0
      • M MarkB777

        I discovered an interesting occurance today. The below code creates a compile time error as expected... int i = 5; label1.Text = i; But this code works fine... int i = 5; label1.Text = "Test " + i; I had a look on Lutz Roeder's Reflector but couldn't find an obvious reason why this works. Could someone enlighten me? Mark.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, in string + something if something is not a string, the compiler will replace it by something.ToString() automatically. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        1 Reply Last reply
        0
        • M MarkB777

          I discovered an interesting occurance today. The below code creates a compile time error as expected... int i = 5; label1.Text = i; But this code works fine... int i = 5; label1.Text = "Test " + i; I had a look on Lutz Roeder's Reflector but couldn't find an obvious reason why this works. Could someone enlighten me? Mark.

          S Offline
          S Offline
          SeeSharp 0
          wrote on last edited by
          #4

          Hi MarkBrock Anything appended to a string is understood as string only by the compiler.

          --Here 2 See Sharp-- I have not failed. I've just found 10,000 ways that won't work. :)

          1 Reply Last reply
          0
          • M MarkB777

            I discovered an interesting occurance today. The below code creates a compile time error as expected... int i = 5; label1.Text = i; But this code works fine... int i = 5; label1.Text = "Test " + i; I had a look on Lutz Roeder's Reflector but couldn't find an obvious reason why this works. Could someone enlighten me? Mark.

            M Offline
            M Offline
            MarkB777
            wrote on last edited by
            #5

            Interesting... I had initially thought it may have been something a little more synister deep under the covers of C#. It's interesting that the C# developers decided to add functionality for converting non-string types if they are appended, but not for non-string types on their own. Thanks for clearing that up guys. Mark.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups