标签: c#-to-vb.net

在VB.Net中构造一个对象并调用一个没有赋值的方法

我不完全确定C#的作用是什么,所以我没有运气搜索VB.Net等效语法(如果它存在,我怀疑它可能没有).

在c#中,您可以这样做:

public void DoSomething() {
    new MyHelper().DoIt(); // works just fine
}
Run Code Online (Sandbox Code Playgroud)

但据我所知,在VB.Net中,您必须将辅助对象分配给局部变量,否则您只会收到语法错误:

Public Sub DoSomething()
    New MyHelper().DoIt() ' won't compile
End Sub
Run Code Online (Sandbox Code Playgroud)

这是我每天都在混合语言项目中遇到的好奇心之一 - 通常有一个VB.Net等价物,它使用的语法不那么明显.任何人?

vb.net c#-to-vb.net

4
推荐指数
1
解决办法
726
查看次数

如何:将匿名方法转换为VB.NET

我在C#中有以下内容:

public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds, EventHandler completedEvent)
{
    double fromValue = (double)animatableElement.GetValue(dependencyProperty);

    DoubleAnimation animation = new DoubleAnimation();
    animation.From = fromValue;
    animation.To = toValue;
    animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);

    //// HERE ----------------------------------------------------
    animation.Completed += delegate(object sender, EventArgs e)
    {
        //
        // When the animation has completed bake final value of the animation
        // into the property.
        //
        animatableElement.SetValue(dependencyProperty,
                                 animatableElement.GetValue(dependencyProperty));
        CancelAnimation(animatableElement, dependencyProperty);

        if (completedEvent != null)
        {
            completedEvent(sender, e);
        }
    };
Run Code Online (Sandbox Code Playgroud)

我有一些问题将匿名方法转换为VB.NET.

我的变体是

  AddHandler animation.Completed,
    Function(sender As …
Run Code Online (Sandbox Code Playgroud)

.net c# vb.net anonymous-function c#-to-vb.net

4
推荐指数
1
解决办法
2443
查看次数

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

尝试编写一个将列表拆分为子列表的方法.

Private Function SplitIdsIntoChunks(ByVal keys As List(Of String)) As List(Of List(Of String))
    Return keys _
        .Select(Function(x, i) New With {Key .Index = i, Key .Value = x}) _
        .GroupBy(Function(x) (x.Index / 10000)) _
        .Select(Function(x) x.Select(Function(v) v.Value).ToList()) _
        .ToList()
End Function
Run Code Online (Sandbox Code Playgroud)

我从这里使用了C#解决方案.C#解决方案工作正常.

我用VB编写的版本返回一个列表的集合,其中包含一个元素而不是10000.我哪里出错了?

提前致谢.

编辑1:

用法:

Dim chunks As List(Of List(Of String)) = SplitIdsIntoChunks(keys)
Run Code Online (Sandbox Code Playgroud)

'钥匙'内容:

在此输入图像描述

我的方法返回一个列表,其中包含一个项目:

在此输入图像描述

预期结果:两个列表的列表 - 首先是10000个项目,第二个是6256个.

.net vb.net c#-to-vb.net

4
推荐指数
1
解决办法
2682
查看次数

非常具体的C#到VB.NET转换问题

我目前正在开发一个使用AutoFac Inversion of Control容器的项目.

我试图将一些示例代码从C#转换为我的现有项目的代码库,该代码库是用VB.NET编写的,我遇到了问题.

原始代码行是:

EventHub.Subscribe<HandshakingEvent>(container.Resolve<HandshakeAuthenticator>().CheckHandshake);
Run Code Online (Sandbox Code Playgroud)

我转换为:

EventHub.Subscribe(Of HandshakingEvent)(Container.Resolve(Of HandshakeAuthenticator)().CheckHandshake)
Run Code Online (Sandbox Code Playgroud)

但是 - 这导致了错误,"没有为CheckHandshake的参数'ev'指定参数".

EventHub.Subscribe(Of HandshakingEvent)过程的参数类型是System.Action(HandshakingEvent)

我可以看出问题是什么,我只是不确定该怎么做!我尝试过使用'AddressOf',但这似乎也不起作用.

提前谢谢...... - 克里斯

c# vb.net c#-to-vb.net

3
推荐指数
1
解决办法
188
查看次数

C#DLLImport转换为VB.NET DLLImport ...我错过了什么?

在C#我有这个:

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();
Run Code Online (Sandbox Code Playgroud)

我试图转换为VB.NET因此:

<DllImport("user32.dll", EntryPoint:="GetDesktopWindow")>
Function GetDesktopWindow() As IntPtr
End Function
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误......"Imports System.Runtime.InteropServices.DllImportAttribute不能应用于实例方法."

有人可以解释我需要做些什么来解决这个问题,甚至更好,告诉我为什么?

谢谢!

vb.net pinvoke interop dllimport c#-to-vb.net

3
推荐指数
1
解决办法
7802
查看次数

C#到VB.Net转换问题

将以下行从C#转换为VB.Net时我得到了

表达式不会产生值

C#

query.ToList().ForEach(ti => cat.Add(ti));
Run Code Online (Sandbox Code Playgroud)

VB.NET

query.ToList().ForEach(Function(ti) cat.Add(ti))
Run Code Online (Sandbox Code Playgroud)

C#代码:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    new DesignerMetadata().Register();

    var toolbox = new ToolboxControl();
    var cat = new ToolboxCategory("Standard Activities");
    var assemblies = new List<Assembly>();
    assemblies.Add(typeof(Send).Assembly);
    assemblies.Add(typeof(Delay).Assembly);
    assemblies.Add(typeof(ReceiveAndSendReplyFactory).Assembly);

    var query = from asm in assemblies
                from type in asm.GetTypes()
                where type.IsPublic &&
                !type.IsNested &&
                !type.IsAbstract &&
                !type.ContainsGenericParameters &&
                (typeof(Activity).IsAssignableFrom(type) ||
                typeof(IActivityTemplateFactory).IsAssignableFrom(type))
                orderby type.Name
                select new ToolboxItemWrapper(type);

    query.ToList().ForEach(ti => cat.Add(ti));
    toolbox.Categories.Add(cat);
    Grid.SetColumn(toolbox, 0);
    Grid.SetRow(toolbox, 1);
    LayoutGrid.Children.Add(toolbox);
}
Run Code Online (Sandbox Code Playgroud)

我想要Vb.net转换.当我在vb.net中转换此代码时在query.ToList()中获取错误.ForEach(Function(ti)cat.Add(ti))此行.error是Expression不生成值.

转换VB.NET代码

  Private Sub MainWindow_Loaded(ByVal …
Run Code Online (Sandbox Code Playgroud)

c# vb.net visual-studio-2010 c#-to-vb.net

3
推荐指数
1
解决办法
454
查看次数

C#转VB问题

嗨,我正在尝试将一些代码从C#转换为VB,但由于我缺乏对C#(或者VB)的了解,我遇到了一些问题.我希望有更多技能的人可以帮我.

我使用的转换工具在这里: http://www.developerfusion.com/tools/convert/csharp-to-vb/ 将一些代码在这里找到: http://www.urmanet.ch/?p=11

该代码旨在制作EF实体的深层副本.

在第87行更正&&后,转换完成正常.稍后进行了一些导入并修复了一些简单的错误,我无法解决的其余错误如下:

1:这条线

<System.Runtime.CompilerServices.Extension()> _
    Public Shared Function Clone(entityObject As EntityObject) As EntityObject
Run Code Online (Sandbox Code Playgroud)

标记为扩展方法只能在模块中定义.它在一个类中定义,但我认为这在C#中工作,那么为什么我会在VB中得到这个错误?

2:有很多这样的行:

For Each [property] As var In properties
Run Code Online (Sandbox Code Playgroud)

其中'as var'部分被标记为未定义; 我不完全确定'as var'意味着什么,我可以删除它并允许编译器推断出类型吗?

3:这行代码中有错误:

Dim t = GetType(EntityCollection(Of )).MakeGenericType(New () {[property].PropertyType.GetGenericArguments()(0)})
Run Code Online (Sandbox Code Playgroud)

在'''之后有一个波浪形,表示预期类型,我不知道该怎么做这条线,因此如何修复它,有人能够澄清一下吗?

4:这条线

Private Shared ReadOnly _tracking As New List()
Run Code Online (Sandbox Code Playgroud)

告诉我System.Collections.Generic.List(Of T)的类型参数太少.这是因为C#和VB之间存在一些差异吗?

非常感谢任何建议.

c# vb.net c#-to-vb.net

2
推荐指数
1
解决办法
2904
查看次数

在 VB.NET 与 C# 中测试对象是否为 Nothing/null

我正在将一些 C# 代码转换为 VB.NET。我有一个简单的类似字典的数据结构,其中包含名称/值对。value 元素的类型为 Object。我的 C# 代码看起来像这样

if(x.Value != null)
  // 1: Store x.Value in database
else
  // Sore DBNULL.Value in database
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,如果 x.Value 恰好是值为 false 的布尔值,则执行上面的代码块 1。

但是,等效的 VB.NET 代码将落入 else 块中的布尔值为 False

If x.Value Is Not Nothing Then
  ' Store x.Value in database
Else
  ' We land here if x.Value is a Boolean with a value of False and incorrectly store DBNULL.Value in database
EndIF
Run Code Online (Sandbox Code Playgroud)

VB 显然认为带有 False 值的布尔值等同于 Nothing。我会保留我对 VB 的评论,但是有没有一种简单的方法(即不使用反射)来解决这个问题?

编辑:我原来的 VB 代码实际上是

If x.Value …
Run Code Online (Sandbox Code Playgroud)

c#-to-vb.net

2
推荐指数
1
解决办法
4万
查看次数

VB.NET必须实现错误

我只是将C#函数转换为VB.NET函数,但不知怎的,我得到了一些错误.下面是我的C#函数.

public abstract class BaseFilterControl: UserControl,IFilterControl
{
    public string PropertyName { get; set; }

    public FilterDescriptorBase AssociatedDescriptor { get; set; }

    public bool IsFirst { get; set; }

    public abstract FilterDescriptorBase BuildDescriptor();

    protected abstract void Initialize();
}
Run Code Online (Sandbox Code Playgroud)

下面是我目前的VB功能

Public MustInherit Class BaseFilterControl
    Inherits UserControl
    Implements IFilterControl

    Public Property PropertyName As String

    Public Property AssociatedDescriptor As FilterDescriptorBase

    Public Property IsFirst As Boolean

    Public MustOverride Function BuildDescriptor() As FilterDescriptorBase

    Protected MustOverride Sub Initialize()
End Class
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

BaseFilterControl must implement Function BuildDescriptor() As …
Run Code Online (Sandbox Code Playgroud)

c# vb.net c#-to-vb.net

2
推荐指数
1
解决办法
1324
查看次数

将 C# PageAsyncTask() 转换为 VB.Net 等效项时遇到问题

尝试将 C# 函数转换为 VB.Net 等效函数时出现编译错误。C# 中的 PageAsyncTask 需要一个 Task 输入,但在 VB.Net 中它正在寻找 Func(Of Task)。我能找到的在线转换器都没有正确翻译语言。错误是:“Task”类型的值不能转换为“Func(Of Task)”

不知道如何继续(我猜我需要定义一个事件?)。这是原始的 C# 代码

        protected void Page_Load(object sender, EventArgs e)
    {
        {
            AsyncMode = true;
            if (!dictionary.ContainsKey("accessToken"))
            {
                if (Request.QueryString.Count > 0)
                {
                    var response = new AuthorizeResponse(Request.QueryString.ToString());
                    if (response.State != null)
                    {
                        if (oauthClient.CSRFToken == response.State)
                        {
                            if (response.RealmId != null)
                            {
                                if (!dictionary.ContainsKey("realmId"))
                                {
                                    dictionary.Add("realmId", response.RealmId);
                                }
                            }

                            if (response.Code != null)
                            {
                                authCode = response.Code;
                                output("Authorization code obtained.");
                                PageAsyncTask t = new PageAsyncTask(performCodeExchange); …
Run Code Online (Sandbox Code Playgroud)

c# vb.net delegates c#-to-vb.net async-await

2
推荐指数
1
解决办法
645
查看次数