简体   繁体   中英

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. 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

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:

C#

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

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. 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).

A single Visual Studio solution can have VB.NET projects and C# projects. 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.

The syntax for constructors is rather different; in C# you use the class name, in VB you use New, for example

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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