Visual C++ 2010 -> Window Form. How to convert string to int? - c++-cli

Hey! I have textBox with text like "12:30" and this code textBox -> Text -> ToString() -> Split(':')[1] It return "30" as string. And I want convert it to Int. How? I founded function like Convert::ToInt32() etc, but it doesnt work for my c++ (Visual C++ 2010 -> Winfow Form). Help me plz! (I started learn c++ 2 days ago)
And i use Managed C++

As you're using Managed C++, then you can do this:
double foo = System::Convert::ToDouble("200");
int bar = System::Convert::ToInt32("200");
Use whatever you need!

you can use c standard lib frunction atoi
CString s = "30";
int x = atoi( s ); // x is now 30
Edit: Oh, your are using managed C++, then one of the following two should do the job
System::Convert::ToInt32(str, 10);
System::Int32::Parse(str);
Refer to this page with an example: http://msdn.microsoft.com/en-us/library/b3h1hf19.aspx

I use
int intVar = Int32::Parse(stringVar);

Related

How to convert a System::Char to a C++ char

I need to convert a System::Char that comes from the .NET world into a char in the C++/CLI world (a native C++ type).
I am confident that the incoming character is a regular ASCII character, just in the .NET Unicode world. So I never will have to deal with a character that doesn't have a direct mapping to char.
How about converting it directly?
System::Char neta = System::Char('b');
char c = neta;
I have some code working, but it is officially... very ugly. I'm hoping someone else will add an answer to this question that has a better solution than this!
// I am given neta externally
System::Char neta = System::Char('b');
// Here is the conversion code - is there a better way?
System::String ^str = gcnew System::String(neta,1);
auto trans = Marshal::StringToHGlobalAnsi(str->ToString());
auto cstr = static_cast<const char*>(trans.ToPointer());
char c = *cstr;
Marshal::FreeHGlobal(trans);
// c contains the expected answer 'b'.
Many thanks!

WritePrivateProfileString with int variable as value

I'm quite new to C++.
Right at the moment I'm trying to write and read an ini file. As I'm working with VS 2010 I d'like to use the WritePrivateProfileString function.
testwise it works like this
WritePrivateProfileString("testsection","testkey","testvalue","testfile.ini");
I can run this successfully.
My idea now is to hand overthe value within a variable of course which represents an int value.
WritePrivateProfileString("testsection","testkey",testvalue,"testfile.ini");
I understand that I would have to cast it first to LPCSTR or char so it works.
int testvalue =12;
WritePrivateProfileString("testsection","testkey",intToChar(testvalue),"testfile.ini");
//convert int to LPCSTR
char* intToChar(int temp){
char wert[8];
itoa(temp, wert, 10);
cout << wert;
return wert;
it does write to the file. but instead of a number i get random signs like "<ยบ-" and stuff.
I've been trying for an hour now. Using different convertion (to LPCSTR, to char, to string) without any success.
what am I doing wrong? I really stat to get desperate.
thanks for any hint.
}
WritePrivateProfileString function can handle only "string", not "Integer Value".
You can make string from integer using itoa().

How to add text plus the text written from a Parameter type C in ABAP?

I am working in an ABAP program and I have a question.
For example in C# when we have a String variable: string name; , and we want this to be filled with some data from a textbox but also add some ohter text.
For example:
string name = "Hello: " + textBox1.text;,
And I want to ask you how can I do this in ABAP ??? How to add text plus the text written from a Parameter type C?
CONCATENATE and the concatenate operator && will do it as answered by Jagger and vwegert. To do it with string expressions, you use the below where name is the screen field or whatever that has the name in it (it doesn't need to be a field-symbol):
greeting = |Hello: { <name> }|.
String expressions are extremely useful as they can be used to build up complex values without declaring extra variables - e.g. they can passed as directly as function module or method parameters without first assigning to a local variable.
You can either use the CONCATENATE keyword or -- in newer releases -- string expressions. Be sure to check the online documentation and sample programs available using the transaction ABAPDOCU, it will save you a ton of seemingly basic questions.
The equivalent operator is &&.
So in your case it would be:
name = 'Hello: ' && textBox1->text.

Does the += operator just not exist in VBA?

I'm trying to incriment the value in a cell, but despite documentation saying Visual Basic allows the += operator, it's just giving me "Compile error: Expected: expression".
Range("CellName").Value += 1
Is what's breaking, but if I do
Range("CellName") = Range("CellName") + 1
It works fine
No, it doesn't exist in VBA.
VB.NET might take += (though I'm not even sure about that).
You'll have to use
Range("CellName").Value = Range("CellName").Value+1
A good reference can be found here
An "official" list of VBA-operators can be found in VBA help here:
Help --> Microsoft Visual Basic Help --> Visual Basic for Applications Language Reference --> Visual Basic Language Reference --> Operators --> Arithmetic Operators (online here )
Maybe controversially, but the next advice would have saved me lots of time and headaches:
I'd say it's better to forget everything about VB and focus at VBA, as the two are different languages. People don't mention "Oh, OCaml, APL, PHP or Fortran have increment operators", as it's off-topic if the scope is VBA. In the same way, what VB has or has not is off-topic, and usually it only adds to the confusion because it's resemblance. Better use the MS-Office provided "local" Visual Basic for Applications Language Reference as provided by the help system, or online:
http://msdn.microsoft.com/en-us/library/office/gg264383%28v=office.15%29.aspx
It's really annoying. I made the following functions to make my life a bit easier:
' ++
Function pp(ByRef x, Optional y = 1)
x = x + y
pp = x
End Function
' --
Function mm(ByRef x, Optional y = 1)
x = x - y
mm = x
End Function
Doesn't really help your situation as you'd still have to type the same amount:
Range("CellName") = pp(Range("CellName"))
but for simple loop increments it helps :
do while cells(x,1) <> ""
pp x
loop
No it does not have it for numbers, e.g. see http://msdn.microsoft.com/de-de/library/215yacb6%28v=vs.80%29.aspx / http://msdn.microsoft.com/en-us/library/cey92b0t.aspx

WxWidgets using wxString

I'm taking a computer sciences class and we have to make a calculator using a GUI. I have been banging my head into a wall trying to even get input though.
Understand how to use pointers and GetValue(), to take in the input as a wxString but that does me no good, If I can't get the string as a double or integer then I can't perform operations on it.
Does anyone know how to convert wxString to Double? or even int?
Thanks in Advance
For this purpose, there are the two functions ToDouble and ToLong.
For details take a look at the offical documentation:
http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtolong
http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtodouble
EDIT: Example:
wxTextCtrl ctrl;
// user has entered a number
double number;
if( !ctrl.GetValue().ToDouble( &number ) )
// handle error
else
// continue...
Please note: It will only work if you enter a number. If you enter a term like 2+3 the function should return false. In this case you have to split the string up and interpret all numbers seperately.