Solved

Using Alternate Class Constructors from Constructor

Posted on 2004-09-13
8
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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