Solved

Please tell me how to convert this C# to VB.NET

Posted on 2014-11-13
7
205 Views
Last Modified: 2014-11-17
        private void button1_Click(object sender, EventArgs e) {
            const string SERVER_URL = "My.Settings.ReportServerURL";
            const string REPORT_PATH = "My.Settings.ReportPath";

            var p = new ReportParameter("name", "val");
            var rv = new ReportViewer() {
                AutoSize = true,
                ProcessingMode = ProcessingMode.Remote
            };
            rv.ServerReport.ReportServerUrl = new Uri(SERVER_URL);
            rv.ServerReport.ReportPath = REPORT_PATH;
            rv.ServerReport.SetParameters(p);
            rv.RefreshReport();
            rv.RenderingComplete += (sender2, eventArgs) => rv.PrintDialog(new PrinterSettings() {
                PrintRange = PrintRange.SomePages,
                FromPage = 1,
                ToPage = 2,
                PrinterName = "MYPRINTER"
            });
        }

Open in new window


all is fine except i cannot figure out the syntax in VB.Net of this line

rv.RenderingComplete += (sender2, eventArgs) => rv.PrintDialog(new PrinterSettings() {
                PrintRange = PrintRange.SomePages,
                FromPage = 1,
                ToPage = 2,
                PrinterName = "MYPRINTER"
            }

Open in new window

0
Comment
Question by:FamousMortimer
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 50 total points
ID: 40440434
The => is a lambda expression:
http://msdn.microsoft.com/en-us/library/bb311046.aspx

The VB.Net equivalent is here:
http://msdn.microsoft.com/en-us/library/bb531253.aspx
where you have to write that one line as a function
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40440571
You need to do it in this way:
	Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
		Const SERVER_URL As String = "My.Settings.ReportServerURL"
		Const REPORT_PATH As String = "My.Settings.ReportPath"

		Dim p = New ReportParameter("name", "val")
		Dim rv = New ReportViewer() With {.AutoSize = True, .ProcessingMode = ProcessingMode.Remote}
		rv.ServerReport.ReportServerUrl = New Uri(SERVER_URL)
		rv.ServerReport.ReportPath = REPORT_PATH
		rv.ServerReport.SetParameters(p)
		rv.RefreshReport()
		AddHandler rv.RenderingComplete, AddressOf OnRenderingComplete
	End Sub

	Private Sub OnRenderingComplete(ByVal sender As Object, ByVal e As RenderingCompleteEventArgs)
		Dim rv As ReportViewer = TryCast(sender, ReportViewer)
		If Not rv Is Nothing Then
			rv.PrintDialog(New PrinterSettings() With { .PrintRange = PrintRange.SomePages, .FromPage = 1, .ToPage = 2, .PrinterName = "MYPRINTER" }
		End If
	End Sub

Open in new window


VB.NET, currently does not allow Lambda based sub event handlers.

http://10rem.net/blog/2010/04/16/tip-anonymous-event-handlers-in-vbnet

-saige-
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 40440676
@it_saige

Your article reference is 4 years old. Lambdas work just fine in VS 2013:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Me.Button1.Click, Function(snd, evt) MessageBox.Show("Click!")
    End Sub
End Class

Open in new window


Screenshot
P.S.

Sub works as well  : )
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40440723
Aha.  Thanks Kaufmed.  I just tried and confirmed in VS2010.  So I can say that atleast since .NET 3.5, this has been resolved.  Modifed code example above:
	Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
		Const SERVER_URL As String = "My.Settings.ReportServerURL"
		Const REPORT_PATH As String = "My.Settings.ReportPath"

		Dim p = New ReportParameter("name", "val")
		Dim rv = New ReportViewer() With {.AutoSize = True, .ProcessingMode = ProcessingMode.Remote}
		rv.ServerReport.ReportServerUrl = New Uri(SERVER_URL)
		rv.ServerReport.ReportPath = REPORT_PATH
		rv.ServerReport.SetParameters(p)
		rv.RefreshReport()
		AddHandler rv.RenderingComplete, Sub(sender2 As Object, e2 As RenderingCompleteEventArgs)
									   Dim rv2 As ReportViewer = TryCast(sender, ReportViewer)
									   If Not rv2 Is Nothing Then
										   rv2.PrintDialog(New PrinterSettings() With {.PrintRange = PrintRange.SomePages, .FromPage = 1, .ToPage = 2, .PrinterName = "MYPRINTER"})
									   End If
								   End Sub
	End Sub

Open in new window


-saige-
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40440924
ps.  My link to lambda support in VB states it is supported from VS2008 onwards
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40440942
As I recall, multi-line lambdas weren't introduced in VB until VS 2010/2012.  I'm getting older, though, so I could be off my rocker  ; )
0
 
LVL 10

Author Closing Comment

by:FamousMortimer
ID: 40447944
Thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question