StevieB
StevieB

Reputation: 6523

How to Call a function from a class in Visual Studio Solution

Now to working with Visual Studio Solutions, but I am trying something very basic but no joy.

So in my project I have a folder App_Code and in this I added a class called Test.vb

and added a simple function

Public Class Test2
Public Function ReplaceXSS(ByVal InputString As String) As String
    InputString = InputString.Replace("<script>", "")
    InputString = HttpUtility.HtmlEncode(InputString)
    InputString = InputString.Replace("</script>", "")
    InputString = InputString.Replace("&", "&#38;")
    InputString = InputString.Replace("<", "&#60;")
    InputString = InputString.Replace(">", "&#62;")
    InputString = InputString.Replace("%", "&#37;")
    InputString = InputString.Replace("|", "&#124;")
    InputString = InputString.Replace("$", "&#36;")
    InputString = InputString.Replace("'", "&#39;")
    InputString = InputString.Replace("""", "&#92;")

    ReplaceXSS = InputString
End Function

End Class

But in my Default.aspx page I am struggling to find the correct syntax to call this method.

Also it doesnt seem like my Test2.vb class is correctly included in the project because If I make a type the project still builds successfully when it shouldnt

Upvotes: 0

Views: 5071

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

The problem is that you Test2 is under some namespace, that's you are unable to make object of that class. You have to mention the namespace before class name Dim test As New NameSpace.Test2

Dim test As New Test2 ' Make an object of that class
test.ReplaceXSS(Parameters.....) ' access the method 

Upvotes: 1

Icemanind
Icemanind

Reputation: 48686

You need to make sure you have a reference to the project that houses Test2. After you have a reference, you should be able to:

Dim MyTest2 As New Test2

If you still cannot instantiate an instance of the object, make sure there is no namespace you defined. If there is, prefix Test2 with the name of the namespace plus a dot.

Upvotes: 0

Related Questions