有人可以帮助我,我是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和其他几个,但生成的代码都没有编译,我不知道为什么......
如果您搜索此方法,您将找到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();
}
}
因为我的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
干杯全部
答案 0 :(得分:2)
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
答案 1 :(得分:1)
试试这个:
Private ReadOnly _products() as Product =
{
New Product() With {.Name = “Melon″, .Category = "Fruits"},
...
}
P.S:问题在于原始的c#代码。它必须是private readonly Product[] _products = new Product[]
因此,转换分为三个部分
private readonly Product[] _products
- > Private ReadOnly _products As Product()
new[]
- > New ()
You Know What
答案 2 :(得分:1)
VB8(VS 2005)中没有优雅的等价物。