Empty Character Literal

  • Thread starter Thread starter VJ
  • Start date Start date
V

VJ

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
 
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.
 
VJ said:
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..

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.
 
VJ said:
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
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.
 
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.
 
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.
 
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
 
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.
 
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.
 
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top