Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将代码从VB转换为C#?_C#_.net_Vb.net_Vb.net To C# - Fatal编程技术网

如何将代码从VB转换为C#?

如何将代码从VB转换为C#?,c#,.net,vb.net,vb.net-to-c#,C#,.net,Vb.net,Vb.net To C#,主要是为了练习。 这是VB中的代码: Private Function ScrambleWord(ByVal word As String) As String Dim i As Integer = 0 Dim builder As System.Text.StringBuilder = New System.Text.StringBuilder() Dim random As Random = New Random() Dim index As Integer

主要是为了练习。 这是VB中的代码:

Private Function ScrambleWord(ByVal word As String) As String
    Dim i As Integer = 0
    Dim builder As System.Text.StringBuilder = New System.Text.StringBuilder()
    Dim random As Random = New Random()
    Dim index As Integer = 0

    Dim lower As Integer = 0
    Dim upper As Integer = 0

    Dim parts() As Char
    Dim part As Char

    If Not (String.IsNullOrEmpty(word)) Then
        If (word.Length > 3) Then
            parts = word.ToCharArray()
            builder.Append(word.Substring(0, 1))
            parts = word.Substring(1, word.Length - 2).ToCharArray()

            lower = LBound(parts) : upper = UBound(parts)
            For i = lower To upper
                index = random.Next(lower, upper)
                part = parts(index)
                parts(index) = parts(i)
                parts(i) = part
            Next
            builder.Append(parts)
            builder.Append(word.Substring(word.Length - 1, 1))
            Return builder.ToString()
        Else
            Return word
        End If
    Else
        Return String.Empty
    End If
End Function
我使用了一个在线翻译网站,并以以下内容结束:

private string ScrambleWord(string word)
{
    int i = 0;
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    int index = 0;

    int lower = 0;
    int upper = 0;

    char[] parts = null;
    char part = '\0';

    if (!(string.IsNullOrEmpty(word)))
    {
        if ((word.Length > 3))
        {
            parts = word.ToCharArray();
            builder.Append(word.Substring(0, 1));
            parts = word.Substring(1, word.Length - 2).ToCharArray();

            lower = Information.LBound(parts);
            upper = Information.UBound(parts);
            for (i = lower; i <= upper; i++)
            {
                index = random.Next(lower, upper);
                part = parts[index];
                parts[index] = parts[i];
                parts[i] = part;
            }
            builder.Append(parts);
            builder.Append(word.Substring(word.Length - 1, 1));
            return builder.ToString();
        }
        else
        {
            return word;
        }
    }
    else
    {
        return string.Empty;
    }
}
两个错误相同:

当前上下文中不存在名称“Information”

我从中获取VB代码,并尝试将其转换为 来自世界各地

您可以添加对Microsoft.VisualBasic的引用,并使用Microsoft.VisualBasic将
添加到文件顶部。

更改代码:

而不是

  lower = Information.LBound(parts);
  upper = Information.UBound(parts);

信息类是VB特有的;在编写C#代码时 您必须使用等效的:

  Information.LBound(value) == value.GetLowerBound(0);
  Information.UBound(value) == value.GetUpperBound(0);  

事实上,您在当前上下文中没有任何名为
信息的内容。(您发布的代码中没有任何声明,无论是VB还是C#)

反正你也不需要它。它被用来访问
部分
数组的边界,你已经知道了。您首先使用它们来创建数组。(您不需要
VisualBasic
名称空间。)

parts=word.Substring(1,word.Length-2.tocharray();
下限=0;
上部=零件。长度;//注意,这比上一个指数高一个,
//因为第一个索引是零

对于(i=lower;i
private static string ScrambleWord(string word)
{
    // easy exits come first
    if (string.IsNullOrEmpty(word))
        return String.Empty;

    if (word.Length <= 3)
        return word;

    // now do the work
    StringBuilder builder = new StringBuilder();
    Random rand = new Random();

    builder.Append(word.Substring(0, 1));

    List<Char> parts = word.Substring(1).ToList();

    while (parts.Count() > 0)
    {
        int upper = parts.Count();
        int index = rand.Next(0, upper);
        builder.Append(parts[index]);
        parts.RemoveAt(index);
    }

    return builder.ToString();
}
私有静态字符串加扰字(字符串字)
{
//容易的出口是第一位的
if(string.IsNullOrEmpty(word))
返回字符串。空;
如果(字长0)
{
int上限=parts.Count();
int index=rand.Next(0,上限);
附加(零件[索引]);
零件拆卸(索引);
}
返回builder.ToString();
}

熟悉C语言的人可能会指出错误。

你遗漏了一些细节。错误是什么,在哪行代码中出现了错误?请尝试而不是lower=Information.LBound(parts);此下限=parts.GetLowerBound(0);而不是上限=Information.UBound(parts);此上限=parts.GetUpperBound(0);您可以添加Microsoft.VisualBasic的引用,并使用Microsoft.VisualBasic添加
您正在练习将VB.NET代码转换为C#?显示的代码无论如何都可以重写。
lower=LBound(parts):upper=UBound(parts)
->删除:然后再次通过代码转换器运行。+1但是您应该提到,
UBound
返回
Length-1
。所以可以使用
upper=parts.Length-1
或修改for循环。@p.s.w.g:注意。我在脑海中自动使用了
,而不是
@KenWhite:仅供参考,关于为什么特别需要LBound的描述有点不准确。在VB6中,可以声明数组的下限和上限,这样数组就不会总是从索引0开始,事实上,它甚至可以从负数开始,即变暗部分(-3到3)。事实上,如果内存可用,则默认下限在VB生命周期的某个点从1更改为0,反之亦然。您是正确的,多维数组是使用L/UBound的另一个原因,因为第二个可选参数指定了要获取绑定的维度。@competable\u tech:谢谢您提供的信息。我对VB6不太熟悉;在我发现Delphi之前,我非常简单地使用了它,它比Delphi好得多。:-)我将编辑以添加下限解释。@KenWhite:没问题,我用VB编程(从3.0开始)的时间比我想起来的要多,所以其中一些已经铭刻在我的脑海中。以下是LBound上的VB6文档供参考:
  Information.LBound(value) == value.GetLowerBound(0);
  Information.UBound(value) == value.GetUpperBound(0);  
parts = word.Substring(1, word.Length - 2).ToCharArray();
lower = 0;
upper = parts.Length;            // Note this is one higher than the last index,
                                 // because the first index is zero
for (i = lower; i < upper; i++)  // So use < rather than <= here
{
  // Other code here.
}
private static string ScrambleWord(string word)
{
    // easy exits come first
    if (string.IsNullOrEmpty(word))
        return String.Empty;

    if (word.Length <= 3)
        return word;

    // now do the work
    StringBuilder builder = new StringBuilder();
    Random rand = new Random();

    builder.Append(word.Substring(0, 1));

    List<Char> parts = word.Substring(1).ToList();

    while (parts.Count() > 0)
    {
        int upper = parts.Count();
        int index = rand.Next(0, upper);
        builder.Append(parts[index]);
        parts.RemoveAt(index);
    }

    return builder.ToString();
}