简体   繁体   English

将vb.net代码段转换为c#

[英]converting a vb.net code snippet to c#

Visual Studio 2005. Visual Studio 2005。

I was converting some of my source code to C#. 我正在将一些源代码转换为C#。

However, when I was doing the code snippet below, I noticed I don't have the IsNumber method. 但是,当我在下面的代码片段中时,我注意到我没有IsNumber方法。

Why is the IsNumber missing? 为什么缺少IsNumber? I wanted to use it so that I can force a user to enter only numbers. 我想使用它,以便我可以强制用户只输入数字。

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar.IsNumber(e.KeyChar) = False Then
            e.Handled = True
        End If
End Sub

Many thanks for any suggestions, 非常感谢任何建议,

use the static char.IsNumber() : 使用静态char.IsNumber()

char.IsNumber(e.KeyChar);

By the way, I think you want to use char.IsDigit() instead. 顺便说一句,我认为你想使用char.IsDigit()代替。 This comes from the MSDN site: 这来自MSDN网站:

IsNumber() determines if a Char is of any numeric Unicode category. IsNumber()确定Char是否属于任何数字Unicode类别。 This contrasts with IsDigit() , which determines if a Char is a radix-10 digit. 这与IsDigit()形成对比,后者确定Char是否是基数为10的数字。

In other words, IsNumber() also returns true for non-western numbers like 六 (Chinese '6'). 换句话说,对于非西方数字(如六(中文'6')), IsNumber()也返回true。

C# is more strict in allowing you to call shared members on an instance variable. 允许您在实例变量上调用共享成员时,C#更严格。

Your original code gives me a warning in VB. 您的原始代码在VB中给出了警告。

Warning 1 Access of shared member, constant member, enum member or nested type through an instance; 警告1通过实例访问共享成员,常量成员,枚举成员或嵌套类型; qualifying expression will not be evaluated. 不会评估合格表达式。

In C# - it's just not allowed. 在C#中 - 它是不允许的。 Your solution to use char.IsNumber is the way to go; 您使用char.IsNumber解决方案是char.IsNumber的方法; and that code will work the same in either language. 并且该代码在任何一种语言中的工作方式都相同。

public class Example1
{
    public static int Test() { return 0; }

    public Example1()
    {
        this.Test();  // This doesn't work
        Example1.Test();  // This does
    }
}

Solved. 解决了。

Used the following: 使用以下内容:

    if (char.IsNumber(e.KeyChar) == false)
    {
        e.Handled = true;
    }

we normally use developer fusion to convert code from vb.net to c# and vice-versa. 我们通常使用开发人员融合将代码从vb.net转换为c#,反之亦然。 you can also try it. 你也可以尝试一下。

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

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