Solved

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

Posted on 2014-11-13
7
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best book for Internet security 4 52
Building an object from several other objects 4 39
How do I "share" on social sites? 2 38
Need Help With GDI+ in VB.Net 8 40
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

734 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