c#-to-vb.net

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

我只是一个虾纸丫 提交于 2019-12-05 17:40:18
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 run into from day to day working on mixed language projects - often there is a VB.Net equivalent which

Protected Set in VB.Net for a property defined in an interface

这一生的挚爱 提交于 2019-12-05 09:18:56
We have an interface, which can be grossly simplified as: public interface IPersistable<T> { T Id { get; } } Most places that implement the interface want to have it so that there is a protected or private set on that property, i.e, in C#: public class Foo : IPersistable<int> { public int Id { get; protected set; } } However, I can't get any sample VB.Net code to compile that follows the same pattern, whilst still implementing the interface, so: Public Class Foo Implements IPersistable(Of Integer) Public Property Id() As Integer Implements IPersistable(Of Integer).Id Get Throw New

CInt does not round Double value consistently - how can I remove the fractional part?

做~自己de王妃 提交于 2019-12-05 02:31:53
I've stumbled upon an issue with CInt and converting a double to an integer. The issue is the following: CInt(10.5) 'Result is 10 CInt(10.51) 'Result it 11, but I expected 10... I got used to C# style conversion where (int) 10.51 is 10. As pointed out in the question about Integer.Parse vs CInt , the result is just rounded in some fashion. However, all I need is to get only integer part and throw away the fractional one. How can I achieve such type of conversion in VB.NET? After some research I see that I can use the Fix() function to do the trick, but is it the best choice? You may use Int or

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

孤人 提交于 2019-12-05 00:34:07
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 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 . You can also use AddRange if you don't want to put all of your items on a single line. Dim list As New List(Of String) From {

Multiassignment in VB like in C-Style languages

巧了我就是萌 提交于 2019-12-05 00:19:54
Is there a way to perform this in VB.NET like in the C-Style languages: struct Thickness { double _Left; double _Right; double _Top; double _Bottom; public Thickness(double uniformLength) { this._Left = this._Right = this._Top = this._Bottom = uniformLength; } } Expanding on Mark's correct answer This type of assignment style is not possible in VB.Net. The C# version of the code works because in C# assignment is an expression which produces a value. This is why it can be chained in this manner. In VB.Net assignment is a statement and not an expression. It produces no value and cannot be

Argument not specified for parameter

点点圈 提交于 2019-12-04 05:10:58
问题 Using VB.net & WPF I've converted code available at Overlaying Controls in WPF with Adorners from C# to VB.Net Original C# Code /// <summary> /// Overlays a control with the specified content /// </summary> /// <typeparam name="TOverlay">The type of content to create the overlay from</typeparam> public class OverlayAdorner<TOverlay> : Adorner, IDisposable where TOverlay : UIElement, new() { private UIElement _adorningElement; private AdornerLayer _layer; /// <summary> /// Overlay the

Argument not specified for parameter

跟風遠走 提交于 2019-12-02 03:11:33
Using VB.net & WPF I've converted code available at Overlaying Controls in WPF with Adorners from C# to VB.Net Original C# Code /// <summary> /// Overlays a control with the specified content /// </summary> /// <typeparam name="TOverlay">The type of content to create the overlay from</typeparam> public class OverlayAdorner<TOverlay> : Adorner, IDisposable where TOverlay : UIElement, new() { private UIElement _adorningElement; private AdornerLayer _layer; /// <summary> /// Overlay the specified element /// </summary> /// <param name="elementToAdorn">The element to overlay</param> /// <returns><

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

佐手、 提交于 2019-12-01 23:11: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 that the instruction on this C# line is telling the code to execute the OnFadeOutAnimationCompleted

What would be the equivalent VB.NET code for this C# FluentNHibernate component mapping?

匆匆过客 提交于 2019-12-01 20:33:41
问题 I'm a C# programmer constrained to write VB.NET code. While exploring NHibernate further for my current client, I encountered FluentNHibernate, which I find real attractive. But now, I wonder how to "translate" this C# code for component mapping into VB.NET code: Component(x => x.Address, m => { m.Map(x => x.Number); m.Map(x => x.Street); m.Map(x => x.PostCode); }); I know from here: Component(Of Client)(Function(c) c.Address, ...) what I miss is how to continue with the brackets in VB.NET,

What would be the equivalent VB.NET code for this C# FluentNHibernate component mapping?

ⅰ亾dé卋堺 提交于 2019-12-01 18:40:42
I'm a C# programmer constrained to write VB.NET code. While exploring NHibernate further for my current client, I encountered FluentNHibernate, which I find real attractive. But now, I wonder how to "translate" this C# code for component mapping into VB.NET code: Component(x => x.Address, m => { m.Map(x => x.Number); m.Map(x => x.Street); m.Map(x => x.PostCode); }); I know from here: Component(Of Client)(Function(c) c.Address, ...) what I miss is how to continue with the brackets in VB.NET, since there's no Begin End keywords or so. EDIT 1: Following Mr. JaredPar instructions, I figured that