Determinate whether a 'Char' is null

G

Guest

How do you determinate whether a varible of data type 'Char' is null or not?

I used the following code, but it seemed that it didn't work:

Dim ans As Char
If ans = "" Then
MsgBox("Ans contains no value")
Else
MsgBox("Ans contains a value")
End If

Thanks.
 
H

Hal Rosser

Xero said:
How do you determinate whether a varible of data type 'Char' is null or not?

I used the following code, but it seemed that it didn't work:

Dim ans As Char
If ans = "" Then
MsgBox("Ans contains no value")
Else
MsgBox("Ans contains a value")
End If

Thanks.
--

if you put it in quotes, then its a string - not char.
try
if ans = toChar("") then
....etc...
 
S

Shiva

Try: If ans = CType ("", Char) Then

How do you determinate whether a varible of data type 'Char' is null or not?

I used the following code, but it seemed that it didn't work:

Dim ans As Char
If ans = "" Then
MsgBox("Ans contains no value")
Else
MsgBox("Ans contains a value")
End If

Thanks.
 
M

Mattias Sjögren

How do you determinate whether a varible of data type 'Char' is null or not?

A Char variable can't be "null" since Char is a value type. The
default value of a Char variable is zero, and you can test it with

If ans = ChrW(0) Then

or

If ans = ControlChars.NullChar Then



Mattias
 
C

Cor Ligthert

Jeff,

AFAIK can you test every value type for its initial value with

If x = nothing

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Shiva said:
Try: If ans = CType ("", Char) Then

A nullchar is different from a string of length 0.

Use 'ControlChars.NullChar'/'vbNullChar'/'ChrW(0)' instead.
 

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