Creating a method in ASP.NET

Bonekrusher

Active member
Joined
Jul 4, 2007
Messages
38
Programming Experience
1-3
I am trying to create a method to allow transforming of XML. The following code gets hung up on :

docXsl.Transform(docXml, Nothing, Response.Output)

I get the following error:

"Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."

How do I fix this?

VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page


    Public Shared Sub mytransform(ByVal myxml As String, ByVal myxslt As String)
        Dim docXml As New System.Xml.XmlDocument()
        docXml.Load(myxml)
        Dim docXsl As New System.Xml.Xsl.XslCompiledTransform()
        docXsl.Load(myxslt)
        Dim myoutput As String = "c:\temp\mytest.html"
        docXsl.Transform(docXml, Nothing, Response.Output)
    End Sub


End Class
 
Shared methods are not called from a class instance. For example this is wrong:
Dim x As New _Default
x.mytransform(...)

This is correct:
_Default.mytransform(...)
So the shared method know nothing about any instance in the class and can't make any references to such.
Are you sure you want that method to be Shared? I'm asking since you seem to want to use the Response.Output instance of that same class. Shared is commonly used for utility methods and placed in external classes that you use in several projects, which this method could be. If you still want to create a Shared method you must provide any external instances used as parameters, example:
VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page

    Public Shared Sub mytransform(ByVal myxml As String, ByVal myxslt As String, ByVal writer As IO.TextWriter)
        Dim docXml As New System.Xml.XmlDocument()
        docXml.Load(myxml)
        Dim docXsl As New System.Xml.Xsl.XslCompiledTransform()
        docXsl.Load(myxslt)
        docXsl.Transform(docXml, Nothing, writer)
    End Sub

End Class
Using this method from an instance of this class is a plain method call where you now also pass Response.Output to the 'writer' parameter, if calling this method from a different class you do like the "correct" example above.
 
Thank you for the help.

You are correct, this is probably not the best method for the desired output. Nevertheless, you helped explain why I was having the problem. Most likely I will do the transformation locally or use the built in ASP.NET controls.

thanks a bunch... The below is the complete code for who ever is interested:

VB.NET:
Public Shared Sub mytransform(ByVal myxml As String, ByVal myxslt As String, ByVal writer As IO.TextWriter)
        Dim docXml As New System.Xml.XmlDocument()
        docXml.Load(myxml)
        Dim docXsl As New System.Xml.Xsl.XslCompiledTransform()
        docXsl.Load(myxslt)
        docXsl.Transform(docXml, Nothing, writer)
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ro = Response.Output
        mytransform(Server.MapPath("myxml.xml"), Server.MapPath("myxsltfile.xslt"), ro)
    End Sub
 
Back
Top