InvalidCastException thrown when trying to read STRING from GDS subscription

I am using the SampleRuntime example template to start the subscription service. I am able to read some other data types with no issue. Attempting to read a STRING logs the error.

[code]
04.02.22 11:06:37.993 root                      ERROR   - ReadSubscription - Arp Exception! Exception of type 'Arp::System::Commons::InvalidCastException' was thrown
Cannot copy value to argument: RscVariant contains data type String but arg is of type Struct
[/code]

This error makes it very obvious what is happening. Though I am unable to find a solution.

[b]example code:[/b]
Declared the C++ variable in the class header as [code]Plc::Commons::Gds::StaticString<80> m_strError;[/code]

GDS port variable declared in PLCnext Engineer in the program as
![](https://i.imgur.com/5YQPr2s.png)

Using AXC F 2152
Everything on version 2021.6

Hi Charkrit,

In your C++ code, please try:

[list]
[*] declaring your string variable as type [b]std::string[/b]
[*] assigning this variable using the [b]GetChars()[/b] method on the [b]RscVariant[/b], instead of the [b]GetValue()[/b] method.
[/list]

Ah I apologise. I was using the CopyTo() method and missed some of the documentation.

"[i]Copies internal data to memory referenced by value. Only for primitive types. Read Strings with GetChars. [/i]"

No need to apologise, I know that the documentation can be a bit tricky to decipher at times.

I hope it now works as expected.

Based on this question, I have now added a new issue to the CppExamples project on Github:
https://github.com/PLCnext/CppExamples/issues/41
... so (when resolved) this will hopefully help others who come across a similar issue in the futue.