Home
About
Contact
Tuesday, June 23, 2009

Recently I worked with a colleague on some code using decimal.TryParse() and tried to find a better way of using it. Specifically I had a string value representing a currency amount entered by a user in a web form. The amount needed more than just normal validation, so I needed to do some stuff with it.

Here's the code I started with:

public void SomeMethod(string userEnteredAmount)
{
    decimal amount;
    if(decimal.TryParse(userEnteredAmount, out amount) {
        //Do some stuff with amount
    }
    else {
        //Show message to user about invalid amount
    }
}

I've recently found many cases where I find the use of Value Objects to be very applicable. I found this case to be a particularly interesting example. Anyway, I created a Value Object called Amount to abstract away the TryParse stuff. Below is the class I created.

public class Amount
{
    private readonly decimal _value;
    private readonly bool _isValid;
    public Amount(string amount)
    {
        _isValid = decimal.TryParse(amount, out _value);
    }
    public decimal Value
    {
        get { return _value; }
    }
    public bool IsValid
    {
        get { return _isValid; }
    }
}

See how clean it got? At least I think so. The important part though is that it's usage is so much easier to understand and read:

var amount = new Amount(userEnteredAmount);
if(amount.IsValid) {
    //Do some stuff with amount.Value
}
else {
    //Show message to user about invalid amount
}

It's more code, but cleaner. Or did I say that already? :-) And of course it's reusable other places where amount has a meaning.

.Net | CSharp | DDD | Patterns
Tuesday, June 23, 2009 12:17:10 AM (W. Europe Daylight Time, UTC+02:00)
Thursday, June 04, 2009

As I’ve previously twitted through @nnugbergen and also through the invites I sent out yesterday, Scott Hanselman and Jeremy D. Miller is coming to NNUG Bergen Monday the 15th of June (Jeremy) and Saturday 20th of June (Scott).

Jeremy will do two talks. The first being Software Design and Testabillity and the second will be about StoryTeller, his take on integration testing. Scott will do his Deep Tour of .NET 4 talk.

To sign up or see the full agenda, use the links below:

Jeremy: http://nnug.no/Avdelinger/Bergen/Moter/NNUG-Bergen---Juni-2009---Jeremy-D-Miller/
Scott: http://nnug.no/Avdelinger/Bergen/Moter/NNUG-Bergen---Juni-2009---Scott-Hanselman/

In case you don’t know, all events going through NNUG is free, including these two! I’m already now worried how we’re going to top this next year. Maybe we should just close down NNUG Bergen after 2009 ;-)

Events | NNUG
Thursday, June 04, 2009 1:08:05 PM (W. Europe Daylight Time, UTC+02:00)
RSS RSS - Comments Twitter LinkedIn
         
SEARCH
 
 
         
TOP POSTS
   
         
NAVIGATION
   
         
CATEGORIES
  .Net (61) ADFS (3) Agile (30) Ajax (5) Architecture (20) Articles (1) ASP.NET (6) ASP.NET-MVC (1) Blogging (12) Books (2) BPEL (1) CleanCode (1) CloudComputing (7) Community (4) CSharp (11) DasBlog (5) Database (2) DDD (5) Deployment (16) DSL (1) Events (38) ExtremeProgramming (6) Fun (6) Gadgets (4) IIS (10) InfoQ (4) Java (2) Lean (3) Linq (2) MemoryLeaks (5) Microsoft (37) MVC (1) NDC (2) NNUG (36) Other (10) Patterns (9) Performance (3) Scrum (17) Security (7) ServiceBus (1) Silverlight (4) Software (19) TeamManagement (11) TechEd (7) Testing (4) Tools (25) TvGuide (1) WCF (8) Web (15) WebDeploy (1) WIF (3) Windows (10) Vista (15) VisualStudio (16) WiX (9) Work (16) Workflow (3)  
         
ARCHIVE
   
         
BLOGROLL
   
         
ON THIS PAGE...
 
Refactoring TryParse Into a Value Object
Scott Hanselman and Jeremy D. Miller to NNUG Bergen!