Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,060 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,591 people online right now. Registration is fast and FREE... Join Now!




Identifier expected

 
Reply to this topicStart new topic

Identifier expected

OliveOyl3471
21 Apr, 2008 - 11:14 AM
Post #1

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
I can't figure out what identifier to use, and where:

CODE

                  //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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Identifier Expected
21 Apr, 2008 - 11:44 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You need to provide an index number to assign a value to an array element. Use the i from your For loop.
CODE

names1[i] = TextBox1.Text.ToString();

User is online!Profile CardPM
+Quote Post

OliveOyl3471
RE: Identifier Expected
21 Apr, 2008 - 09:45 PM
Post #3

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
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.
smile.gif

This post has been edited by OliveOyl3471: 21 Apr, 2008 - 09:48 PM
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Identifier Expected
22 Apr, 2008 - 08:49 AM
Post #4

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
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. smile.gif

This post has been edited by OliveOyl3471: 22 Apr, 2008 - 11:03 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Identifier Expected
22 Apr, 2008 - 10:00 AM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

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

CODE

string[] names1=new string[10];


The Array class provides methods for creating, manipulating, searching, and sorting arrays, it is not meant to be instantiated.
User is online!Profile CardPM
+Quote Post

OliveOyl3471
RE: Identifier Expected
22 Apr, 2008 - 11:33 AM
Post #6

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
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:

CODE


        //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]


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Identifier Expected
22 Apr, 2008 - 01:29 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

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

CODE

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

User is online!Profile CardPM
+Quote Post

OliveOyl3471
RE: Identifier Expected
22 Apr, 2008 - 08:13 PM
Post #8

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,611



Thanked: 17 times
Dream Kudos: 150
My Contributions
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.

icon_up.gif biggrin.gif

This post has been edited by OliveOyl3471: 22 Apr, 2008 - 08:14 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:25PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month