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

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

ASP.NET_NewbieHowTo: Import external class files in .aspx file

Question: How do I "USE" a class I created in my webform?

The book i'm reading (Teach yourself ASP.NET in 21 days) works from the premise that everyone is using NOTEPAD. As such in day 03 it talks about using creating classes which I understand. Only problem is...i'm using Visual Studio.net (2003) and as such, i'm using "code-behind". THE BOOK IS NO HELP WITH CODE-BEHIND.

I know, from reading code that a NameSpace is important (naming collisions, etc.) by i do not know how to embedd the directive in the page.

the class code follows...thanks in advance!

the_sleeper

=======[simple exercise]==========

Namespace day03
    Public Class clsHuman
        Private m_height As String
        Private m_weight As String
        Private m_haircolor As String
        Private m_eyecolor As String
        Private m_birthdate As Date

        Public Property Height() As String
            Get
                Return m_height
            End Get
            Set(ByVal Value As String)
                m_height = Value
            End Set
        End Property

        Public Property Weight() As String
            Get
                Return m_weight
            End Get
            Set(ByVal Value As String)
                m_weight = Value
            End Set
        End Property

        Public Property HairColor() As String
            Get
                Return m_haircolor
            End Get
            Set(ByVal Value As String)
                m_haircolor = Value
            End Set
        End Property

        Public Property EyeColor() As String
            Get
                Return m_eyecolor
            End Get
            Set(ByVal Value As String)
                m_eyecolor = Value
            End Set
        End Property

        Public Property BirthDate() As Date
            Get
                Return m_birthdate
            End Get
            Set(ByVal Value As Date)
                m_birthdate = Value
            End Set
        End Property
    End Class
End Namespace
 
0
the_sleeper
Asked:
the_sleeper
  • 3
  • 2
1 Solution
 
nauman_ahmedCommented:
Ok. Lets simplify it first: I have a page called HomePage.aspx that  has a namespace ABCINC. I am using code behind. So I would have declared the code as follows:

Imports System
Imports System.Web
Namespace ABCInc
    Public Class MyHomePage
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            Dim clsHuman As New clsHuman
            clsHuman.BirthDate = CDate("1/1/1998")
            clsHuman.EyeColor = "Brown"
            clsHuman.HairColor = "Black"
            clsHuman.Weight = "140"

            Response.Write("Birth Date: " & clsHuman.BirthDate & "<br>")
            Response.Write("Eye Color: " & clsHuman.EyeColor & "<br>")
            Response.Write("Hair Color:" & clsHuman.HairColor)
            Response.Write("Weight: " & clsHuman.Weight & "<br>")

        End Sub
    End Class
End Namespace


Now if you have created your page using VS.Net, it will automatically add the following statement at the top of the page:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyHomePage.aspx.vb" Inherits="VBWebApplication.ABCInc.MyHomePage"%>

So now compile and run your code and you will see the output. But hey clsHuman is missing a very important property: Name :)

HTH, Nauman.

0
 
nauman_ahmedCommented:
Here is the complete code:

Imports System
Imports System.Web
Namespace ABCInc
    Public Class MyHomePage
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            Dim clsHuman As New clsHuman
            clsHuman.BirthDate = CDate("1/1/1998")
            clsHuman.EyeColor = "Brown"
            clsHuman.HairColor = "Black"
            clsHuman.Weight = "140"

            Response.Write("Birth Date: " & clsHuman.BirthDate & "<br>")
            Response.Write("Eye Color: " & clsHuman.EyeColor & "<br>")
            Response.Write("Hair Color:" & clsHuman.HairColor)
            Response.Write("Weight: " & clsHuman.Weight & "<br>")

        End Sub

    End Class
    Public Class clsHuman
        Private m_height As String
        Private m_weight As String
        Private m_haircolor As String
        Private m_eyecolor As String
        Private m_birthdate As Date
        Private m_Name As String

        Public Property Height() As String
            Get
                Return m_height
            End Get
            Set(ByVal Value As String)
                m_height = Value
            End Set
        End Property

        Public Property Weight() As String
            Get
                Return m_weight
            End Get
            Set(ByVal Value As String)
                m_weight = Value
            End Set
        End Property

        Public Property HairColor() As String
            Get
                Return m_haircolor
            End Get
            Set(ByVal Value As String)
                m_haircolor = Value
            End Set
        End Property

        Public Property EyeColor() As String
            Get
                Return m_eyecolor
            End Get
            Set(ByVal Value As String)
                m_eyecolor = Value
            End Set
        End Property

        Public Property BirthDate() As Date
            Get
                Return m_birthdate
            End Get
            Set(ByVal Value As Date)
                m_birthdate = Value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return m_Name
            End Get
 
            Set(ByVal Value As String)
                m_Name = Value
            End Set
        End Property
    End Class

End Namespace
0
 
the_sleeperAuthor Commented:
@nauman_ahmed>>Ok. Lets simplify it first: I have a page called HomePage.aspx that  has a namespace ABCINC. I am using code behind. So I would have declared the code as follows...


 ? Which page are you adding this code to?
    a.  the .aspx
    b. the code behind?

@nauman_ahmed >>But hey clsHuman is missing a very important property: Name :)

wow. my first asp.net bug-a-boo. good catch...



0
 
the_sleeperAuthor Commented:
i guess no responses....any takers?
0
 
nauman_ahmedCommented:
You have to add the code in the .aspx.vb file and the following in .aspx page:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyHomePage.aspx.vb" Inherits="VBWebApplication.ABCInc.MyHomePage"%>

Sorry for the late response.

Best, Nauman.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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