C # To VB.Net Conversion - an array of class objects with initialization

can someone help me pls im new to vb.net and im trying to work through the nhibernate firstsolution sample (written in C # re-posted here https://web.archive.org/web/20090831053827/http://blogs .hibernatingrhinos.com / nhibernate / archive / 2008/04/01 / your-first-nhibernate-based-application.aspx since his site crashed again) and I'm struggling to convert this bit. ive tried many converters; telerik, developerfusion, and a few others, but none of the generated code will compile, and I cannot understand why ...

if you are looking for this method, you will find where im upto ...

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

# vb , /.

fusion :

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() {..." ive , New(), , ... ? ? equivilent?

, , n # . ive . .

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

+3
3

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
+2

:

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

P.S: #. private readonly Product[] _products = new Product[]

  • private readonly Product[] _productsPrivate ReadOnly _products As Product()
  • new[]New ()
  • You Know What
+1

There is no elegant equivalent for this in VB8 (VS 2005).

+1
source

Source: https://habr.com/ru/post/1744479/


All Articles