3

We all know this thing

bool.Parse(Session["foo"].ToString())

How do implement the same PARSE method for some custom class?

So if I have

class MyClass
{

}

It would be possible to do like

MyClass.Parse(Session["foo"])
4
  • What problem exactly are you facing? What's stopping your from simply writing the Parse() method? Commented Apr 15, 2012 at 14:54
  • well I need the syntax like this MyClass.Parse(Session["foo"]) and not like MyClass newclass = new MyClass(); newclass.Parse(Session["foo"]) Commented Apr 15, 2012 at 14:56
  • And why don't you directly store MyClass in the Session? Commented Apr 15, 2012 at 14:59
  • Do you have an example for what kind of string you would like to be able to parse? Commented Apr 15, 2012 at 15:00

5 Answers 5

8

All you need to do is write a static method called Parse() for your class that takes the String and creates an Instance of MyClass with it.

public class MyClass
{
    public static MyClass Parse(string input)
    {
        if(String.IsNullOrWhiteSpace(input)) throw new ArgumentException(input);

        var instance = new MyClass();

        // Parse the string and populate the MyClass instance

        return instance;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Your solution was the first one and it is a simple one.
3

You could try something like this.

       public static MyCustomClass Parse(object o)
    {

        if (o == null)
            return null;

        try
        {
            if (o is MyCustomClass)
                return (MyCustomClass)o;
        }
        catch
        {
            return null;
        }
        return null;

    }

Comments

2
public Class MyCustomClass{
   public static MyCustomClass Parse(string str){
        if (str == "Whatever"){
             return new MyCustomClass();
        }
        else if (/*other conditions*/) {

        }
   }
}

Or, was there something else you didn't understand?

Comments

1

Absolutely - you would need to write a moderate amount of code to implement it, but you can certainly do it manually if you must:

public class MyClass {
    public string First {get; private set;}
    public string Last {get; private set;}
    public MyClass(string first, string last) {
        First = first;
        Last = last;
    }
    public static bool Parse(string s, out MyClass res) {
        res = null;
        if (s == null) return false;
        var tokens = s.Split(new[] {';'});
        if (tokens.Length != 2) return false;
        res = new MyClass(tokens[0], tokens[1]);
        return true;
    }
}

This version takes an output argument and returns bool; you can easily modify it to throw an exception and return MyClass "the factory method style".

Comments

1
public class foo
{
 public static foo Parse(object obj)
 {
  return new foo(obj);
 }

 public foo(){}

 public foo(object obj)
 {
  //your code to parse from obj to foo..
 }
}

in your 'frame/outer' code:

var t = foo.Parse(someInstance);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.