9

What is the VB statement expression Chr(0) equivalent in C#?

Thanks for any help.

Deanna
  • 23,876
  • 7
  • 71
  • 156
user225626
  • 1,091
  • 5
  • 16
  • 32
  • 2
    Even in VB6, `Chr(0)` is not the best option, you should use the `vbNullChar` constant as it doesn't require runtime evaluation. – Deanna Jun 13 '12 at 11:01

4 Answers4

14

The equivalent I believe is '\0'

I deleted the comment as I thought it is more appropriate to update in the post :)

sValue = vValue + Chr(0) 'As mentioned in your comment

can be written as

sValue += "\0";
Deanna
  • 23,876
  • 7
  • 71
  • 156
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • That's ok :) Deleted the comment and Updated the post above. – Siddharth Rout Jun 13 '12 at 07:11
  • @MarkJ: That is not my code. It was given in the comment as an example by OP. – Siddharth Rout Jun 13 '12 at 08:52
  • OK. So you're referring to a comment that was deleted before I looked at this question? That's confusing. How were we supposed to know that the `==` came from the OP? Maybe you could edit that into your answer – MarkJ Jun 13 '12 at 08:56
  • (+1 BTW) Worth mentioning that this is correct for values from 0-127. VB6 [Chr](http://msdn.microsoft.com/en-us/library/aa262692(v=vs.60).aspx) uses ANSI character codes from the current system code page, not Unicode character codes. Means that none of the alternatives in your answer would be correct for other values. You would need to import Microsoft.VisualBasic and use [Chr](http://msdn.microsoft.com/en-us/library/613dxh46(v=vs.90).aspx) – MarkJ Jun 13 '12 at 16:24
8

You can use (char)0. Or '\0' of course. If you want to call a method, you can use Convert.ToChar(0).

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • 1
    +1 Worth mentioning that this is correct for values from 0-127. VB6 [Chr](http://msdn.microsoft.com/en-us/library/aa262692(v=vs.60).aspx) uses ANSI character codes from the current system code page, not Unicode character codes. Means that none of the alternatives in your answer would be correct for other values. VB6 has a ChrW function that accepts Unicode character codes. – MarkJ Jun 13 '12 at 08:53
2

The equivalent would be (char)0 . If you are looking for escape sequences and other characters you can use \n and likewise

Vikram
  • 1,617
  • 5
  • 17
  • 37
-2
sValue == vValue + Strings.Chr(0)

You can try this yourself using this website

mb2o
  • 35
  • 6