What is the VB.NET equivalent of this code?
-
Not sure if this is best forum but... I am trying to convert some old C# code into a VB.NET version and in the process add some more functionality. Silly me... I don't know any C# at all but having an educated guess. This following bit has me stumped. Can anybody help with the conversion or give an indication of what his code is doing?
protected void onMenuItemClick(Object sender, EventArgs e) { MenuItem item = (MenuItem)sender; if (MyItemClicked != null) { string fileName = (string)_itemMap[item]; MyItemClicked(this, new MyItemClickedEventArgs(fileName)); } }
Cheers Tim -
Not sure if this is best forum but... I am trying to convert some old C# code into a VB.NET version and in the process add some more functionality. Silly me... I don't know any C# at all but having an educated guess. This following bit has me stumped. Can anybody help with the conversion or give an indication of what his code is doing?
protected void onMenuItemClick(Object sender, EventArgs e) { MenuItem item = (MenuItem)sender; if (MyItemClicked != null) { string fileName = (string)_itemMap[item]; MyItemClicked(this, new MyItemClickedEventArgs(fileName)); } }
Cheers TimHi I'm not much familiar with vb, but the equivalent for this code in vb should be something like this: Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem.Click Dim item As MenuItem If Not MyItemClicked Is Nothing Then Dim filename As String filename = _itemMap(item).ToString() MyItemClicked(Me, New MyItemClickedEventArgs(filename)) End If End Sub if you need, I can explain what this method does, and then you can write the correct code yourself. HTH farshad
-
Not sure if this is best forum but... I am trying to convert some old C# code into a VB.NET version and in the process add some more functionality. Silly me... I don't know any C# at all but having an educated guess. This following bit has me stumped. Can anybody help with the conversion or give an indication of what his code is doing?
protected void onMenuItemClick(Object sender, EventArgs e) { MenuItem item = (MenuItem)sender; if (MyItemClicked != null) { string fileName = (string)_itemMap[item]; MyItemClicked(this, new MyItemClickedEventArgs(fileName)); } }
Cheers Tim -
Not sure if this is best forum but... I am trying to convert some old C# code into a VB.NET version and in the process add some more functionality. Silly me... I don't know any C# at all but having an educated guess. This following bit has me stumped. Can anybody help with the conversion or give an indication of what his code is doing?
protected void onMenuItemClick(Object sender, EventArgs e) { MenuItem item = (MenuItem)sender; if (MyItemClicked != null) { string fileName = (string)_itemMap[item]; MyItemClicked(this, new MyItemClickedEventArgs(fileName)); } }
Cheers TimThe first reply was close...but no cigar. VB equivalents regarding events can be tricky. You'll need (especially note the hidden "MyItemClickedEvent" variable): Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem.Click Dim item As MenuItem If Not MyItemClickedEvent Is Nothing Then Dim filename As String filename = _itemMap(item).ToString() RaiseEvent MyItemClicked(Me, New MyItemClickedEventArgs(filename)) End If End Sub
David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Clear VB: Cleans up VB.NET code C# Code Metrics: Quick metrics for C#
-
The first reply was close...but no cigar. VB equivalents regarding events can be tricky. You'll need (especially note the hidden "MyItemClickedEvent" variable): Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem.Click Dim item As MenuItem If Not MyItemClickedEvent Is Nothing Then Dim filename As String filename = _itemMap(item).ToString() RaiseEvent MyItemClicked(Me, New MyItemClickedEventArgs(filename)) End If End Sub
David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Clear VB: Cleans up VB.NET code C# Code Metrics: Quick metrics for C#
-
The first reply was close...but no cigar. VB equivalents regarding events can be tricky. You'll need (especially note the hidden "MyItemClickedEvent" variable): Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem.Click Dim item As MenuItem If Not MyItemClickedEvent Is Nothing Then Dim filename As String filename = _itemMap(item).ToString() RaiseEvent MyItemClicked(Me, New MyItemClickedEventArgs(filename)) End If End Sub
David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Clear VB: Cleans up VB.NET code C# Code Metrics: Quick metrics for C#
David, On further examination of your response, does the following (second line of original code) assign a value to item?
MenuItem item = (MenuItem)sender;
If so, this assignment is missing from your response. After the = is what I am struggling with. Cheers Tim -
David, On further examination of your response, does the following (second line of original code) assign a value to item?
MenuItem item = (MenuItem)sender;
If so, this assignment is missing from your response. After the = is what I am struggling with. Cheers TimD'oh! I started with Farshad's response and I just modified it. You should definitely have: Dim item As MenuItem = CType(sender, MenuItem)
David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Clear VB: Cleans up VB.NET code C# Code Metrics: Quick metrics for C#
-
D'oh! I started with Farshad's response and I just modified it. You should definitely have: Dim item As MenuItem = CType(sender, MenuItem)
David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter, VB to C++ converter Clear VB: Cleans up VB.NET code C# Code Metrics: Quick metrics for C#