Output string array to listbox - c#

I have created a string array named 'coin' and I am trying to output the content of the string array to a item box along with another integer variable, and I keep getting the list box saying String[] Array.
To create the array, I have used the following:
string[] coins = new string[4];
Then on the click of the enter button, a new item will be added to the array, along with the count variable which increments on the click of the button, then it outputs the contents of the coin array to the list box:
coins[valuesEntered] = "coin" + valuesEntered.ToString();
valuesEntered++;
listBox1.Items.Add(coins);
However, this seems not to work and instead just outputs String[] Array to the group box each time the enter button is clicked. I cant seem to get past this brick wall at the moment.

You are attempting to add an object to your ListBox, specifically a string array represented in syntax as String[] Array. You want the contents of the array, not the array itself.
You first need to join the individual contents of the array to a separate string variable before you attempt to add it to the ListBox.
string listOfCoins = string.Join(" ",coins);
listBox1.Items.Add(listOfCoins.ToString());
this might work too
listBox1.Items.Add(coins.Text);
I don't use Visual C# much, so take this with a grain of salt.

Related

String Splitting another string

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
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.
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];
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.

Write a function filterLongWords() that finds and displays all words that are longer than a given integer i in C#

I'm new to programming I have this exercise where I have to Write a function filterLongWords() that finds and displays all words that are longer than a given integer i in a list that has been passed in as an argument and I cant display the words more than once and I don't know how to write it if any one here could tell me how or tell me a website I can go to for help that would be ok too.Thanks
A hint for u....
Pass a string array to that List<string> filterLongWords(string[] words) function.
Instantiate a list that you will return at the end.
Implement a foreach loop that iterates that array.
Now you look at the length of that current iterated word and if it doesn't fit your length put it in your instantiated list.
Return your instantiated list or convert it to an array and return that.
I won't code it for you - it's just a hint.

syntax error in my foreach loop as it looks through an Array of int Arrays?

So, I have an array that contains four int arrays, I also have an int array that holds player input. I want to compare the player Input array against each of the int Arrays within the first Array. It was recommended to use a foreach loop however I cannot get it to work, this is what it looks like at the moment:
foreach(int[] i in spellList[i][])
{
Debug.Log("One Array");
}
So as I understand it it is saying for every int array in spellList[in the first array][] print One Array in the console.
I would also like to use the local variable i to compare it against another array which in theory should be simple as I can just say if(i = inputArray) as it runs the foreach however I can't get that far.
Thanks and sorry if this seems simple I'm just getting into loops that aren't just the update function!
Edit:
So having resolved the syntax error and changing my code to the code below I have a new issue, it just isn't responding, I know it should as I have debugged it multiple times through the console:
foreach(int[] i in spellList)
{
if (spellCapture == i)
{
Debug.Log("match");
}
}
Now i know that at least once the array of ints i is equal to the stored array of ints that takes user input in spellCapture but it wont run the debug.log...

Display the first char in a string array?

I am making a simple learning game where a number is generated and the user will have to enter the word. The problem is Im trying to get a hint button working basically when the user clicks the hint button a message box will display the first character of the string which is in the array.
Here is a example of what my array looks like.
static string[] numberList = { "one","two", "Three","four","five","Six","Seven","Eight","Nine","Ten"};
So if the number is 2 and the strings are always in that order, then you can do
var firstletter = numberList[2-1][0];
That will get you a Char. If you want that also as string then do
firstletter.ToString();
just like this
var first = numberList[2-1][0];
first.ToString();
I don't know if this is optimized, but I like using substring() because it's very obvious what I want.
numberlist[i-1].Substring(0, 1);
where 'i' is the number you are looking for (subtract 1 because arrays use a 0-based index)

Making a jukebox, adding track list to list box from .text file

Right I am relatively new to programming and I am currently in the middle of my second assignment which is to create a jukebox using arrays, list boxes, media player, text box's, timers and menustrip. I have a number of text files that are laid out like this
2
Other
Big Bad John (1961) - Jimmy Dean.mp3
My Ding A Ling - Chuck Berry.mp3
The first line is the amount of tracks and the second is the genre of the tracks the rest is the actual tracks, what I need to do is display the tracks in a list box and when I use the scroll bar of the list box have it display the next set of tracks from the following genre.
The following is a small piece of my code,
I currently have some global's
public string MediaPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
public int NumberofGenre;
ListBox[] mediaLibrary;
and then I use the number of genre for the columns of the array if i have done it correctly.
private void JukeBox_Load(object sender, EventArgs e)
{
StreamReader inputStream = File.OpenText(#"C:\Users\James Dunn\Documents\Visual Studio 2012\Projects\Assignment 2(1)\Assignment 2\MyJukeBox\bin\Genre amount.txt");
NumberofGenre = Convert.ToInt32(inputStream.ReadLine());
mediaLibrary = new ListBox[NumberofGenre];
}
If anyone can give me any help in displaying the tracks that are listed in my text file in my listbox, it would be much appreciated. It will likely require me to skip the first 2 lines as i only want to display the track names. I have spent a days going over lectures and the general internet and I am at a loss.
For a single file you would do these things:
1 read all lines into a string array
2 (optional) check if the total number of lines is lines1 + 1
3 (optional) show line2 in a label
4 for each remaining line add it to the listbox
note: line1 is lines[0] etc..
for a whole list of files repeat for each file and listbox.
Here is a starter:
1 - string[] lines = File.ReadAllLines("firstFile.txt");
..
4 - for (int l = 2; l < lines.Length; l++) mediaLibrary[0].Items.Add(lines[l]);
(Don't worry about global variables; at your current level you probably don't even know how to create them. Your variables are class variables and when used with care perfectly OK..)

Categories

Resources