简体   繁体   English

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

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

I have been out of VB.Net way too long, I have a custom class in C# that needs to be converted to VB.Net and would like to know the main differences between them. 我离开VB.Net的时间已经太久了,我在C#中有一个自定义类,需要将其转换为VB.Net,并且想知道它们之间的主要区别。 Certain things in C# I cannot seem to do in Vb.Net with classes such as use: public classname or public [classname](DataTable dt) in VB.net 我似乎无法在Vb.Net中使用某些类在C#中完成某些C#工作:VB.net中的公共类名或公共[类名](DataTable dt)

My class looks like the following: 我的课程如下所示:

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());
        }
    }
}

What are the differences 有什么区别

First, read this 首先, 请阅读

Constructors in VB.NET are different syntactically: VB.NET中的构造函数在语法上有所不同:

C# C#

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

VB VB

Class Foo
    Public Sub New( ByVal arg as Integer )

    End Sub
End Class

You can for the most part do anything in VB.NET that you can in C#, you are just going to have to change your syntax appropriately. 在大多数情况下,您可以在VB.NET中做任何与C#一样的事情,您只需要适当地更改语法即可。 There is plenty of reference material out there, make use of it. 有很多参考资料,请充分利用。

If your project is implementing in VB.NET, you other projects (even the C# ones) can still call the VB.NET methods all the same (and vice versa). 如果您的项目是在VB.NET中实现的,则其他项目(甚至是C#项目)仍然可以完全相同地调用VB.NET方法(反之亦然)。

A single Visual Studio solution can have VB.NET projects and C# projects. 一个Visual Studio解决方案可以具有VB.NET项目和C#项目。 Each (with the proper project references) can access the other's methods and classes since they're all .NET classes that have been compiled into MSIL for the CLR to run. 每个(具有适当的项目引用)都可以访问对方的方法和类,因为它们都是已编译为MSIL以供CLR运行的.NET类。

The syntax for constructors is rather different; 构造函数的语法大不相同。 in C# you use the class name, in VB you use New, for example 在C#中使用类名,在VB中使用新建,例如

Class SubContractor

   Public Sub New()
   End Sub

   Public Sub New(dt As DataTable)
   End Sub

End Class

There's more specific details on the differences, including constructor/destructor differences on this cheat sheet . 关于差异的更多详细信息,包括该备忘单上的构造函数/析构函数差异。

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

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