how to test a char variable for null?

M

mp

before looping through the characters in a string i define a variable to
hold the character preceeding the current character
char PreceedingCharacter;

before assigning PreceedingCharacter, how do I check it's state?

I keep getting error:
Use of unassigned local variable 'PreceedingCharacter'

i've tried
if (PreceedingCharacter.Equals ( null))
if(PreceedingCharacter==null)

at end of loop i assign PreceedingCharacter = CurrentCharacter;
so after the first character is examined, it (PreceedingCharacter ) will be
assigned.

it appears i have to assign to ' ' when defining it(although it's not
actually true)
char PreceedingCharacter = ' ';
is that the only way?
thanks
mark

private void CheckQuote2(string InputString)
{
//char PreceedingCharacter = ' '; <<< not true but it works
char PreceedingCharacter; <<< true but it doesn't work
char CurrentCharacter;

for (int CharPos = 0; CharPos < InputString.Length; CharPos++)
{
CurrentCharacter=InputString[CharPos];
if (CurrentCharacter.ToString()== "\"")
{
if (_AmInQuote == true)
{
//if (PreceedingCharacter.Equals ( null))
//if(PreceedingCharacter==null)
//if (is null(PreceedingCharacter ))
if(CharPos > 0)
{
if (PreceedingCharacter.ToString() == "\\")
{
Debug.Print("this is an escaped quote
character inside a string");
}
}
}
else
{
_AmInQuote = false;
Debug.Print("End quote");
}
}
else
{
if (PreceedingCharacter.ToString() == "\\")
{
//this is an escaped quote character outside a
string
}
else
{
_AmInQuote = true;
Debug.Print("Start quote");
}
}
PreceedingCharacter = CurrentCharacter;
}//end for
}//end Check
 
J

Jeff Johnson

before looping through the characters in a string i define a variable to
hold the character preceeding the current character
char PreceedingCharacter;

before assigning PreceedingCharacter, how do I check it's state?

I keep getting error:
Use of unassigned local variable 'PreceedingCharacter'

Chars are value types and therefore can never be null, so there's no point
in testing it for such. Why no set it to a "NUL" character, aka Chr$(0) in
VB:

char PreceedingCharacter = '\0';

I didn't look too deeply into your code to see if it could be handled
better. I'll leave that to Pete....
 
M

mp

Peter Duniho said:
That message seems clear to me. All local variables must be assigned
before being used. Including that one.

it was clear, but i thought i was able to do
string s;
if (s != null){}
i see that too is not allowed, not sure what i was remembering.

None of that would work, but you could compare your local variable to
whatever you want in that statement, there's no possible comparison syntax
that will fix the "unassigned local variable" error.


Hopefully you can see why the assignment you have in the code is not
guaranteed to happen before you try to use the local variable.

yes, i see, just thought there'd be a way to check for state of being
'pre-assigned'
You can use any literal there except '\\' and the code should work just
fine. I recommend '\0' (as I mentioned in the other thread, and as Jeff
has suggested here), just because that's the default value for the char
type anyway.

Pete

sounds good,
thanks to you both
mark
 
M

mp

Peter Duniho said:
On 11/5/10 9:38 AM, mp wrote:
snip

just because that's the default value for the char
type anyway.

Pete

so could i do either of the following and it would be equivalent?

char PreceedingCharacter = '\0';
char PreceedingCharacter = new char();
 

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

Top