7 Replies - 17531 Views - Last Post: 22 April 2008 - 09:13 PM Rate Topic: -----

#1 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Identifier expected

Posted 21 April 2008 - 12:14 PM

I can't figure out what identifier to use, and where:

				  //this code will execute every time the user presses enter. when they are finished, i should contain 
				 //the number of names and the array should hold each name

							Array names1=new Array[10];
for(int i; i < names1.Length; i++)
{
//the following = gives the error message "identifier expected"
names1[] = TextBox1.Text.ToString();
}
	 



I am trying to sort a list of two to ten names, typed into a text box . The user will press enter after each name
is typed in. When they click a button, the list is supposed to be sorted automatically and then appear in alphabetical
order in the listbox. I am trying to assign each name to an array as the user presses the enter button.
(we don't know how many names will be typed in, but I made the array to hold 10 since that is the maximum number
of names allowed in the list). I know this is kinda sloppy. It probably could be much more efficient.

If you need to see more of the code I can include all of it...or a larger part of it.

Is This A Good Question/Topic? 0
  • +

Replies To: Identifier expected

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Identifier expected

Posted 21 April 2008 - 12:44 PM

You need to provide an index number to assign a value to an array element. Use the i from your For loop.
names1[i] = TextBox1.Text.ToString();

Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Identifier expected

Posted 21 April 2008 - 10:45 PM

Thank you. I will do that. I will let you know if the rest of the program works. I will probably be working
on it again tomorrow.
I'm fairly new at this, and I appreciate all the help I can get.
:)

This post has been edited by OliveOyl3471: 21 April 2008 - 10:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Identifier expected

Posted 22 April 2008 - 09:49 AM

Now it gives other errors instead of "identifier expected"

Error 1 Cannot apply indexing with [] to an expression of type 'System.Array'

Error 2 The name 'TextBox1' does not exist in the current context

I think I could fix the second error by using a method. Is that right?
I think I could NOT use the array at all, just sort the items that are in the listbox using listBox1.Items[index].

Am I on the right track with this? If so, then I think this is beginning to make sense to me. :)

This post has been edited by OliveOyl3471: 22 April 2008 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Identifier expected

Posted 22 April 2008 - 11:00 AM

Oops, i didn't notice that you are declaring an Array object. Declare the array with a specific data type, not an Array object.

string[] names1=new string[10];


The Array class provides methods for creating, manipulating, searching, and sorting arrays, it is not meant to be instantiated.
Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Identifier expected

Posted 22 April 2008 - 12:33 PM

Thank you Jayman. I will try to remember that when I use arrays.

Now I think I do not need the array after all.

I am trying to sort the contents of the listbox instead of adding the names to an array and then sorting the array.

This seems to be better, but it still has errors:


		//create sort method to sort the contents of listbox
		public string sort()
		{
			int nameList = listBox1.Items.Count;
			object temp;
			for (int x = 0; x < nameList; x++)
			{
				for (int y = x; y < nameList; y++)
				{
					if (string.Compare(listBox1.Items[x], listBox1.Items[x + 1]) > 0)
					{
						temp = listBox1.Items[x];
						listBox1.Items[x] = listBox1.Items[y];
						listBox1.Items[y] = temp;
					}
				}
			}
		}




Error 1 The best overloaded method match for 'string.Compare(string, string)' has some invalid arguments
string.Compare(listBox1.Items[x], listBox1.Items[x + 1])

Error 2 Argument '1': cannot convert from 'object' to 'string'
listBox1.Items[x]

Error 3 Argument '2': cannot convert from 'object' to 'string'
listBox1.Items[x + 1]
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Identifier expected

Posted 22 April 2008 - 02:29 PM

The String.Compare method is expecting two strings, but the ListBox.Items property returns an Object. You need to cast it to a string.

if (string.Compare(listBox1.Items[x].ToString(), listBox1.Items[x + 1].ToString()) > 0)

Was This Post Helpful? 1

#8 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Identifier expected

Posted 22 April 2008 - 09:13 PM

Thank you so much! I should have thought of that.

My program works exactly like it's supposed to, now.
It didn't even need the array, after all.

:^: :D

This post has been edited by OliveOyl3471: 22 April 2008 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1