Empty Character Literal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VJ

    Empty Character Literal


    I have a property for a control ( source code not available) . The property
    accepts single char as value.. so I can set it to '/' or ':' or anything
    like that...At initialize of my From I want this property to be set to a
    empty literal. I am able to do this at design time using VS.NET property
    box. This works for controls created at design time, but for some controls I
    create at run-time I want to do the same at Form_load..

    VJ



  • Paul E Collins

    #2
    Re: Empty Character Literal

    There is no "empty character literal", because every possible value
    for a char type is one character long. You can have zero length
    strings ("") but not zero-length characters, e.g. this will not
    compile:

    char ch = ''; // doesn't work

    The closest you can get is a space (' ') or a null character ('\0',
    ASCII zero).

    P.


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Empty Character Literal

      VJ <vijaybalki@yah oo.com> wrote:[color=blue]
      > I have a property for a control ( source code not available) . The property
      > accepts single char as value.. so I can set it to '/' or ':' or anything
      > like that...At initialize of my From I want this property to be set to a
      > empty literal. I am able to do this at design time using VS.NET property
      > box. This works for controls created at design time, but for some controls I
      > create at run-time I want to do the same at Form_load..[/color]

      There's no such thing as an "empty" character, any more than there's an
      "empty integer". What's the property used for? Character 0 (the "null"
      character) *might* be an appropriate start value, but it's hard to say
      without more information.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Christof Nordiek

        #4
        Re: Empty Character Literal


        "VJ" <vijaybalki@yah oo.com> schrieb im Newsbeitrag
        news:uPLg0BtyFH A.916@TK2MSFTNG P10.phx.gbl...[color=blue]
        >
        > I have a property for a control ( source code not available) . The
        > property accepts single char as value.. so I can set it to '/' or ':' or
        > anything like that...At initialize of my From I want this property to be
        > set to a empty literal. I am able to do this at design time using VS.NET
        > property box. This works for controls created at design time, but for some
        > controls I create at run-time I want to do the same at Form_load..
        >
        > VJ
        >[/color]
        If you can do it with VS.NET, simply do it and check the value while
        runtime.
        The you know, what character it is and set it in code.


        Comment

        • Kevin Spencer

          #5
          Re: Empty Character Literal

          Just a technical note about this: The reason that you can have empty strings
          is that a string is an array of char with a null terminator. That is, a
          string is actually (in memory) one char longer than the actual string,
          including the null terminator. A char, on the other hand, is not an array,
          but a single char.

          --
          HTH,

          Kevin Spencer
          Microsoft MVP
          ..Net Developer
          Ambiguity has a certain quality to it.

          "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in message
          news:di4djo$r06 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
          > There is no "empty character literal", because every possible value for a
          > char type is one character long. You can have zero length strings ("") but
          > not zero-length characters, e.g. this will not compile:
          >
          > char ch = ''; // doesn't work
          >
          > The closest you can get is a space (' ') or a null character ('\0', ASCII
          > zero).
          >
          > P.
          >
          >[/color]


          Comment

          • Daniel Jin

            #6
            Re: Empty Character Literal

            that is not true.

            ..net strings are not null terminated. the reason why you can have an
            empty string is because it's perfectly legal to have a 0-length array.

            Kevin Spencer wrote:[color=blue]
            > Just a technical note about this: The reason that you can have empty strings
            > is that a string is an array of char with a null terminator. That is, a
            > string is actually (in memory) one char longer than the actual string,
            > including the null terminator. A char, on the other hand, is not an array,
            > but a single char.
            >
            > --
            > HTH,
            >
            > Kevin Spencer
            > Microsoft MVP
            > .Net Developer
            > Ambiguity has a certain quality to it.
            >
            > "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in message
            > news:di4djo$r06 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=green]
            > > There is no "empty character literal", because every possible value for a
            > > char type is one character long. You can have zero length strings ("") but
            > > not zero-length characters, e.g. this will not compile:
            > >
            > > char ch = ''; // doesn't work
            > >
            > > The closest you can get is a space (' ') or a null character ('\0', ASCII
            > > zero).
            > >
            > > P.
            > >
            > >[/color][/color]

            Comment

            • VJ

              #7
              Re: Empty Character Literal

              Ok I got this one... The property had a corresponding Int property which if
              I set to zero then it achieved the same thing as setting to nothing using
              VS.NET property window...

              Thanks all for your input..
              VJ

              "VJ" <vijaybalki@yah oo.com> wrote in message
              news:uPLg0BtyFH A.916@TK2MSFTNG P10.phx.gbl...[color=blue]
              >
              > I have a property for a control ( source code not available) . The
              > property accepts single char as value.. so I can set it to '/' or ':' or
              > anything like that...At initialize of my From I want this property to be
              > set to a empty literal. I am able to do this at design time using VS.NET
              > property box. This works for controls created at design time, but for some
              > controls I create at run-time I want to do the same at Form_load..
              >
              > VJ
              >
              >
              >[/color]


              Comment

              • Kevin Spencer

                #8
                Re: Empty Character Literal

                Well, Daniel, you made me do my homework. In fact, we were both wrong. The
                truth is a bit more complex. It depends on the implementation used. If C# or
                C++ is used, it is indeed an array of WCHAR. If C is used, it is a
                null-terminated string.

                Now for some aspirin...

                --
                HTH,

                Kevin Spencer
                Microsoft MVP
                ..Net Developer
                Ambiguity has a certain quality to it.

                "Daniel Jin" <shinakuma8@yah oo.com> wrote in message
                news:1128691275 .053386.73830@g 43g2000cwa.goog legroups.com...[color=blue]
                > that is not true.
                >
                > .net strings are not null terminated. the reason why you can have an
                > empty string is because it's perfectly legal to have a 0-length array.
                >
                > Kevin Spencer wrote:[color=green]
                >> Just a technical note about this: The reason that you can have empty
                >> strings
                >> is that a string is an array of char with a null terminator. That is, a
                >> string is actually (in memory) one char longer than the actual string,
                >> including the null terminator. A char, on the other hand, is not an
                >> array,
                >> but a single char.
                >>
                >> --
                >> HTH,
                >>
                >> Kevin Spencer
                >> Microsoft MVP
                >> .Net Developer
                >> Ambiguity has a certain quality to it.
                >>
                >> "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in message
                >> news:di4djo$r06 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=darkred]
                >> > There is no "empty character literal", because every possible value for
                >> > a
                >> > char type is one character long. You can have zero length strings ("")
                >> > but
                >> > not zero-length characters, e.g. this will not compile:
                >> >
                >> > char ch = ''; // doesn't work
                >> >
                >> > The closest you can get is a space (' ') or a null character ('\0',
                >> > ASCII
                >> > zero).
                >> >
                >> > P.
                >> >
                >> >[/color][/color]
                >[/color]


                Comment

                • Daniel Jin

                  #9
                  Re: Empty Character Literal

                  I'm not sure what you mean by "depends on implemenation". according to Jon
                  Skeet's article on .NET string though, it is indeed null terminated
                  internally for p/invoke. so I was wrong, which makes my original point a
                  little inconsequential , that you don't need null termination for empty
                  strings, a 0-length array should do just fine.

                  "Kevin Spencer" wrote:
                  [color=blue]
                  > Well, Daniel, you made me do my homework. In fact, we were both wrong. The
                  > truth is a bit more complex. It depends on the implementation used. If C# or
                  > C++ is used, it is indeed an array of WCHAR. If C is used, it is a
                  > null-terminated string.
                  >
                  > Now for some aspirin...
                  >
                  > --
                  > HTH,
                  >
                  > Kevin Spencer
                  > Microsoft MVP
                  > ..Net Developer
                  > Ambiguity has a certain quality to it.
                  >
                  > "Daniel Jin" <shinakuma8@yah oo.com> wrote in message
                  > news:1128691275 .053386.73830@g 43g2000cwa.goog legroups.com...[color=green]
                  > > that is not true.
                  > >
                  > > .net strings are not null terminated. the reason why you can have an
                  > > empty string is because it's perfectly legal to have a 0-length array.
                  > >
                  > > Kevin Spencer wrote:[color=darkred]
                  > >> Just a technical note about this: The reason that you can have empty
                  > >> strings
                  > >> is that a string is an array of char with a null terminator. That is, a
                  > >> string is actually (in memory) one char longer than the actual string,
                  > >> including the null terminator. A char, on the other hand, is not an
                  > >> array,
                  > >> but a single char.
                  > >>
                  > >> --
                  > >> HTH,
                  > >>
                  > >> Kevin Spencer
                  > >> Microsoft MVP
                  > >> .Net Developer
                  > >> Ambiguity has a certain quality to it.
                  > >>
                  > >> "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in message
                  > >> news:di4djo$r06 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...
                  > >> > There is no "empty character literal", because every possible value for
                  > >> > a
                  > >> > char type is one character long. You can have zero length strings ("")
                  > >> > but
                  > >> > not zero-length characters, e.g. this will not compile:
                  > >> >
                  > >> > char ch = ''; // doesn't work
                  > >> >
                  > >> > The closest you can get is a space (' ') or a null character ('\0',
                  > >> > ASCII
                  > >> > zero).
                  > >> >
                  > >> > P.
                  > >> >
                  > >> >[/color]
                  > >[/color]
                  >
                  >
                  >[/color]

                  Comment

                  • Bruce Wood

                    #10
                    Re: Empty Character Literal

                    By the way, just for your information C# 2.0 has the concept of
                    nullable types. There is still no such thing as the "empty character",
                    but in C# 2.0 you can say:

                    char? emptyChar = null;

                    which isn't quite what you were after, but I thought I'd mention it
                    because it's sort of related. :)

                    Comment

                    Working...