Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String(33, 0) in VB 6.0 and equivalent in C#

Tags:

c#

vb6

What is the meaning of UserName = String(33, 0) in VB 6.0 and what will be the equivalent in C#.

Please help I'm getting error while converting VB 6.0 code into C#.

Thanks in advance.

like image 973
apratik Avatar asked Dec 16 '22 08:12

apratik


2 Answers

String in VB6 is a function that returns a string containing a repeating character string of the length specified.

String(number,character)

example:

strTest = String(5, "a")
' strTest = "aaaaa"

strTest = String(5, 97)
' strTest = "aaaaa" (97 is the ASCII code for "a")

In this case, String(33,0) will return a string containing 33 null characters.

The equivalent in C# would be

UserName = new String('\0', 33);
like image 72
linepogl Avatar answered Dec 17 '22 21:12

linepogl


In VB6, that function creates a string that contains 33 characters, all of whom have zero ordinal value.

Typically you do that because you are about to pass the string to some native function which fills out the buffer. In C# the closest equivalent to that would be to create a StringBuilder instance which you would then pass to the native code in a p/invoke function call.

I think that a direct translation of that single line of code is not particularly useful. That code exists in context and I strongly suspect that the context is important.

So, whilst you could create a new C# string with 33 null characters, what would be the point of that? Since the .net string is immutable, you cannot do very much of interest with it. In your VB6 code you will surely be mutating that object, and so StringBuilder is, in my view, the most likely tool for the job.

like image 26
David Heffernan Avatar answered Dec 17 '22 22:12

David Heffernan