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 |
|
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 |
|
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 |
|
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 |
|
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
|
|
|
|
Goto Forum:
Current Time: Sat May 04 10:22:04 CEST 2024
Total time taken to generate the page: 0.02258 seconds
|