473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

About constuctor arguments

Hello!

Here's a simple class.

class C
{
public int Num;
public C(int i)
{
Num = i;
}
public C(string s): this(int.Parse(s))
{
}
}

When I create an object..

C o = new C("1"); //this is ok.
C o = new C("a"); //this causes an error, of course.

I want to have some exception handling mechanism for the argument validation
in the class.
What's the common practice for such a situation?

Thanks.

Sam
Nov 16 '05 #1
2 2751

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:Ox**************@TK2MSFTNGP15.phx.gbl...
Hello!

Here's a simple class.

class C
{
public int Num;
public C(int i)
{
Num = i;
}
public C(string s): this(int.Parse(s))
{
}
}

When I create an object..

C o = new C("1"); //this is ok.
C o = new C("a"); //this causes an error, of course.

I want to have some exception handling mechanism for the argument
validation
in the class.
What's the common practice for such a situation?


In general, when you need to do something to a constructor argument, use a
private static function:

class C
{
public int Num;
public C(int i)
{
Num = i;
}
static int Parse(string s)
{
//validate s and throw an exception
return int.Parse(s);
}
public C(string s): this(Parse(s))
{
}
}

David
Nov 16 '05 #2
Sam,

If you want to have some sort of exception handling, you would have to
define a static method somewhere and then call that in the call to the other
constructor, like so:

private static int ValidateStringConstructorArgument(string s)
{
// Do something here, but return an int to pass to the overload of C
}

public C(string s) : this(ValidateStringConstructorArgument(s))
{}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:Ox**************@TK2MSFTNGP15.phx.gbl...
Hello!

Here's a simple class.

class C
{
public int Num;
public C(int i)
{
Num = i;
}
public C(string s): this(int.Parse(s))
{
}
}

When I create an object..

C o = new C("1"); //this is ok.
C o = new C("a"); //this causes an error, of course.

I want to have some exception handling mechanism for the argument
validation
in the class.
What's the common practice for such a situation?

Thanks.

Sam

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
1
by: Michael Shestero | last post by:
Hello. General description of my knotty problem: I have a code (a function f) on standart C++ that perform some smart action. This code works as console application getting user input from...
1
by: Bob Hairgrove | last post by:
Why can I do this: template<typename A, typename B=A> struct X { /*...*/ }; whereas this gives me an error about an undeclared identifier with MSVC++ 7.1: struct A { A(int arg1, int...
7
by: I wish | last post by:
http://www.eskimo.com/~scs/C-faq/q5.4.html It says "In particular, a cast may still be necessary before NULL (as before 0) in a function call argument." Does that mean if I want to pass a...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
12
by: onkar | last post by:
sometimes main accepts int main() and sometimes int main(int argc,char** argv) and sometimes int main(int argc,char **argv,char** env) still the code complies properly How is this possible ???...
18
by: cmk128 | last post by:
hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
4
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.