如何修改现有的VB.NET类以传递其他变量

时间:2018-10-25 03:59:38

标签: c# vb.net class oop

我的OOP技能不是很强,我很难理解如何修改现有的类以传递其他变量。

一点点背景。

我试图使用VB.net打印RDLC报告。我找到了一个可工作的C#类,并将其修改为VB.Net。目前,它工作正常。该类具有分配页面高度,宽度和边距值的方法。我已对其进行硬编码以满足我的当前需求。但我不想对其进行硬编码,我想将高度,宽度和边距值传递给该类。

原始课程

Click To See

我的修改已硬编码到班级

Return String.Format(CultureInfo.InvariantCulture, "<DeviceInfo><OutputFormat>emf</OutputFormat>
                                                            <StartPage>0</StartPage>
                                                            <EndPage>0</EndPage>
                                                            <MarginTop>{0}</MarginTop>
                                                            <MarginLeft>{1}</MarginLeft>
                                                            <MarginRight>{2}</MarginRight>
                                                            <MarginBottom>{3}</MarginBottom>
                                                            <PageHeight>{4}</PageHeight>
                                                            <PageWidth>{5}</PageWidth>
                                                            </DeviceInfo>", ToInches(10),
                                                            ToInches(5), ToInches(5),
                                                            ToInches(10), ToInches(600),
                                                            ToInches(900))

我当前如何通过按钮单击事件进行调用

Dim autoprintme As AutoPrintCls = New AutoPrintCls(ReportViewer1.LocalReport)
autoprintme.Print()

我怎么称呼

Dim hight As Double = 585 
Dim width As Double = 827 
Dim autoprintme As AutoPrintCls = New AutoPrintCls(ReportViewer1.LocalReport, hight, width)
autoprintme.Print()

2 个答案:

答案 0 :(得分:0)

您要将高度和宽度作为参数传递给CreateEMFDeviceInfo方法。它应该在字符串中使用这些参数。所以..

 Private Sub RenderAllLocalReportPages(ByVal localReport As LocalReport)
        Try
            Dim deviceInfo As String = CreateEMFDeviceInfo()
            Dim warnings As Warning()
            localReport.Render("IMAGE", deviceInfo, AddressOf LocalReportCreateStreamCallback, warnings)
        Catch e As Exception
            MessageBox.Show("error :: " & e.Message)
        End Try
    End Sub

    Private Function LocalReportCreateStreamCallback(ByVal name As String, ByVal extension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
        Dim stream As MemoryStream = New MemoryStream()
        m_pages.Add(stream)
        Return stream
    End Function

    Private Function CreateEMFDeviceInfo() As String
        Dim paperSize As PaperSize = m_pageSettings.PaperSize
        Dim margins As Margins = m_pageSettings.Margins
        Return String.Format(CultureInfo.InvariantCulture, "<DeviceInfo><OutputFormat>emf</OutputFormat>
                                                            <StartPage>0</StartPage>
                                                            <EndPage>0</EndPage>
                                                            <MarginTop>{0}</MarginTop>
                                                            <MarginLeft>{1}</MarginLeft>
                                                            <MarginRight>{2}</MarginRight>
                                                            <MarginBottom>{3}</MarginBottom>
                                                            <PageHeight>{4}</PageHeight>
                                                            <PageWidth>{5}</PageWidth>
                                                            </DeviceInfo>", ToInches(margins.Top),
                                                            ToInches(margins.Left), ToInches(margins.Right),
                                                            ToInches(margins.Bottom), ToInches(paperSize.Height),
                                                            ToInches(paperSize.Width))


    End Function

成为

     Private Sub RenderAllLocalReportPages(ByVal localReport As LocalReport)
    Try
        Dim pageHeight, pageWidth as Integer
        'Set values
        '....

        Dim deviceInfo As String = CreateEMFDeviceInfo(pageHeight,pageWidth)
        Dim warnings As Warning()
        localReport.Render("IMAGE", deviceInfo, AddressOf LocalReportCreateStreamCallback, warnings)
    Catch e As Exception
        MessageBox.Show("error :: " & e.Message)
    End Try
End Sub

Private Function LocalReportCreateStreamCallback(ByVal name As String, ByVal extension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
    Dim stream As MemoryStream = New MemoryStream()
    m_pages.Add(stream)
    Return stream
End Function

Private Function CreateEMFDeviceInfo(PageHeight As Integer, PageWidth as Integer) As String
    Dim paperSize As PaperSize = m_pageSettings.PaperSize
    Dim margins As Margins = m_pageSettings.Margins
    Return String.Format(CultureInfo.InvariantCulture, "<DeviceInfo><OutputFormat>emf</OutputFormat>
                                                        <StartPage>0</StartPage>
                                                        <EndPage>0</EndPage>
                                                        <MarginTop>{0}</MarginTop>
                                                        <MarginLeft>{1}</MarginLeft>
                                                        <MarginRight>{2}</MarginRight>
                                                        <MarginBottom>{3}</MarginBottom>
                                                        <PageHeight>" & PageHeight & "</PageHeight>
                                                        <PageWidth>" & PageWidth & "</PageWidth>
                                                        </DeviceInfo>", ToInches(margins.Top),
                                                        ToInches(margins.Left), ToInches(margins.Right),
                                                        ToInches(margins.Bottom), ToInches(paperSize.Height),
                                                        ToInches(paperSize.Width))


End Function

答案 1 :(得分:0)

您想将其他变量传递给此类而不会破坏已经存在的内容吗?没问题,我的朋友:我们将创建一个新的重载,该重载将称为原始重载,但会有所不同!

这是我读到的原始的New()Sub:

    Public Sub New(ByVal report As Report)
        Dim reportPageSettings As ReportPageSettings = report.GetDefaultPageSettings()
        m_pageSettings = New PageSettings()
        m_pageSettings.PaperSize = reportPageSettings.PaperSize
        m_pageSettings.Margins = reportPageSettings.Margins
    End Sub

现在,您可以通过这种方式添加重载(我包含了原始的原始代码,以至于很明显您不必修改它!):

    Public Sub New(ByVal report As Report, ByVal height as Double, ByVal width as Double)
        Initialize(report)
        Me.height = height
        Me.width = width
    End Sub

    Private Sub Initialize(ByVal report as Report)
        Dim reportPageSettings As ReportPageSettings = report.GetDefaultPageSettings()
        m_pageSettings = New PageSettings()
        m_pageSettings.PaperSize = reportPageSettings.PaperSize
        m_pageSettings.Margins = reportPageSettings.Margins
        RenderAllLocalReportPages(localReport)
    End Sub

现在,很抱歉,因为太晚了,我在这里确实要承担一些事情:我假设无论该类是什么,它都有width属性和height属性。如果不是这种情况,则必须设计一种方法,以有意义的方式对其进行修改。

您似乎有一定的编码背景,所以我有信心您会明白我的意思,也祝您好运! (如果您仍有问题,请随时询问“ em”)