Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shane.H.1@gmail.com - 13 Nov 2006 19:20 GMT
Hi
Im learning C sharp, and I am trying to grok exceptions
Using the following code name is returning "N/A" where I want it to
return the string held by tempHold
What am I doing wrong?

public string Name
        {

            get{
                return name;
            }
            set{

                string tempHold = value;
                if(value.Length >14){

                    //value.Length = -0;
                    string tempStr = value;
                   tempHold = "";
                    for (int i = 0; i <=14;i++){
                                tempHold += tempStr[i];
                    }
                    value = tempHold;
                    Exception e = new Exception("Error the name must be less than 14
characters long: name now equals" + tempHold);
                    throw e;
                }
                name = value;
            }
        }
ssamuel - 13 Nov 2006 19:36 GMT
How about this:

public string Name
{
 get
 {
   return name;
 }
 set
 {
   if (value != null || value.Length < 15)
     throw new Exception(...);
   name = value;
 }
}

You seem to be doing many unnecessary operations and you're missing
several little things. Simplicity breeds success, and a simpler method
will probably give you fewer problems.

Stephan

> Hi
> Im learning C sharp, and I am trying to grok exceptions
[quoted text clipped - 27 lines]
>             }
>         }
Shane.H.1@gmail.com - 13 Nov 2006 19:42 GMT
> > Hi
> > Im learning C sharp, and I am trying to grok exceptions
[quoted text clipped - 48 lines]
>
> Stephan

Hi,
Yeah the only thing is I want name updated if its too long
Using your suggestion I could ask the user to re-enter new details but
I want to handle the issue in the code
Also, with your suggestion name is given value, which may be too long
I only want name (in this case) to be 0 - 14 chars long
ssamuel - 13 Nov 2006 19:52 GMT
> Yeah the only thing is I want name updated if its too long

Huh? You want it updated *then* you want to throw the exception?

That's easy:

set
{
 name = value;
 if (name == null || name.Length > 14)
   throw new Exception(...);
}

There's almost definitely a better way to implement that logic,
however.

> Using your suggestion I could ask the user to re-enter new details but
> I want to handle the issue in the code

In a property is never the correct place to ask the user for more or
different information. Your best bet here is to do something more
robust with user input. Try something like getting the input, then
immediately checking if it's valid.

> Also, with your suggestion name is given value, which may be too long
> I only want name (in this case) to be 0 - 14 chars long

I'm sure you can figure out how to tweak the exact logic (> and <
signs, etc.) to fit what you're trying to do.

Are you sure you understand the symantic meaning of classes and
properties? It seems like you're trying to write procedural (non-OOP)
code and shoehorn it into a class.

Stephan
Jon Skeet [C# MVP] - 13 Nov 2006 19:36 GMT
> Im learning C sharp, and I am trying to grok exceptions
> Using the following code name is returning "N/A" where I want it to
> return the string held by tempHold
> What am I doing wrong?

Well, you're only setting name if the value that's passed in is valid -
and that seems only reasonabe. An exception is being thrown otherwise.
The exception is thrown instead of the property being set, and that's
usually a useful semantic: it would be quite odd for name to be changed
*and* an exception to be thrown. (Where possible, it's useful for a
method to make no visible changes if it throws an exception.)

You should also look up String.Substring, by the way: your current code
is equivalent to (but less efficient and harder to read than):

   public string Name
   {        
       get
       {
           return name;
       }
       set
       {
           if (value.Length > 14)
           {
               throw new Exception
                   ("Error the name must be less than " +
                    "14 characters long: name now equals" +
                    value.Substring (0, 14));
           }
           name = value;
    }
   }

Now, if you *do* want the semantics suggested by the text of the
exception, use:

   public string Name
   {        
       get
       {
           return name;
       }
       set
       {
           if (value.Length > 14)
           {
               name = value.Substring(0, 14);
               throw new Exception
                   ("Error the name must be less than " +
                    "14 characters long: name now equals" +
                    name);
           }
           else
           {
               name = value;
           }
    }
   }

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Shane.H.1@gmail.com - 13 Nov 2006 19:46 GMT
Jon wrote:
> > Im learning C sharp, and I am trying to grok exceptions
> > Using the following code name is returning "N/A" where I want it to
[quoted text clipped - 60 lines]
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too

Sweet, thats exactly what needed to be done, as is probably obvious Im
not overly familiar with SubString, but thats how I learn right :-)
sloan - 13 Nov 2006 19:40 GMT
Huh??
Why are you looping on it?

> public string Name
> {

set
{

   bool badName = false;

if( null==value)
{
       badName = true;
}
else
{
   if(value.Length >14)
{
   badName = true;
}
}

   if (badName==true)
{
   throw new ArgumentException("Name must be < 14 characters");
}

   m_name = value;

}
}

> Hi
> Im learning C sharp, and I am trying to grok exceptions
[quoted text clipped - 27 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.