Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2004, 05:28 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default A Class in ASP.NET

The following is a class for a typical employee, that states whether an employee is a full time or a part time worker.I would like to know how can i incroporate other features including salary,social security number,address, etc.
Plus how can i make this class useful and fuctional in real business/company scenarios.


<%@ Page Language="VB" %>

<script language="vb" runat="server">
Public Class Employee
    'PRIVATE INSTANCE DATA MEMBERS
    Private _name as string
    Private _onduty as String

    Public Property Name() as String
        'ACCESSOR METHODS
        Get
          return _name
        End Get
        Set(value as String)
          _name=value
        End Set
    End Property

    Public ReadOnly Property Status() as String
        Get
          return _onduty
        End Get
    End Property

    Public Sub Working(CompanyTime as Integer)
        If CompanyTime > 20 Then _onduty = "Full Time"
        If CompanyTime < 20 Then _onduty = "Part Time"
    End Sub


End Class

Sub Page_Load()
    Dim emp1 as New Employee
    Dim emp2 as New Employee

    emp1.Name = "John"
    emp1.Working(10)

    emp2.Name="Sarah"
    emp2.Working(30)


    Response.Write(emp1.Name & " is a " & emp1.Status & " employee" + "</br>")
    Response.Write(emp2.Name & " is a " & emp2.Status & " employee" + "</br>")
End Sub
</script>


&nbsp;&nbsp;CEO InteliSoft

Maqsood ur Rahman
__________________
&nbsp;&nbsp;&nbsp;CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
 
Old March 19th, 2004, 09:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You look like you are on the right track, though maybe you want to make that clas in a separate class file instead of putting it in a web page. You have your options; you can add additional parameters for salary, SSN, etc., add any kind of functions for computation, validation, whatever, define events as needed. The simpler the better though.
 
Old March 20th, 2004, 10:06 AM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thnax Brian for your comment and appreciation.i really don't know how to call the class from my aspx file.Could you tell me how to do it.In addition to that i ahve improved this class to include SSN,Gender and salary.It is working perfectly
Please copy/paste the following and let me know how can make it more useful:
<%@ Page Language="VB" %>

<script language="vb" runat="server">
Public Class Employee
    'PRIVATE INSTANCE DATA MEMBERS
    Private _name as string
    Private _onduty as String
    Private _sal as Double
    Private _ssn as String
    Private _gender as String
    private _vacates as String


    Public Property Name() as String
        'ACCESSOR METHODS
        Get
          return _name
        End Get
        Set(value as String)
          _name=value
        End Set
    End Property

    Public Property SSN() as String
         Get
          return _ssn
        End Get
        Set(value as String)
              _ssn = value
        End Set
    End Property
    Public Property Gender() as String
        Get
          return _gender
        End Get
        Set(value as String)
          _gender = value
        End Set
    End Property

    Public ReadOnly Property Status() as String
        Get
          return _onduty
        End Get
    End Property

    Public Property Vacates() as String
        Get
          return _vacates
        End Get
        Set(value as String)
          _vacates = value
        End Set
    End Property

    Public ReadOnly Property Wage() as Double
        Get
          return _sal
        End Get
    End Property
    Public Sub Working(CompanyTime as Integer)
        If CompanyTime > 20 Then
            _onduty = "Full Time"
            _sal = CompanyTime * 5
        Else If CompanyTime < 20 Then
            _onduty = "Part Time"
            _sal = CompanyTime * 3
        End If
    End Sub


End Class

Sub Report(Sender as Object,E as EventArgs)
    Dim emp1 as New Employee
    Dim emp2 as New Employee
    Dim i as Integer
    Dim j as integer

    emp1.Name = txt1.Text
    emp1.Working(Val(txt2.Text))

    emp1.SSN= txt3.Text
    For i = 0 to 1
        if (lst3.Items(i).Selected) Then emp1.Vacates=lst3.Items(i).Text
        if lst1.Items(i).Selected then emp1.Gender = lst1.Items(i).Text
    Next

    emp2.Name=txt4.Text
    emp2.Working(Val(txt5.Text))

    emp2.SSN= txt6.Text
    For j = 0 to 1
        if (lst4.Items(j).Selected) Then emp2.Vacates=lst4.Items(j).Text
        if lst2.Items(j).Selected then emp2.Gender = lst2.Items(j).Text
    Next

    Response.Write(emp1.Name & " is a " & emp1.Gender & " " & emp1.Status & " employee with social security number " & emp1.SSN & ",wage is $ " & emp1.Wage & " and is " & emp1.Vacates & "</br>")

    Response.Write(emp2.Name & " is a " & emp2.Gender & " " & emp2.Status & " employee with social security number " & emp2.SSN & " and her wage is $ " & emp2.Wage & " and is " & emp2.Vacates & "</br>")

End Sub
</script>

<html>
    <body>
        <form runat="server">
              <table>
            <TR><TD>Employee Name</TD><TD><asp:TextBox id="txt1" runat="server"/></TD></TR>
            <TR><TD>No. Hours Working</TD><TD><asp:TextBox id="txt2" runat="server"/></TD></TR>
                  <TR><TD>Social Security Number</TD><TD><asp:TextBox id="txt3" runat="server"/></TD></TR>
              </table>
            Gender</b>
            <asp:RadioButtonList id="lst1" runat="server">
                <asp:listitem text="Male" id="opt1a" ruant="server"/>
                <asp:listitem text="Female" id="opt1b" ruant="server"/>
            </asp:RadioButtonList>
            Status
            <asp:RadioButtonList id="lst3" runat="server">
                <asp:listitem text="On Job" id="opt3a" ruant="server"/>
                <asp:listitem text="Off" id="opt3b" ruant="server"/>
            </asp:RadioButtonList>
            </br></br>
              <table>
            <TR><TD>Employee Name</TD><TD><asp:TextBox id="txt4" runat="server"/></TD></TR>
            <TR><TD>No. Hours Working</TD><TD><asp:TextBox id="txt5" runat="server"/></TD></TR>
                  <TR><TD>Social Security Number</TD><TD><asp:TextBox id="txt6" runat="server"/></TD></TR>
              </table>

            Gender<asp:RadioButtonList id="lst2" runat="server">
                <asp:listitem text="Male" id="opt2a" ruant="server"/>
                <asp:listitem text="Female" id="opt2b" ruant="server"/>
            </asp:RadioButtonList>
            Status
            <asp:RadioButtonList id="lst4" runat="server">
                <asp:listitem text="On Job" id="opt4a" ruant="server"/>
                <asp:listitem text="Off" id="opt4b" ruant="server"/>
            </asp:RadioButtonList>
            <asp:Button id="Eval" Text ="Tell me" onClick=Report runat="server"/>
        </form>
    </body>
</html>

&nbsp;&nbsp;CEO InteliSoft

Maqsood ur Rahman
 
Old March 20th, 2004, 09:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You look like you are on the right track. You instantiate an object and use it; remember a class is an object that you use as a variable. Name, SSN, etc. are parameters. You need to figure out what you need to make the class better (based on your business requirements). The only thing I can think of is figure out validation rules for SSN, make sure that you can't put in 1000000 for wage or something out of bounds, maybe change the status to an enum, if has a predefined set of values...

Hope this helps,

Brian Mains
 
Old March 21st, 2004, 07:43 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

In order to use this class for other parts of your application, you need to build the class into an assembly (DLL) so that the page code can use instances of those classes without the need to have the class code in the pages themselves. You can either compile that class directly into an assembly by itself, or you can compile all your VB.NET code for the web application into the web project's assembly (this is usually the first step). When you get into a large application scenario you can start to put all your "business entities" into class libraries. These are just VB.NET projects that have nothing but classes in them. The project compiles into an assembly. Your ASP.NET web application project then needs to reference the business classes project so the pages can use those classes. This whole progression can snowball very quickly.

How are you building your web applications at the moment? Are you using a development environment like Visual Studio or Web Matrix? I use VS but have never used Web Matrix so I don't know its capabilities. If you are writing all your pages and code in an environment that is not built for .NET then you would need to create some compilation scripts to be able to compile all the code into assemblies. It can get pretty complicated if you have to do it all by hand.

Let us know how you are doing things right now and we can take it from there.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 22nd, 2004, 03:24 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't work in any visual environment.I'm just using notepad for my scripts and then i save it as an aspx file and then just simply display it.
I really need to know how to compile my class even i have to write a seperate script file for it as you have mentioned.Or alternatively let me know the convenient way if there is any.
Could you please tell me how can i compile my class, all i have is .Net Framework/IIS installed on my system.
Also don't forget to have a look on my next post "A Typical Book Class" and reply me with your precious feedback.
Thanx to Brian Mains about the Validation rules he mentioned.

&nbsp;&nbsp;CEO InteliSoft

Maqsood ur Rahman
 
Old March 22nd, 2004, 04:02 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can use the command line compiler to build any code-behind files into an assembly. Most of the books have samples of this because they make the assumption that the reader doesn't have Visual Studio.net in front of them. However, the command line can be a bit difficult to manage.

There is a really good free, open source build tool for .net call NAnt. It came from the Ant Java build tool and is based on the same structure and similar syntax. I use it to build pretty complex multi-project systems, but it is very easy to use for building a small set of code files. The build file is in XML so it's very easy to use and manage. There is even an editor that someone created that provides a lot of explanation of the properties and settings that you need to use. Take a look at the NAnt site where you can see the documentation and download it.

http://nant.sourceforge.net

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 22nd, 2004, 06:47 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I downloaded nant-0.8.3-rc1 from the site you mentioned for installing nAnt but the Windows Installer Package takes ages to determine my disk space requirements.Could you plaese care to guide me what to do about it.


&nbsp;&nbsp;CEO InteliSoft

Maqsood ur Rahman
 
Old March 23rd, 2004, 02:00 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Forget about the installer. You don't need to install anything really.
Just download the latest release build (http://sourceforge.net/project/showf...group_id=31650) and unzip it somewhere. All you need to do is add the path to NAnt's bin folder (c:\nant\bin) to your system's path so that you can call nant from anywhere on your disk. Then you can start building a build file. Look at the documentation that comes with it for some examples of build files and for the task reference.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 23rd, 2004, 02:05 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Also, be warned! If you run NAnt in a directory that has a single file with the .build extension, NAnt will start building it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
My own class in asp.net 2.0 DarkForce ASP.NET 2.0 Professional 1 October 13th, 2008 12:39 PM
Error When Using Webclient class in ASP.NET sebastiansony .NET Web Services 0 June 24th, 2005 12:16 AM
Class existens in ASP.NET mega ASP.NET 1.x and 2.0 Application Design 7 November 23rd, 2004 01:30 PM
.NET class file in ASP page shahpragnesh_p VS.NET 2002/2003 1 August 22nd, 2003 10:50 AM
Exception Handling in ASP.net class mittalpa VS.NET 2002/2003 0 July 11th, 2003 03:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.