Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 May 23rd, 2006, 04:13 AM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Basic OOPS..invariant and object creation

The requirement in my project is creation of itinearay ..which will have a name,category(like adventure,classisc),noofdays(eg 6 day itinerary)..so..to design this situation when user creates an itinerary,i need to create an itinerary object with name,category and noofdays(my understnding is that these are invariant of class)..my question is where to check if data supplied by user is incorrect(eg 0 nofodays is invalid)..in constructor..if so,if data is invalid..what nees to be done raise exception and destroy object? or use a factory class to create object..which will check data provided and if everything is ok,then create itinerary object..How i have implememted..
Code:
public class Itinerary
    {
        private string _ItineraryName;
        private byte _ItineraryDays;
        private string _ItineraryCategory;

     public  Itinerary(string itineraryName,byte itineraryDays,string itineraryCategory)
        {
            if (itineraryName == "")
            {
                throw new ArgumentException() ; 

            }
            else
            {
                _ItineraryName = itineraryName;
                _ItineraryDays = itineraryDays;
                _ItineraryCategory = itineraryCategory;
            }
        }
        .

 
Old May 24th, 2006, 05:58 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

within constructor's else part

Bijgupt
 
Old May 24th, 2006, 06:04 PM
Authorized User
 
Join Date: May 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The answer to this question depends on the structure of your code.

I would suggest checking that the data is valid before creating the new Instance (and thus calling the constructor).

Where is the data that is sent to the constructor coming from? If it is coming from form controls, then
I would suggest something like this:

// assuming all controls are textboxes:
Itinerary itinerary = null;
string IName = ItineraryNameControl.Text;
int IDays;
if (!TryParse(ItineraryDaysControl.Text, IDays))
   IDays = 0;
string ICategory = ItineraryCategory.Text;

if (IName.Equals(String.Empty))
   MessageBox.Show ("Need name of itinerary.");
else if (IDays == 0)
   MessageBox.Show ("Please provide the number of days.");
else if (ICategory.Equals(String.Empty))
   MessageBox.Show ("Please categorize this itinerary.");
else
{
   itinerary = new Itinerary (IName, IDays, ICategory);
   // process away.
}

In this way, you know that the instance will be constructed with valid data.

Brandon





Similar Threads
Thread Thread Starter Forum Replies Last Post
submitting form - object creation timing forumuser BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 December 3rd, 2007 02:59 PM
oops with c#.net sureshmm General .NET 8 March 22nd, 2007 10:43 PM
oops with c#.net sureshmm ASP.NET 1.0 and 1.1 Basics 3 March 21st, 2007 11:58 AM
OOPs bijgupt C# 5 May 18th, 2006 05:36 AM
Problem understanding Object creation cdplayer Classic ASP Basics 4 March 18th, 2004 07:04 PM





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