8 Replies - 472 Views - Last Post: 15 April 2010 - 03:04 PM Rate Topic: -----

#1 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Method Return Types

Posted 15 April 2010 - 12:25 PM

Hey all.

Well basically I'm about to start building a class library containing extension methods for data validation.

I've come to the conclusion that I want to return a bool (determine if the data is correct of not) and a string containing the particular reason why the data isn't valid. As a method can only have one return type, I thought of creating a new class and use it's constructor to return the two pieces of data above.

However this is too problematic as the mini class to hold data would have to be static, however it can't have parameters as a static class. Anyway I could go on.

So I'm looking for suggestions, below is some basic code, I actually though of maybe creating a generic list of type my class, and simply change the return type to list, but there's probably a flaw in this :).

    public static class DataValidation
    {
        public static bool IsValidEmail(this string email)
        {

            if (string.IsNullOrEmpty(email))
            {
                // return a bool = false, and a string with an error message
            }
        }
    }



Advice appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Method Return Types

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6056
  • View blog
  • Posts: 23,493
  • Joined: 23-August 08

Re: Method Return Types

Posted 15 April 2010 - 12:30 PM

What's the "this" in your argument?

If you want to return an error along with the bool, you'll have to use a ref string or out string as an argument.
Was This Post Helpful? 1
  • +
  • -

#3 egof  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: Method Return Types

Posted 15 April 2010 - 12:36 PM

You could throw an exception then have your calling code catch the InvalidEmailException
public class InvalidEmailException:ApplicationException
{
        public InvalidEmailException()
        {

        }
        public InvalidEmailException(string reason):base(reason)
        {

        }
}
    public static class DataValidation
    {
        public static bool IsValidEmail(this string email)
        {

            if (string.IsNullOrEmpty(email))
            {
                throw new InvalidEmailException("you forgot to add the email");
            }
            return true;
        }
    }


Was This Post Helpful? 1
  • +
  • -

#4 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: Method Return Types

Posted 15 April 2010 - 12:38 PM

View PostJackOfAllTrades, on 15 April 2010 - 11:30 AM, said:

What's the "this" in your argument?

If you want to return an error along with the bool, you'll have to use a ref string or out string as an argument.


Thanks for the links, and the 'this' is required as it shows which type your methods regarding, at least that's what I think.

Edit: egof nice suggestion, I might implement that.

This post has been edited by Bacanze: 15 April 2010 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6056
  • View blog
  • Posts: 23,493
  • Joined: 23-August 08

Re: Method Return Types

Posted 15 April 2010 - 12:54 PM

While throwing an exception is an option, it does not appear to fit with what you're attempting to do. Exceptions are expensive...they should be a last resort. A data validation class should NOT -- in my opinion -- throw exceptions. Think of the TryParse() functions for each primitive type as a reference point.
Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Method Return Types

Posted 15 April 2010 - 01:00 PM

Quote

However this is too problematic as the mini class to hold data would have to be static


Why?

public class ValidationResult
{
    public bool IsValid { get; set; }
    public string Message { get; set; }
}



Then:

public static class ValidatorExtensions
{
    public static ValidationResult IsValidEmail(this string email)
    {
        if (String.IsNullOrEmpty(email))
            return new ValidationResult() { IsValid = false, Message = "Blank email" };
        else
            return new ValidationResult() { IsValid = true, Message = "Success" };
    }
}



Static classes have no problem returning instances of objects. In fact, that's exactly how factories behave.

public static class AccountFactory
{
    public static Account CreateAccount(AccountInformation accountInfo)
    {
        switch (accountInfo.AccountType)
        {
            case AccountTypes.User:
                return new UserAccount(accountInfo);
            case AccountTypes.Administrator:
                return new AdministratorAccount(accountInfo);
            default:
                return new GuestAccount(accountInfo);
        }
    }
}



Just an example.

You know, I might be missing a detail here. If so, sorry.
Was This Post Helpful? 2
  • +
  • -

#7 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: Method Return Types

Posted 15 April 2010 - 02:36 PM

View PostMentalFloss, on 15 April 2010 - 12:00 PM, said:

Quote

However this is too problematic as the mini class to hold data would have to be static


Why?

public class ValidationResult
{
    public bool IsValid { get; set; }
    public string Message { get; set; }
}



Then:

public static class ValidatorExtensions
{
    public static ValidationResult IsValidEmail(this string email)
    {
        if (String.IsNullOrEmpty(email))
            return new ValidationResult() { IsValid = false, Message = "Blank email" };
        else
            return new ValidationResult() { IsValid = true, Message = "Success" };
    }
}



Static classes have no problem returning instances of objects. In fact, that's exactly how factories behave.

public static class AccountFactory
{
    public static Account CreateAccount(AccountInformation accountInfo)
    {
        switch (accountInfo.AccountType)
        {
            case AccountTypes.User:
                return new UserAccount(accountInfo);
            case AccountTypes.Administrator:
                return new AdministratorAccount(accountInfo);
            default:
                return new GuestAccount(accountInfo);
        }
    }
}



Just an example.

You know, I might be missing a detail here. If so, sorry.


Thanks for the awesome example MentalFloss, I was using a constructor in the class and using it to pass the values to public properties within the class. Can't beleive I didn't think of just creating the auto implement properties within a class :(.

Anyway the end result looks really nice, time to start building my classes :).

 bool result = textBox1.Text.IsValidEmail().IsValid;

Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Method Return Types

Posted 15 April 2010 - 02:44 PM

I think you might want to go with retrieving the entire object from the validation call. That way, when it's false, you can process the message easier. The way you have it is you would check result and when it's false, youd call the method again and instead get the .Message property.

So, I'd do this (and change that method name. You're doing something which is validation. So you are Validating the email - ValidateEmail().

ValidationResult result = textbox1.Text.ValidateEmail();

if (!result.IsValid)
{
    MessageBox.Show(String.Format("{0}", result.Message));
}
else
{
    // business as usual...
}


Was This Post Helpful? 0
  • +
  • -

#9 Bacanze  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 202
  • Joined: 09-April 10

Re: Method Return Types

Posted 15 April 2010 - 03:04 PM

Thanks for the reply again, yeah I tried something similar, except using var.

var result = textBox1.Text.IsValidEmail();


I was just testing bits of code out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1