-2

I have used Splits in the past, but this one is a bit different for some reason, and I am not sure why...

Code:

string responceuptime = scripting.ReadUntilPrompt();
string[] suptime = responceuptime.Split('s');
UpTime.Text = suptime;

Error:

cannot implicitly convert type string[] to string

maccettura
10.9k3 gold badges31 silver badges37 bronze badges
asked Nov 27, 2017 at 21:33
3
  • Well, UpTime.Text obviously expects a normal string, not an array (as the error tells you) - it's unclear what your question is Commented Nov 27, 2017 at 21:36
  • I imagine UpTime.Text is a string property right? What happens when you assign an array of string to a string property? Why would you expect that would work? Commented Nov 27, 2017 at 21:37
  • 1
    Possible duplicate of convert string array to string Commented Nov 27, 2017 at 21:56

3 Answers 3

3

That is very basic thing and is very easy to figure out from the error message what is wrong actually.

The following line is the culprit by the way here:

UpTime.Text = suptime;

As suptime is of type string[] which is array while Text property is of type String. When assigning references to and from the type should be same otherwise we will see this error message which you just facing.

It's unclear from the above lines of code that what you are trying to achieve here, but you would need to assign single String object to Text, you cannot assign array or collection to single String object.

Hope it helps.

answered Nov 27, 2017 at 21:37
Sign up to request clarification or add additional context in comments.

Comments

1

Your variable suptime is a string[] - an array of strings. While I don't know what Uptime.Text is, I'm guessing that it's looking for a single string, and that's why you're getting the compiler error that you are.

If you want to get the first string out of the array, then you could set it like so:

UpTime.Text = suptime[0];
answered Nov 27, 2017 at 21:36

1 Comment

Nice and simple fix. You are correct, it was looking for a single string, and I spaced it out. I am a C# noob! I will mark as answered when the timer is done. Thanks for the help!
1

The output of a call to String.Split is an array of strings (String[]). What your code does, here, is attempting to assign a String[] to a String variable, therefore the application is throwing an exception.

Hence, you must identify, within your array, the value you are looking for and picking the index that points to it (from 0 to suptime.Length - 1). For example:

UpTime.Text = suptime[0]; // first value of the array
UpTime.Text = suptime[2]; // third value of the array
UpTime.Text = suptime[suptime.Length - 1]; // last value of the array

If the result of your split is:

{"A" "Z" "11:57"}

and you want your UpTime.Text to be filled with something that looks like a time value, it's kinda obvious that the value you must pick is the third one.

answered Nov 27, 2017 at 21:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.