|
-
Nov 21st, 2007, 03:45 PM
#1
Thread Starter
Frenzied Member
question about <undefined values>
Lets say I have this:
Code:
class classX
{
int name;
int age;
}
class classY
{
classX obj;
}
If I were to declare "classY y", and then put "y.name" as a parameter, it will crash because "y' was not initiated. Is there a way so that it does not crash? I know I could use "try...catch", but it could get messy and too many codes may have to be written. For instance, I would have to first check to see that "y" is not null, then use y.name as a parameter. But what happens if its like this:
class classA
{
int property1;
}
class classB
{
classA obja;
}
class classC
{
classB objb;
}
class classD
{
classC objc;
}
And "classD d" is declared, and I want to use "d.objc.objb.obja.property". I would have to make sure that each layer of class is initiated so that when i use "d.objc.objb.obja.property", it does not crash. Now is there a way or nice algorithm so that I won't have to check each class if they are initiated.
-
Nov 22nd, 2007, 03:14 AM
#2
Re: question about <undefined values>
If you're going to lazy load values, I'm afraid you're all out of luck. There is, quite simply, no "quick fix" that would not impose additional, unnecessary overhead.
As to the problem itself, rather than manage application flow with exception handling, use conditions:
Code:
if (y == null)
// Initialise "y".
// Do something with "y".
-
Nov 22nd, 2007, 04:19 PM
#3
Re: question about <undefined values>
This is what constructors are for.
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|