Vb.net 使用方法更改对象参照值

Vb.net 使用方法更改对象参照值,vb.net,pdfsharp,Vb.net,Pdfsharp,这是一个PDFSharp问题,但我认为它更符合vb.net中对象引用的工作方式 我有这样的代码: Dim page as PdfPage = pdf1.AddPage() Dim graphics as XGraphics = XGraphics.FromPdfPage(pdfPage) 因此,这自然会创建我需要的graphics XGraphics和PdfPage对象 现在,当我需要向正在创建的PDF文档中添加页面时,我调用以下代码: page = pdf1.AddPage() graphi

这是一个PDFSharp问题,但我认为它更符合vb.net中对象引用的工作方式

我有这样的代码:

Dim page as PdfPage = pdf1.AddPage()
Dim graphics as XGraphics = XGraphics.FromPdfPage(pdfPage)
因此,这自然会创建我需要的graphics XGraphics和PdfPage对象

现在,当我需要向正在创建的PDF文档中添加页面时,我调用以下代码:

page = pdf1.AddPage()
graphics = XGraphics.FromPdfPage(pdfPage)
当然,我也会做其他的事情,比如添加XTextFormatters,定义我的起始X,Y坐标,等等。但是,作为一个懒惰的人,我试着编写一个方法,允许我传入我想要的所有变量,并为我自动更新所有这些变量。简言之,比如:

Private Sub ConductPrinting()
    Dim pdf1 as New Pdf
    Dim page as PdfPage = pdf1.AddPage()
    Dim graphics as XGraphics = XGraphics.FromPdfPage(pdfPage)

    'Printing code here
    'Printing code here
    'Printing code here

    'Start new page code
    pdfPage = pdf1.AddPage()
    graphics = XGraphics.FromPdfPage(pdfPage)
End Sub
Private Sub StartNewPage(pdf1 As Pdf, page As PdfPage, graphics As XGraphics) As Integer
    pdfPage = pdf1.AddPage()
    graphics = XGraphics.FromPdfPage(pdfPage)
End Sub
我想编写一个
StartNewPage()
方法,类似于:

Private Sub ConductPrinting()
    Dim pdf1 as New Pdf
    Dim page as PdfPage = pdf1.AddPage()
    Dim graphics as XGraphics = XGraphics.FromPdfPage(pdfPage)

    'Printing code here
    'Printing code here
    'Printing code here

    'Start new page code
    pdfPage = pdf1.AddPage()
    graphics = XGraphics.FromPdfPage(pdfPage)
End Sub
Private Sub StartNewPage(pdf1 As Pdf, page As PdfPage, graphics As XGraphics) As Integer
    pdfPage = pdf1.AddPage()
    graphics = XGraphics.FromPdfPage(pdfPage)
End Sub
因此,代码块可以如下所示:

Private Sub ConductPrinting()
    Dim pdf1 as New Pdf
    Dim page as PdfPage = pdf1.AddPage()
    Dim graphics as XGraphics = XGraphics.FromPdfPage(pdfPage)

    'Printing code here
    'Printing code here
    'Printing code here

    'Start new page code
    StartNewPage(pdf1, page, graphics)
End Sub

你打算怎么做?因为这不起作用;页面和图形仍在引用旧页面和图形。

您需要通过引用而不是通过值传递要更改的对象,因此

Private Sub StartNewPage(pdf1 As Pdf, ByRef page As PdfPage, ByRef graphics As XGraphics)
发件人:

何时通过引用传递参数
如果过程确实需要更改调用代码中的底层元素,请声明相应的参数ByRef


我认为您的
页面
pdfPage
变量/类型有点混淆。如果它们以我怀疑的方式混合在一起,那么您需要
专用子开始newpage(pdf1为Pdf,ByRef page为PdfPage,ByRef graphics为XGraphics)
-请参阅。你在使用吗?ByRef关键字工作得很好。我全忘了。将其添加为答案,我会将其标记为正确:)