Jump to content
  • Advertisement
Sign in to follow this  
toddhd

Need advice on return value

This topic is 5469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this is a simple one, but my brain is not working this morning for some reason... :) I have a class that reads/writes Ini files. I'm adding a function to read an integer value (as opposed to a string) from the file, and return that int value. Here's the thing - I have some code in place that will do some basic validation - it makes sure that a value exists, and that the value is numeric, and so on. The thing is, if there is a problem, I'm not sure what to return to indicate a problem occured? For example, the code may call the function like this:
int myValue = CIniFile::GetValueInt("MySection","MyKey",FileName);
Normally I'd say to just return a -1 to indicate a failure, but its possible (this being an Ini file) that the file may contain positive or negative or zero values. What I'd really like is some way to return a NULL or non-value of some type. I just can't figure out how to do it. What do you reccomend?

Share this post


Link to post
Share on other sites
Advertisement
There are several ways to do error handling:
* throw an exception
* pass a reference to your out paramter and return an error code
* have a global error flag that must be checked after the call
* invent your own method of error handling... ;-)

Share this post


Link to post
Share on other sites
or


class CIniFile {
enum IniFileError {IniFileErrorOkay, IniFileErrorImvalidValue};
IniFileError GetValue(const std::string& section,
const std::string& key,
const std::string& filename,
int& value) {
//etc.
}
};




Share this post


Link to post
Share on other sites
Why not return a default value that is passed as argument?
For example:

int CIniFile::GetValueInt(char * section_name, char * key_name, char * file_name, int default_value = 0);

if MySection or MyKey doesn't exist, the function will return default_value (by default: 0).

You can also put a variable in your class to indicate error:

class CInitFile
{
private:
int m_s32Error;
...
public:

int GetLastError() { return m_s32Error; }
};

m_s32Error contains error code (0= Ok, -1=invalide ini file, ...)
In you class methods, set the error to the proper value.

hope it helps a lot.

Share this post


Link to post
Share on other sites
Thanks for the input. I can't really implement a Class error, because the class contains all static members (think of a std::string or std::vector for example). Also, I don't want to throw an error, as it is not very graceful, and would crash a program over a simple error.

I decided to take the imperfect but fairly reasonable alternative of returning INT_MAX as the bad value. That way, pretty much everything will work, and by using INT_MAX, it should still work on any compiler.

Share this post


Link to post
Share on other sites
Quote:
Original post by sumydi
Why not return a default value that is passed as argument?


Hey, that's a good idea too :)

Share this post


Link to post
Share on other sites
Quote:
Original post by toddhd
Also, I don't want to throw an error, as it is not very graceful, and would crash a program over a simple error.
Typically, thrown exceptions (especially ones that you throw) are meant to be caught, not ignored. Which means it would not crash your program.

Share this post


Link to post
Share on other sites
Quote:
Original post by toddhd
I can't really implement a Class error, because the class contains all static members (think of a std::string or std::vector for example). Also, I don't want to throw an error, as it is not very graceful, and would crash a program over a simple error.


I don't see the point... Most of std::string and std::vector members are not static. I really don't think it's a good idea to have your class static.

Do you close and reopen your file every time you want to read a value?
Do you have static members or only static methods?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!