Solved

Using Alternate Class Constructors from Constructor

Posted on 2004-09-13
8
323 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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