Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Constructor overloading
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00996906
Message ID:
00997041
Views:
13
>Chad and Cetin,
>Both of you answered the same thing within minutes of each other again.
>I figured it would be something simple like that.
>Have either one of you (or anyone else for that matter) tried something similar to that. The reason I am asking is because I am wondering what will happend if an exception is thrown by the method (I know ToString() doesn't throw too many exceptions but if it was the other way around and I wanted to convert to an int and I passed a non numeric character so Convert.ToInt32() would throw an exception.
>
>Hmm I guess I could have tested this in the time it took me to post this question, but I am also interested in finding out if that type of constructor overloading is common and/or reccomended.
>
>Einar
>
>>public myConstructor(int para0):this(para0.ToString()) {}

Einar,

I happen to like overloading, but on something that might throw an exception, I would probably have another method (a set method perhaps) that stored the value the way I wanted it and optionally overload that method as well. That way, in this case anyway, I wouldn't need to have the constructors call each other. Something like this:
private int someValue;

public myConstructor(int para0)
{
  this.setValue(para0);
}

public myConstructor(string para0)
{
  this.setValue(para0);
}

public myConstructor(bit para0)
{
  this.setValue(para0);
}

public setValue(int para0)
{
  this.someValue = para0;
}

public setValue(string para0)
{
  try
  {
    this.someValue = para0.ToInt();
  }
  catch
  {
    this.someValue = 0;
  }
}

public setValue(bit para0)
{
  this.someValue = (int)para0;
}
OR something like this:
private int someValue;

public myConstructor(int para0)
{
  this.setValue(para0);
}

public myConstructor(string para0)
{
  try
  {
    this.setValue(Int.Parse(para0).Value);
  }
  catch
  {
    this.setValue(0);
  }
}

public myConstructor(bit para0)
{
  this.setValue((int)para0);
}

public setValue(int para0)
{
  this.someValue = para0;
}
Of course, you might want more error handling and I can't remember the right ways to parse ints right now, but I hope you get the jest of it.

HTH,
Chad
_________________________________
There are 2 types of people in the world:
    Those who need closure
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform