c#-to-vb.net

Split a List into parts - VB conversion fails

好久不见. 提交于 2019-12-23 12:35:43
问题 Trying to write a method that splits a list into sublists. 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 I used C# solution from here. C# solution works fine. My version written in VB returns a collection of lists with one element instead

How do I write [DefaultValue(null)] in VB.NET? <DefaultValue(Nothing)> does not compile

为君一笑 提交于 2019-12-23 10:17:06
问题 This isn't as trivial as it appears to be. It's a follow-up to this question. Let's say I have a Windows Forms user control with a property: // using System.ComponentModel; [DefaultValue(null)] public object DataSource { … } If I translate this to VB.NET, I would try this: 'Imports System.ComponentModel <DefaultValue(Nothing)> Public Property DataSource As Object … End Property This won't work because the compiler has problems choosing the correct overload of DefaultValueAttribute's

Constructing an object and calling a method without assignment in VB.Net

做~自己de王妃 提交于 2019-12-22 07:57:13
问题 I'm not entirely sure what to call what C# does, so I haven't had any luck searching for the VB.Net equivalent syntax (if it exists, which I suspect it probably doesn't). In c#, you can do this: public void DoSomething() { new MyHelper().DoIt(); // works just fine } But as far as I can tell, in VB.Net, you must assign the helper object to a local variable or you just get a syntax error: Public Sub DoSomething() New MyHelper().DoIt() ' won't compile End Sub Just one of those curiosity things I

Adding items to the List at creation time in VB.Net

你说的曾经没有我的故事 提交于 2019-12-22 02:34:06
问题 In c# I can initialize a List at creation time like var list = new List<String>() {"string1", "string2"}; is there a similar thing in VB.Net? Currently I can do it like Dim list As New List(Of String) list.Add("string1") list.Add("string2") list.Add("string3") but I want to avoid boring .Add lines 回答1: VB10 supports collection initializers. I believe your example would be: Dim list As New List(Of String) From { "string1", "string2", "string3" } MSDN has more information. 回答2: You can also use

How do I Convert from Anonymous Event Handlers in C# to VB.Net

 ̄綄美尐妖づ 提交于 2019-12-20 07:14:38
问题 I have the below example code for a Windows Phone 7 application, and I am trying to convert it to VB.Net as a starting point. The assignments like this: Loaded += (_, __) => { anonymousMethodBody();} are failing to convert when I use a C#-to-VB conversion tool. How should those be translated? public MainPage() { InitializeComponent(); Loaded += (_, __) => { PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled; cam = new VideoCamera(); cam.Initialized += (_

C# to VB: Class.Event += (sender, args) => FunctionName(params)

那年仲夏 提交于 2019-12-20 02:58:14
问题 I'm trying to convert the C# code from this webpage to VB. Everything seems to have converted pretty much fine using an online converter tool, but then I reach the following line: fadeOutAnimation.Completed += (sender, args) => OnFadeOutAnimationCompleted(d, hostGrid, grid); The fadeOutAnimation.Completed event produces an event with the signature (sender, args), and d, hostGrid and grid are variables local to the function containing this mysterious event handler assignment. I think I can see

Can VB.NET be forced to initialize instance variables BEFORE invoking the base type constructor?

跟風遠走 提交于 2019-12-17 15:54:33
问题 After debugging a particularly tricky issue in VB.NET involving the order in which instance variables are initialized, I discovered that there is a breaking discrepancy between the behavior that I expected from C# and the actual behavior in VB.NET. Nota bene: This question concerns a slight discrepancy in the behaviors of VB.NET and C#. If you're a language bigot that is unable to provide an answer other than "that's why you should use C#, noob" , there is nothing for you to see here; kindly

How to Convert This C# Event/Delegate Code Into VB.NET (2008 or 2010)

拟墨画扇 提交于 2019-12-13 07:58:33
问题 C# to VB.NET Here is the relevant C# Code namespace MyApp { public delegate bool AllocHandlerDelegate(int param1); public interface ILoader { event AllocHandlerDelegate evAlloc; bool Load(); } public class MyLoader : ILoader { public event AllocHandlerDelegate evAlloc; public bool Load() { try { if (this.evAlloc != null && !evAlloc(1)) return false; } } } Here is what I came up with so far. The C# delegate is a function which returns a boolean result. So I converted the delegate definition

C# internal VS VBNET Friend

旧时模样 提交于 2019-12-12 10:29:46
问题 To this SO question: What is the C# equivalent of friend?, I would personally have answered "internal", just like Ja did among the answers! However, Jon Skeet says that there is no direct equivalence of VB Friend in C#. If Jon Skeet says so, I won't be the one telling otherwise! ;P I'm wondering how can the keyword internal (C#) not be the equivalent of Friend (VBNET) when their respective definitions are: Friend VBNET The Friend (Visual Basic) keyword in the declaration statement specifies

Syntax error in `New SomeClass { Key .SomeProperty = SomeValue }` after C# -> VB conversion

筅森魡賤 提交于 2019-12-11 11:16:27
问题 A co-worker of mine and I both do programming. He has made a class in C# and I am working on converting it to VB.NET. I got the full class converted except for a single line, and at this point I cannot figure it out so thought a fresh set of eyes maybe able to find my error. Original C# code using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })) Converted VB.NET code Using client = New HttpClient(New