繁体   English   中英

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

[英]C# To VB.Net Conversion - array of class objects with initialisation

有人可以帮助我,我是vb.net的新手,我试图通过nhibernate第一个问题的样本(用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都是shakey,我试图使用几个转换工具/站点。

开发者融合给出:

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

试试这个:

Private ReadOnly _products() as Product = 
{ 
    New Product() With {.Name = “Melon″, .Category = "Fruits"},
    ...
}

PS:问题在于您的原始c#代码。 它必须是private readonly Product[] _products = new Product[]

因此,转换分为三个部分

  1. private readonly Product[] _products - > Private ReadOnly _products As Product()
  2. new[] - > New ()
  3. You Know What

在VB8(VS 2005)中没有优雅的等价物。

暂无
暂无

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

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