简体   繁体   English

VB 6.0 中的 String(33, 0) 和 C# 中的等价物

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

What is the meaning of UserName = String(33, 0) in VB 6.0 and what will be the equivalent in C#. VB 6.0 中的UserName = String(33, 0)是什么意思,C# 中的等价物是什么。

Please help I'm getting error while converting VB 6.0 code into C#.请帮助我在将 VB 6.0 代码转换为 C# 时出错。

Thanks in advance.提前致谢。

String in VB6 is a function that returns a string containing a repeating character string of the length specified. VB6 中的String是一个函数,它返回一个包含指定长度的重复字符串的字符串。

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.在这种情况下, String(33,0)将返回一个包含 33 个空字符的字符串。

The equivalent in C# would be C# 中的等价物是

UserName = new String('\0', 33);

In VB6, that function creates a string that contains 33 characters, all of whom have zero ordinal value.在 VB6 中,该函数创建一个包含 33 个字符的字符串,所有这些字符的序数值都为零。

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 ap/invoke function call.在 C# 中,最接近的等价物是创建一个StringBuilder实例,然后将其传递给 ap/invoke 函数调用中的本机代码。

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?因此,虽然您可以创建一个包含 33 个空字符的新 C# string ,但这样做有什么意义呢? Since the .net string is immutable, you cannot do very much of interest with it.由于 .net 字符串是不可变的,因此您不能对它感兴趣。 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.在您的 VB6 代码中,您肯定会改变该对象,因此在我看来, StringBuilder是最有可能完成这项工作的工具。

I believe you are looking for:我相信您正在寻找:

UserName = new String((Char)0, 33);

Reference this for what the VB6 method did.参考这个VB6 方法做了什么。

You can create an function that perform this action, or o can do a extenssion of the class String .您可以创建一个执行此操作的函数,或者 o 可以对String类进行扩展。

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(strGen("01",3));
    }
    //param s is the string that you can generete and the n param is the how many times.
    private static string strGen(String s, int n){
        string r = string.Empty;
        for (int x = 1; x <= n; x++)
            r += string.Copy(s);
        return r;
    }
}

You can use this Code For duplication in C#:您可以将此代码用于 C# 中的重复:

   {
        this.Text = Strings.StrDup(33, "0");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM