.NET C ++ WinForm项目中的字符串 [英] Strings in .NET C++ WinForm project

查看:73
本文介绍了.NET C ++ WinForm项目中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自C#开发,但我必须在

C ++中创建一个.NET WinForm应用程序。

我在处理C ++中的字符串时遇到了麻烦,这似乎比C#中的问题要多得多。

让我说我想构造一个这样的字符串,它结合了字符串

变量,普通来自WinForm控件的字符串和字符串:


string startStr ="<刚开始>" ;;

string newStr = startStr + myControl1-> Text +进一步 +

myControl2->文字+结束;


在C#中这没问题,但在C ++中我遇到各种错误。

关于如何处理这个的任何建议,将不胜感激。


谢谢,

Kim

解决方案

我来自C#开发,但我必须在
中创建一个.NET WinForm应用程序


C ++。

我在处理C ++中的字符串时遇到了一些麻烦,这似乎比C#中的问题要多得多。

让我说我想要构造一个这样的字符串,它组合了字符串

变量,普通字符串和来自WinForm控件的字符串:


string startStr ="<刚刚开始>" ;;

string newStr = startStr + myControl1-> Text +进一步 +

myControl2->文字+结束;


在C#中这没问题,但在C ++中我遇到各种错误。

如何处理此问题的任何建议,将不胜感激。



如果您实际发布编译器错误会有所帮助。我的水晶

球目前正在进行维修。


但是我仍然可以给你以下提示:


1)C ++中的字符串用于STL字符串类。你在CSS中调用字符串
C#实际上是C ++ / CLI中的System :: String。这可以保证在你的问题中至少产生一些部分。


2)C ++可以处理ASCII和UNICODE字符串。

你好是ASCII,LHello是unicode。尝试为你的字符串文字添加前缀

with L


-


亲切的问候,

Bruno。
br ***** *****************@hotmail.com

仅删除_nos_pam




" Bruno van Dooren" < br ********************** @ hotmail.comskrev i en

meddelelse新闻:B5 ******* *************************** @ microsof t.com ...


>我来自C#开发,但我必须在C ++中制作一个.NET WinForm应用程序。
我有一些麻烦处理C ++中的字符串似乎比C#更有问题。
让我说我想构造一个这样的字符串,它结合了字符串
变量,普通字符串和来自WinForm控件的字符串:

string startStr ="<刚开始>" ;;
字符串newStr = startStr + myControl1->文字+越来越多 +
myControl2->文字+结束;

在C#中这没问题,但在C ++中我遇到各种错误。
关于如何提出建议处理这个,将不胜感激。



如果您实际发布编译器错误会有所帮助。我的水晶

球目前正在进行维修。


但是我仍然可以给你以下提示:


1)C ++中的字符串用于STL字符串类。你在CSS中调用字符串
C#实际上是C ++ / CLI中的System :: String。这可以保证在你的问题中至少产生一些部分。


2)C ++可以处理ASCII和UNICODE字符串。

你好是ASCII,LHello是unicode。尝试为你的字符串文字添加前缀

with L


亲切的问候,

布鲁诺。



我更喜欢你如何在C ++中实际处理组合字符串,而不是

实际的编译器错误。

但是让我说我​​使用std :: string,它类似于string在C#和我这样做:


std :: string baseStr ="< Hello>" ;;

textKeyFile-> Text = baseStr.c_str ()+ textCustomerName->文字;


然后你得到一个不能添加两个指针错误,因为两者都是指针

显然。或者如果你这样做:


std :: string baseStr ="< Hello>" ;;

textKeyFile-> Text = baseStr + textCustomerName- >文字;


你得到其他错误,因为这两个字符串是不同的类型。


所以我想我要问的是什么类型的C ++字符串应该在我开始将它们全部添加到一起之前将所有字符串转换为



/ Kim




Kim Hellan写道:


>

我更喜欢如何你实际上处理C ++中的组合字符串,而不是

实际的编译器错误。



是的,但是编译错误会帮助我们理解你的问题!!


但是我可以说我使用std :: string,它类似于string在C#



不,它不是! std :: string是本机代码,ansi字符串(不是Unicode)

并且有一个与System :: String完全不同的API。如果你想要

来处理WinForms(以及因此maanged代码)坚持使用

System :: String ^。


>我这样做:


>

std :: string baseStr ="< Hello> ;" ;;

textKeyFile-> Text = baseStr.c_str()+ textCustomerName-> Text;


然后你得到一个无法添加两个指针错误,因为两者都是指针

显然。



这里有2个错误:

- c_str()成员函数返回一个const char *。从那里开始,你就可以进入原生指针领域了,而指针运算将取代

通常。字符串连接语义。

- 什么是textCustomerName?我怀疑这是一个.NET对象,因此

因此textCustomerName-> Text是一个System :: String。你不能

简单地连接std :: string和System :: String ^ !!

请改为尝试:


System :: String ^ baseStr ="<你好>英寸; //构建一个maanged字符串

textKeyFile-> Text = baseStr + textCustomerName-> Text; //完全与在C#中相同的



Arnaud

MVP - VC


I come from C# development, but I have to make a .NET WinForm application in
C++.
I''m having some troubles handling strings in C++, which seems a lot more
problematic than in C#.
Lets say I want to construct a string like this which combines string
variables, plain strings and strings from WinForm controls:

string startStr = "<just starting>";
string newStr = startStr + myControl1->Text + "getting further" +
myControl2->Text + "the end";

In C# this is no problem, but in C++ I get all kinds of errors.
Any advice on how to handle this, would be much appreciated.

Thanks,
Kim

解决方案

I come from C# development, but I have to make a .NET WinForm application in

C++.
I''m having some troubles handling strings in C++, which seems a lot more
problematic than in C#.
Lets say I want to construct a string like this which combines string
variables, plain strings and strings from WinForm controls:

string startStr = "<just starting>";
string newStr = startStr + myControl1->Text + "getting further" +
myControl2->Text + "the end";

In C# this is no problem, but in C++ I get all kinds of errors.
Any advice on how to handle this, would be much appreciated.

It would help if you would actually post the compiler errors. My crystal
ball is in for repairs at the moment.

But I can still give you the following hints:

1) string in C++ is used for the STL string class. What you call string in
C# is actually System::String in C++/CLI. This is guaranteed to cause at
least some part of your problems.

2) C++ can handle both ASCII and UNICODE strings.
"Hello" is ASCII, L"Hello" is unicode. Try prefixing your string literals
with L

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"



"Bruno van Dooren" <br**********************@hotmail.comskrev i en
meddelelse news:B5**********************************@microsof t.com...

>I come from C# development, but I have to make a .NET WinForm application
in
C++.
I''m having some troubles handling strings in C++, which seems a lot more
problematic than in C#.
Lets say I want to construct a string like this which combines string
variables, plain strings and strings from WinForm controls:

string startStr = "<just starting>";
string newStr = startStr + myControl1->Text + "getting further" +
myControl2->Text + "the end";

In C# this is no problem, but in C++ I get all kinds of errors.
Any advice on how to handle this, would be much appreciated.


It would help if you would actually post the compiler errors. My crystal
ball is in for repairs at the moment.

But I can still give you the following hints:

1) string in C++ is used for the STL string class. What you call string in
C# is actually System::String in C++/CLI. This is guaranteed to cause at
least some part of your problems.

2) C++ can handle both ASCII and UNICODE strings.
"Hello" is ASCII, L"Hello" is unicode. Try prefixing your string literals
with L

Kind regards,
Bruno.

I was more after how you actually handle combined strings in C++, not the
actual compiler errors.
But lets say I use std::string, which resembles "string" in C# and I do:

std::string baseStr = "<Hello>";
textKeyFile->Text = baseStr.c_str() + textCustomerName->Text;

then you get a "Cannot add two pointers" error, because both are pointers
obviously. Or if you do:

std::string baseStr = "<Hello>";
textKeyFile->Text = baseStr + textCustomerName->Text;

you get other errors since the two string are different types.

So I guess what I''m asking is, what type of C++ string should all strings be
converted to before I start adding them all together?

/Kim



Kim Hellan wrote:

>
I was more after how you actually handle combined strings in C++, not the
actual compiler errors.

Yep, but the compiler errors would help us understand your problem!!

But lets say I use std::string, which resembles "string" in C#

No it doesn''t! std::string is native code, ansi string (not Unicode)
and has an API that is quite different from System::String. If you want
to deal with WinForms (and therefore maanged code) stick with
System::String^.

>and I do:

>
std::string baseStr = "<Hello>";
textKeyFile->Text = baseStr.c_str() + textCustomerName->Text;

then you get a "Cannot add two pointers" error, because both are pointers
obviously.

There are 2 errors here :
- the c_str() member function return a const char*. From there, you''re
into the realm of native pointers, and pointer arithmetic will replace
"usual" string concatenation semantic.
- What is textCustomerName? I suspect this is a .NET object, and
therefore textCustomerName->Text is a System::String. You cannot
"simply" concatenate a std::string and a System::String^ !!
Try this instead:

System::String^ baseStr = "<Hello>"; //build a maanged string
textKeyFile->Text = baseStr + textCustomerName->Text; //exactly the
same as in C#!

Arnaud
MVP - VC


这篇关于.NET C ++ WinForm项目中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆