Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Alternate Class Constructors from Constructor

Posted on 2004-09-13
8
Medium Priority
?
334 Views
Last Modified: 2010-04-15
I have something like the following:

class a
{
    public a (int v)
    {
        // Do Something with int
    }

    public a (string v)
    {
       
    }
}

I want to call the constructor which takes the string which needs to try to parse the integer.  If parsing fails it'll use a fall-back value.  Then it'll call the int constructor.

The answer is not something like

    public a (string v) : this(int.Parse(v))
    {
       
    }

because it isn't fault-tolerant
0
Comment
Question by:a_goat
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12044815
please see code below

public a (string v) : this(int.Parse(v))
    {
        try
        {
           this(int.Parse(v));
           return;
        }
        catch(FormatException fe)
        {
        }
        //doing you want to string constructor
    }
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 750 total points
ID: 12044886
class a {
      private static int ParseIntOrDefault(string value, int def) {
            try {
                  return int.Parse(value);
            } catch () {
                  return def;
            }
      }
      
    public a (int v) {
        // Do Something with int
    }

    public a (string v): this(ParseIntOrDefault(v, 0)) {}
}
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12044896
Sorry, should only be "} catch {" without the parantheses...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:tzxie2000
ID: 12045098
I read your question more careful and modify my comment to fit your require

class a
{
   public a(int v)
  {
  //doing init
  }
  public a (string v)
  {
        try
        {
           this(int.Parse(v));
        }
        catch(FormatException fe)
        {
           //set  fall-back value to v
           this(fall-back value);
          //or just call this(0); as any number like 0 is your fall-back value to call.
        }
    }
}
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12045118
tzxie2000, you cannot call another constructor from within the constructor body. This is only possible before the body of the constructor.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12045262
I am very sorry for that mistake
I think the code should be below

class testclass
{
  public testclass(int v):this(v,"",false){}
  public testclass(string v):this(0,v,true){}
  private testclass(int v,string s,bool fromstring)
  {
     if(fromstring)
     {
        try
        {
              v=int.Parse(s);
        }
        catch
       {
           v=0;//set to your fall-back value
        }
    }
    //doing real init;
  }
}
0
 
LVL 3

Author Comment

by:a_goat
ID: 12045641
I was hoping for something a littel more elegant, but good idea.  I hadn't thought of that one
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12045660
a_goat, thank you for grading the question. But may I ask what you would have expected to give a higher grade?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question