1

在通过串口接收数据的后台工作人员工作期间,我需要返回字符串的四维数组。

在后台工作人员中:

    array<String^>^ Received = gcnew array<String^>(4);
    backgroundWorker1->ReportProgress(10,Received);

报告进度:

private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender,   System::ComponentModel::ProgressChangedEventArgs^  e)
{
    array<String^>^ Received2 = gcnew array<String^>(4);
    Received2 =(e->UserState);
}

我正进入(状态:

错误 C2440:“=”:无法从“System::Object ^”转换为“cli::array”

这并不奇怪,但是如何转换和分配UserStatestring array

4

1 回答 1

0

您需要将对象句柄转换为数组句柄,然后检查 nullptr 以查看转换是否成功,例如:

array<String^>^ Received2 = dynamic_cast<array<String^>^>(e->UserState);
if (Received2 != nullptr)
{
    // Cast was successful, safe to use Received2.
}
else
{
    // Cast failed, do not use Received2.
}
于 2013-06-25T19:47:40.497 回答