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 22nd, 2004, 03:35 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default A Typical Book Class

I have tried my best to model a typical book as an object and produced the following class.It also returns you the profit/loss when a particular book is being sold and in addition to this it also returns the local currency symbol.
Take time to have a look and rply me with your critical comments and feedback.

<%@ Page Language="VB" runat="server" %>

<script language="VB" runat="server">
Public Class Book
    Private _title as String
    Private _isbn as String
    Private _author as String
    Private _result as String
    Private _profit as Double
    Private _price as Decimal
    Private _LocalPrice as Decimal
    Private _LocalCur as String
        Private _LocalCurSym as String

    Public Property Title() as String
        Get
          return _title
        End Get
        Set(value as String)
          _title = value
        End Set
    End Property
    Public Property ISBN() as String
        Get
          return _isbn
        End Get
        Set(value as String)
          _isbn = value
        End Set
    End Property
    Public Property Author() as String
        Get
          return _author
        End Get
        Set(value as String)
          _author=value
        End Set
    End Property
    Public ReadOnly Property LocCurSym() as String
        Get
           return _LocalCurSym
        End Get
    End Property
    Public ReadOnly Property Profit() as Double
        Get
           return _profit
        End Get
    End Property
    Public ReadOnly Property Result() as String
        Get
          return _result
        End Get
    End Property
    Public ReadOnly Property LocalPrice() as Decimal
         Get
          return _LocalPrice
        End Get
    End Property
    Public Sub ConLocal(LocCur as String)
        _LocalCurSym = LocCur
        Select Case LocCur
            Case "US Dollar"
                 _LocalPrice = _price
                 _LocalCurSym = "$ "
            Case "Rupees"
                 _LocalPrice = _price * 60.00
                 _LocalCurSym = "Rs."
            Case "Canadian Dollar"
                 _LocalPrice = _price * 43.50
                 _LocalCurSym = "$ "
        End Select
    End Sub
    Public Sub CalcProfit(CostPrice as Double,RetailPrice as Double)
        If (CostPrice <= 500 And RetailPrice <= 500) Then
            _price = RetailPrice
            _profit = RetailPrice - CostPrice
            Select Case _profit
                Case 0
                      _result = "No Profit No Loss"
                Case > 0
                      _result = "Profit"
                Case < 0
                       _result = "Loss"
            End Select
        Else
             Exit Sub
        End If
    End Sub
End Class
Public Sub BookDetail(Sender as Object,E as EventArgs)
    Dim Book1 as New Book()
    Book1.Author ="Knuth"
    Book1.ISBN = "32432-23-2342"
    Book1.Title = "The Art of Computer Programming"

    Book1.CalcProfit(5000,10)
    Book1.ConLocal("Canadian Dollar")

    Response.Write("<b>Author: </b>" & Book1.Author & "</br>")
    Response.Write("<b>ISBN: </b>" & Book1.ISBN & "</br>")
    Response.Write("<b>Title: </b>" & Book1.Title & "</br>")
    Response.Write("<b>Result: </b>" & Book1.Result & "</br>")
    Response.Write("<b>Return: </b>" & Book1.Profit & "</br>")
    Response.Write("<b>Local Price: </b>" & Book1.LocCurSym & Book1.LocalPrice)
End Sub
</script>

<html>
    <body>
        <form runat="server">
            <Table>

            </Table>
            <asp:Button id="btn" Text = "Book Details" onClick=BookDetail runat="server"/>
        </form>
    </body>
</html>


&nbsp;&nbsp;CEO InteliSoft

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

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

With regards to class design, I'd say this is just fine. The only thing I would point out is that you could utilize the localization capabilities of .NET for the currency symbol part. You can create resource files that have the appropriate data for a given locale so you don't have to do the tests and you can easily add more.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 22nd, 2004, 04:37 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You could also try to make your class behave in a more natural way. For example, what is a book without an ISBN number? In most cases, the ISBN is probably required. To enforce that, you could make the ISBN Number ReadOnly, and change the Constructor so you have to pass in the ISBN number, like this:

Public Sub New (isbn as String)
  Me._isbn = isbn
End Sub

This way, you cannot construct a book like this any longer:

Dim BeginningDreamweaverMX2004 As New Book()

but you'll have to do something like this instead:

Dim BeginningDreamweaverMX2004 As New Book("0-7645-5524-3")

This makes you class safer, as you can now no longer create books without an ISBN number.

Of course, all this depends on what you're trying to do with your book class. If you run a modern publisher, the ISBN is probably required. If you try to build an index of ancient books discovered in Egypt, I am sure an ISBN is of not much use ;)
The fact that the ISBN (or other fields like Title etc) are ReadOnly or not also depends on your design. Sometimes such fields should be ReadOnly, in other scenario's it can be useful if you are able to change them from outside the class.


Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 23rd, 2004, 03:12 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I really need to know how to create Resource files, Can you tell me how to use the localization capabilities of .Net for the currency symbol part i did.As planoie have said that by doing it this class is going to be more flexible and i can add/modify the currency symbols as and when required.

Thanx to Imar for his precious feedback.Would he also care to leave his comments on my employee class as well, i shall be grateful.

   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
 
Old March 23rd, 2004, 04:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Sure. What is the current status of the Employee class? Can you pst the latest version of the code somewhere?

Are you using these classes in a real world application? The current implementation seems rather theoretical / academical to me....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 23rd, 2004, 04:15 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well that's what i have designed so far,let me know how can i make it practical and useful for real world applications?
Assume that i like to use this class for a typical Payroll/holidays/overtime/performance log,etc.


   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
 
Old March 23rd, 2004, 04:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right. But where can I find the most recent version of the Employee class? This thread only seems to deal with the Book class....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 23rd, 2004, 04:30 PM
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Click the title "A Class in ASP.Net" and brwose thru the code.This was my first ever class designed in ASP.Net.

   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
 
Old March 23rd, 2004, 04:57 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ah, OK. You posted the code in an earlier thread. I see now....

Like I said earlier, the design of a class depends heavily on the application you are developing (otherwise, you could just use someone else's generic Employee class).

There are many factors influencing this design, like specific business rules, your country (in my country a SSN looks different than one used in the US), the goal of the app (ancient books from Egypt Vs a modern publisher) and many many others, so it's hard to give feedback about your class without knowing its goal.

A few general comments can be made, though.

Gender
IMO, Gender is not a string. A Gender has clearly defined boundaries, so you can use an Enum for that instead, something like this:

Public Enum GenderType
  Female,
  Male,
  NotSpecified,
  Unknown
End Enum

The property will then look like this:

Public Property Gender() As GenderType
        Get
          return _gender
        End Get
        Set(value As GenderType)
          _gender = value
        End Set
End Property

And in your app, you can then use something like:

If Me.Gender = GenderType.Female Then....

It looks like NotSpecified is the same as Unknown, but that is not necessarily true. NotSpecified could be something like "none of your business", whereas with Unknown, you never asked or got an answer.....

Status
What is status? Sounds vague... Is it a job status? Then call it JobStatus. You may define a JobStatusType Enum for that as well.
In any case, give it a proper and clearly identifying name.

Wage
Why is wage read only? Can't I get a raise if I have an excellent performance record?

General stuff
What about Age, Date of Birth, **************** Size, Pet's name? First Name, Initials, Eye Color, Job Type, Manager, Experience, Resume Record, Name of Mistress (in case you need to contact your employee during the weekends....), Department, Home Address, Phone Number, E-mail Address etc etc etc etc. You may not need all this data, but you should carefully consider what you need or don't need.

It has been mentioned before, but what about validation? What does an employee with a name of " " mean in your application? How do you handle that?

What applies to a Book, applies to the Employee as well. You have to consider what data is very important to an Employee. Is the SSN required? Or is the Name required? If either, or both of them are required, modify the constructor, and have it accept a SSN and a Name. Set the internal variables based on the stuff from the constructor.

That way, this will no longer work:

Dim MrX as New Employee()

but this will:

Dim MyValuedEmployee("Mr. Bladiebla", "12934444")

This way, you won't have an anonymous employee laying around.

All in all, I think you're on the right track with this. I have more comments about your class, but I don't think they are appropriate right now. Teaching Class Design is not something that works well over a public forum.
I think it's a good idea to read more about Class Design to understand the classes you're designing better (and to design better classes). Wrox used to have an excellent book about Class Design called "Visual Basic .NET Class Design Handbook" which sounds ideal for you.

HtH,

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Property access from Class within Partial Class zoltac007 C# 2005 0 December 1st, 2006 01:01 AM
Typical Algorithm rajanikrishna ASP.NET 1.0 and 1.1 Professional 0 November 20th, 2006 03:05 AM
Displaying null value in a typical combobox - C# momof2 C# 1 August 24th, 2006 12:47 AM
Regarding Class Library (.dll) from class file manish.sharma04 BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 March 3rd, 2006 07:32 AM
typical logic rajanikrishna Classic ASP Basics 2 June 11th, 2004 11:15 PM





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