Solved

Using Alternate Class Constructors from Constructor

Posted on 2004-09-13
8
325 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 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now