-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,514
  • 3
  • 28
  • 35
dwb
  • 475
  • 6
  • 31
  • Well, `UpTime.Text` obviously expects a normal `string`, not an array (as the error tells you) - it's unclear what your question is – UnholySheep Nov 27 '17 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? – maccettura Nov 27 '17 at 21:37
  • 1
    Possible duplicate of [convert string array to string](https://stackoverflow.com/questions/4841401/convert-string-array-to-string) – mjwills Nov 27 '17 at 21:56

3 Answers3

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.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
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];
scwagner
  • 3,975
  • 21
  • 16
  • 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! – dwb Nov 27 '17 at 21:40
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.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98