繁体   English   中英

C#和VB.Net自定义类之间的区别

[英]Difference between C# and VB.Net custom classes

我离开VB.Net的时间已经太久了,我在C#中有一个自定义类,需要将其转换为VB.Net,并且想知道它们之间的主要区别。 我似乎无法在Vb.Net中使用某些类在C#中完成某些C#工作:VB.net中的公共类名或公共[类名](DataTable dt)

我的课程如下所示:

public class subcontractor
{
    public int organization_id { get; set; }
    public int subcontractor_id { get; set; }
    public int project_id { get; set; }
    public List<evaluationpoint> points { get; set; }

    public subcontractor() { }
    public subcontractor(DataTable dt)
    {
        organization_id = Convert.ToInt32(dt.Rows[0]["organization_id"].ToString());
        subcontractor_id = Convert.ToInt32(dt.Rows[0]["subcontractor_id"].ToString());
        project_id = Convert.ToInt32(dt.Rows[0]["project_id"].ToString());
        points = new List<evaluationpoint>();
        foreach ( DataRow dr in dt.Rows )
        { points.Add(new evaluationpoint(dr)); }
    }

    public class evaluationpoint
    {
        public int category_id { get; set; }
        public int eval_id { get; set; }
        public int rating { get; set; }

        public evaluationpoint() { }
        public evaluationpoint(DataRow dr)
        {
            category_id = Convert.ToInt32(dr["category_id"].ToString());
            eval_id = Convert.ToInt32(dr["eval_id"].ToString());
            rating = Convert.ToInt32(dr["rating"].ToString());
        }
    }
}

有什么区别

首先, 请阅读

VB.NET中的构造函数在语法上有所不同:

C#

Class Foo
{
    public Foo( int arg ) { }
}

VB

Class Foo
    Public Sub New( ByVal arg as Integer )

    End Sub
End Class

在大多数情况下,您可以在VB.NET中做任何与C#一样的事情,您只需要适当地更改语法即可。 有很多参考资料,请充分利用。

如果您的项目是在VB.NET中实现的,则其他项目(甚至是C#项目)仍然可以完全相同地调用VB.NET方法(反之亦然)。

一个Visual Studio解决方案可以具有VB.NET项目和C#项目。 每个(具有适当的项目引用)都可以访问对方的方法和类,因为它们都是已编译为MSIL以供CLR运行的.NET类。

构造函数的语法大不相同。 在C#中使用类名,在VB中使用新建,例如

Class SubContractor

   Public Sub New()
   End Sub

   Public Sub New(dt As DataTable)
   End Sub

End Class

关于差异的更多详细信息,包括该备忘单上的构造函数/析构函数差异。

暂无
暂无

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

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