C# 到 VB.Net 转换 - 具有初始化的类对象数组

标签 c# vb.net c#-to-vb.net

有人能帮我吗,我是 vb.net 的新手,我正在尝试完成 nhibernate firstsolution 示例(用 c# 编写,重新发布在这里 https://web.archive.org/web/20090831053827/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx 因为他们的网站再次关闭),我正在努力转换这个少量。我试过很多转换器; telerik、developerfusion 和其他几个人,但生成的代码都无法编译,我看不出为什么……

如果你搜索这个方法,你会发现我在做什么......

private readonly Product[] _products = new[] 
{
    new Product {Name = "Melon", Category = "Fruits"},
    new Product {Name = "Pear", Category = "Fruits"},
    new Product {Name = "Milk", Category = "Beverages"},
    new Product {Name = "Coca Cola", Category = "Beverages"},
    new Product {Name = "Pepsi Cola", Category = "Beverages"},                 
};

' just the next part of the tutorial, ive resolved the "var" in vb.net 2005 bit np
private void CreateInitialData()        
{
    using(ISession session = _sessionFactory.OpenSession())                
        using(ITransaction transaction = session.BeginTransaction())                  
        {
           foreach (var product in _products)
               session.Save(product);
               transaction.Commit();
        }
}

因为我的 c# 和 vb 都是摇摇欲坠的,所以我尝试使用几个转换工具/站点。

开发者融合提供:

Private ReadOnly _products As Product() = New () {New Product(), New Product(), New Product(), New Product(), New Product()}

telerik 给

Private ReadOnly _products As Product() = New () {New Product() With { _
 .Name = "Melon", _
 .Category = "Fruits" _
}, New Product() With { _
 .Name = "Pear", _
 .Category = "Fruits" _
}, New Product() With { _
 .Name = "Milk", _
 .Category = "Beverages" _
}, Nw Product() With { _
 .Name = "Coca Cola", _
 .Category = "Beverages" _
}, New Product() With { _
 .Name = "Pepsi Cola", _
 .Category = "Beverages" _
}}

这似乎是最有用的,除了它提示这里预期的类型“New () {...” 我尝试了各种方法,包括评论中建议的 New() 中缺少的类型,但就是想不通……我错过了什么?我只是愚蠢吗?还是不存在并且等价?

这是我所有的代码,因为它是从教程 c# 到转换器站点的简单复制和粘贴。与此同时,我使用了开发人员融合定义并在另一种方法中手动填充了数组元素。即

Private _products As Product() = {New Product(), New Product(), New Product(), New Product(), New Product()}
Private Sub CreateInitialData()
    ' =================
    ' since i couldnt figure out how to convert the initialisation of the 
    ' "_products" array/collections whatever it is, i cheated and did this, 
    ' seems to work ok though probably poor practice
    With _products(0)
        .Name = "Melon"
        .Category = "Fruits"
    End With
    ' etc....
End Sub

重要的背景:vs2005、.net 2.0

大家好

最佳答案

VB.NET 8.0/Visual Studio 2005 不支持使用 With 语句直接初始化对象。但是,我相信您应该能够将初始化封装在一个函数中:

    Private ReadOnly _products() As Product = BuildProducts()

    Private Function BuildProducts() As Product()
        Dim products(4) As Product

        Dim product0 As New Product
        With product0
            .Name = "Melon"
            .Category = "Fruits"
        End With

        Dim product1 As New Product
        With product1
            .Name = "Pear"
            .Category = "Fruits"
        End With

        Dim product2 As New Product
        With product2
            .Name = "Milk"
            .Category = "Beverages"
        End With

        Dim product3 As New Product
        With product3
            .Name = "Coca Cola"
            .Category = "Beverages"
        End With

        Dim product4 As New Product
        With product4
            .Name = "Pepsi Cola"
            .Category = "Beverages"
        End With

        products(0) = product0
        products(1) = product1
        products(2) = product2
        products(3) = product3
        products(4) = product4

        Return products
    End Function

关于C# 到 VB.Net 转换 - 具有初始化的类对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2787544/

相关文章:

c# - 没有连接字符串的代码优先更改数据源

C# Master Detail View - 恢复未保存的更改

c# - 使用 C# 和 httpclient 将请求发送到 Rest Api

vb.net - VB.NET 中的类与模块

.net - Vb.NET 索引属性

.net - 将列表拆分为多个部分 - VB 转换失败

c# - 从 C# 中的 URI 字符串获取文件名

javascript - 如何从 vb 代码立即调用 javascript 函数

c# - 帮助将小型 C# WCF 应用程序转换为 Visual Basic(第 2 部分)

c# - .NET TreeView : Attach objects to TreeNodes