我有以下代码,我需要转换为VB.NET.问题是我找到的每个翻译工具都是错误地转换添加处理程序部分.我自己似乎无法做到这一点.
FtpClient ftpClient = new FtpClient();
ftpClient.UploadProgressChanged += new EventHandler<UploadProgressChangedLibArgs>(ftpClient_UploadProgressChanged);
ftpClient.UploadFileCompleted += new EventHandler<UploadFileCompletedEventLibArgs>(ftpClient_UploadFileCompleted);
Run Code Online (Sandbox Code Playgroud) 出于教育目的,我尝试将以下Linq表达式从"Linq in action"转换为VB.net
原创C#
var list =
from book in SampleData.Books
group book by new { book.Publisher.Name, book.Subject }
into grouping
select new {
Publisher = grouping.Key.Publisher,
Subject = grouping.Key.Subject,
Book = grouping
};
Run Code Online (Sandbox Code Playgroud)
我的尝试:
Dim list = _books.GroupBy(Function(book) New With {.Publisher = book.Publisher.Name,
book.Subject}).
Select(Function(grouping) New With {.Publisher = grouping.Key.Publisher,
.Subject = grouping.Key.Subject,
.Books = grouping})
For Each item In list
Console.WriteLine("Publisher:" & item.Publisher & ", Subject:" & item.Subject)
For Each Book In item.Books
Console.WriteLine(" " & Book.Title)
Next …
Run Code Online (Sandbox Code Playgroud) 我有一个团队,人们对C#非常熟悉,但我们要求在VB.net中编写一个项目.在C#中思考并在飞行中转换为VB有多难?那可行吗?
你能否列出我们可以遇到的问题?
我听说VB.net没有闭包.对于.net 3.5来说,这仍然是真的吗?
(这与其他问题有关)
如果你定义一个接口,其中有一个属性,只有一个getter(=只读在VB.NET),为什么你能在实施与C#类定义的制定者而不是用VB?
我原以为它是在.NET级别定义的,而不是特定于语言的.
示例:对于此接口
'VB.NET
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interface
Run Code Online (Sandbox Code Playgroud)
要么
//C# code
interface IPublicProperty
{
string PublicProperty { get; }
}
Run Code Online (Sandbox Code Playgroud)
这是C#中的正确实现:
public class Implementer:IPublicProperty
{
private string _publicProperty;
public string PublicProperty
{
get
{
return _publicProperty;
}
set
{
_publicProperty = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这在VB.NET中无效
Public Property PublicProperty As String Implements SomeInterface.PublicProperty
Get
Return _myProperty
End Get …
Run Code Online (Sandbox Code Playgroud) 我偶然发现了CInt的一个问题并将double转换为整数.
问题如下:
CInt(10.5) 'Result is 10
CInt(10.51) 'Result it 11, but I expected 10...
Run Code Online (Sandbox Code Playgroud)
我习惯了C#风格转换,其中(int) 10.51
10.
正如在关于Integer.Parse和CInt的问题中指出的那样,结果只是以某种方式舍入.
但是,我只需要获得整数部分并扔掉小数部分.如何在VB.NET中实现这种类型的转换?经过一些研究后,我发现我可以使用该Fix()
功能来完成这个技巧,但它是最好的选择吗?
在C#中,我可以这样做:
public string myProperty { get; private set; }
Run Code Online (Sandbox Code Playgroud)
这被称为"自动吸气器/定位器"(据我所知).VB.NET是否支持这些?到目前为止,凭借我的属性,我所能做的就是:
Public Property myProperty As String
Get
Return String.Empty
End Get
Private Set(ByVal value As String)
somethingElse = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
这是非常笨重的.
所以...有更好的方法吗?
我一直在看的ASP.NET MVC的店面视频系列再次和看到的东西,我从来没有注意到或祈祷,任何关注前.我注意到this
在各种方法的签名列表中有很多引用.这是一个例子:
public static Category WithCategoryName(this IList<Category> list, string categoryName)
{
return
(
from s in list
where s.Name.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase)
select s
)
.SingleOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
我立刻就明白了IList<Category> list
和string categoryName
中的签名,但感到无所适从this
呢.
所以,作为一个95%的VB人,我将代码弹出到我最喜欢的转换器中并得到:
<System.Runtime.CompilerServices.Extension>
Public Shared Function WithCategoryName(list As IList(Of Category), categoryName As String) As Category
Return
(
From s In list
Where s.Name.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase)
Select s
)
.SingleOrDefault()
End Function
Run Code Online (Sandbox Code Playgroud)
首先,我不完全确定为什么<System.Runtime.CompilerServices.Extension>
被包括在内,也许只是转换器,然而,正如你所看到的,this
除非与上述内容有关,否则我没有转换成任何东西<System.Runtime.CompilerServices.Extension>
.
所以问题是:
this
在C#方法签名中实际引用和/或做什么?以下在c#中工作正常(假设value
是object
):
if (value is DateTime)
Run Code Online (Sandbox Code Playgroud)
什么是VB.NET的等效检查?
我正在将一些代码从C#转换为VB.NET,我需要知道C#的using指令的等价物.
更新:对不起,但到目前为止我还没有得到答案.这是一个C#示例:
using moOutlook = Microsoft.Office.Interop.Outlook;
using moExcel = Microsoft.Office.Interop.Excel;
namespace ReportGen
{
class Reports
Run Code Online (Sandbox Code Playgroud) 我有以下C#方法:
private static string GetMemberName<T>(Expression<Func<T>> expr)
{
MemberExpression memberExpr = expr.Body as MemberExpression;
if (memberExpr == null)
throw new ArgumentOutOfRangeException("Wrong type of lambda...");
return memberExpr.Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样使用它来打印类级别字段,方法参数或本地var的名称(注意这是pre-C#6.0 nameof
运算符):
private static int _myFieldVar = 62;
private static void DoStuff(int myMethodParam)
{
int myLocalVar = 2;
Debug.Print(GetMemberName(() => myMethodParam)); // prints "myMethodParam"
Debug.Print(GetMemberName(() => myLocalVar)); // prints "myLocalVar"
Debug.Print(GetMemberName(() => _myFieldVar)); // _myFieldVariable
}
Run Code Online (Sandbox Code Playgroud)
现在我想将此代码转换为VB.NET,所以这里是GetMemberName
方法:
Private Function GetMemberName(Of T)(expr As Expression(Of Func(Of T))) As String
Dim memberExpr As …
Run Code Online (Sandbox Code Playgroud) c#-to-vb.net ×10
vb.net ×8
.net ×4
c# ×4
linq ×2
properties ×1
rounding ×1
signature ×1
vb.net-to-c# ×1