Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Value: String to int - problem or feature?
Value: String to int - problem or feature? [message #36391] Wed, 23 May 2012 19:59 Go to next message
steffen is currently offline  steffen
Messages: 38
Registered: May 2007
Location: Denmark
Member

Hi,

Maybe I have a wrong understanding of how Value should work, but I keep getting Asserts on simple data conversions.

I have a VectorMap<String,Value> holding configuration data read from a section in an .ini file.
When using numeric values in this ini file I get the asserts.

Could anyone please explain to me why I cant do the following:
Value v = "122";
int i = v;

Re: Value: String to int - problem or feature? [message #36394 is a reply to message #36391] Wed, 23 May 2012 20:42 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Steffen,

Value is not meant meant for converting types. It is just container that can store various types. In most cases, you always get out the same type you put in. If there implicit conversion is possible, it will be done the same way as you can do e.g. <double> = <int>, but for incompatible types such as int and String, error occurs.

What you need to do is to call StrInt in between:
Value v = "122";
int i = StrInt(v);
If you are not sure what is in your Value variable, you can test it before the conversion using Is<>() method, e.g:
Value v = "122";
if (v.Is<int>()) { ... }
else if (v.Is<String>()) { ... };
Also, as a special case, you can convert any type of Value into String using v.ToString().

Did that answered your question?

Best regards,
Honza
Re: Value: String to int - problem or feature? [message #36396 is a reply to message #36394] Wed, 23 May 2012 21:13 Go to previous messageGo to next message
steffen is currently offline  steffen
Messages: 38
Registered: May 2007
Location: Denmark
Member

Thank you for the explanation.

I always thought Value was something like the Delphi Variant type.

I imagined it would use something like CParser::IsNumber if one was trying to convert a string to int.

The manual conversion is OK, I was just annoyed by having to manually convert it every time I looked something up in my configuration map.
I will add some overloaded array operators to my class instead.
My usage will be something like:
ClientSocket(socket, gConfig["Host"], gConfig["Port"])

Re: Value: String to int - problem or feature? [message #36405 is a reply to message #36394] Thu, 24 May 2012 08:56 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
dolik.rce wrote on Wed, 23 May 2012 14:42

Value v = "122";
if (v.Is<int>()) { ... }
else if (v.Is<String>()) { ... };



Also note the existence of IsNumber (returns true for int, double, int64 and bool) and IsString (returns true for String and WString), Value is the convertible to any of these 'compatible' types.

[Updated on: Thu, 24 May 2012 08:57]

Report message to a moderator

Re: Value: String to int - problem or feature? [message #36415 is a reply to message #36391] Fri, 25 May 2012 07:29 Go to previous message
steffen is currently offline  steffen
Messages: 38
Registered: May 2007
Location: Denmark
Member

OK, I think I understand the idea with the Value type now.

I ended up making an IniValue derived from String, with operators for int and bool. Now my config object can be used as I want it to.

Thank you both for the explanations.
Previous Topic: Out of memory panic
Next Topic: Serialize of derived classes
Goto Forum:
  


Current Time: Thu Mar 28 17:17:26 CET 2024

Total time taken to generate the page: 0.01105 seconds