Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

asp.net vb.net sub in class from another page in project

I have an asp.net web application using VB code behind.  I have created a class to show a message box (trying to learn how to use classes).  I cant seem to figure out how to call the sub within the class.  Everything I have found sudgest that it should be working. I get an error that ShowMessage (name of sub in class) is not a member of WebMsgBox (name of class).  Thanks in advace for the help
***** VB Code Calling Class ******

Public Class UserManagment
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnRun_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRun.Click

        WebMsgBox.ShowMessage("this is a test")
    End Sub
End Class

******* Class Code *******
Public Class WebMsgBox
    Public Class WebMsgBox
        Protected Shared handlerPages As New Hashtable()
        Private Sub New()
        End Sub

        Public Shared Sub ShowMessage(ByVal Message As String)
            If Not (handlerPages.Contains(HttpContext.Current.Handler)) Then
                Dim currentPage As Page = DirectCast(HttpContext.Current.Handler, Page)
                If Not ((currentPage Is Nothing)) Then
                    Dim messageQueue As New Queue()
                    messageQueue.Enqueue(Message)
                    handlerPages.Add(HttpContext.Current.Handler, messageQueue)
                    AddHandler currentPage.Unload, New EventHandler(AddressOf CurrentPageUnload)
                End If
            Else
                Dim queue As Queue = DirectCast(handlerPages(HttpContext.Current.Handler), Queue)
                queue.Enqueue(Message)
            End If
        End Sub

        Private Shared Sub CurrentPageUnload(ByVal sender As Object, ByVal e As EventArgs)
            Dim queue As Queue = DirectCast(handlerPages(HttpContext.Current.Handler), Queue)
            If queue IsNot Nothing Then
                Dim builder As New StringBuilder()
                Dim iMsgCount As Integer = queue.Count
                builder.Append("<script language='javascript'>")
                Dim sMsg As String
                While (iMsgCount > 0)
                    iMsgCount = iMsgCount - 1
                    sMsg = System.Convert.ToString(queue.Dequeue())
                    sMsg = sMsg.Replace("""", "'")
                    builder.Append("alert( """ & sMsg & """ );")
                End While
                builder.Append("</script>")
                handlerPages.Remove(HttpContext.Current.Handler)
                HttpContext.Current.Response.Write(builder.ToString())
            End If
        End Sub
    End Class
End Class

Open in new window

0
NCSA SCADA
Asked:
NCSA SCADA
2 Solutions
 
Miguel OzSoftware EngineerCommented:
You are defining  Class WebMsgBox twice

Public Class WebMsgBox
    Public Class WebMsgBox

Just use one: (delete the other one)
    Public Class WebMsgBox

Also check that they belong to the same namespace.
0
 
käµfm³d 👽Commented:
You have declared a class within a class. You need to refer to both naming levels when trying to call the shared method:
Protected Sub btnRun_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRun.Click

    WebMsgBox.WebMsgBox.ShowMessage("this is a test")
End Sub

Open in new window

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now