Share via

c# cast problem

Lloyd Sheen 1,491 Reputation points
2022-09-04T16:08:12.3+00:00

Really boggled.

Code
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string[] ss = (string)value.Split("|");

This throws a compile error. Now since I am casting the field "value" I cannot see why the Split method is not useable. I can simply case value to another field defined as string and then use that field in the Split method. Why can I not do the whole thing in one statement?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2022-09-05T01:34:07.58+00:00

Hi @Lloyd Sheen ,
(string) is converted last.
So string[] ss = (string)value.Split("|");, What actually happens is the following steps.

string[] a = value.Split("|");  
string[] ss = (string)a;  

It is recommended to use your step-by-step code, or try string[] ss = value.ToString().Split("|");.

Best Regards.
Jiachen Li

----------

If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Muhammed Şeker 76 Reputation points
    2022-09-05T10:34:43.867+00:00

    can you try this?

    string x=value.ToString();
    string[] ss = value.Split("|");

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.