2 Replies - 2779 Views - Last Post: 21 February 2010 - 11:33 PM Rate Topic: -----

#1 Lava79   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 145
  • Joined: 31-January 10

Passing an array from a click event to a method

Posted 21 February 2010 - 09:59 PM

Ok, I've been staring at this for hours upon hours. The goal is to code 3 arrays in their own separate click events (sort, search and reverse) after adding the user input to a list box (got the add button coded and working just fine). I think I've gotten that part, but then I have to pass each array to one main load array method, do the sort/search/reverse (dependent on which button I click), and then pass the array info BACK to the click event. I am really struggling with the passing of the arrays into the method. Attached is the code. Any nudges in the right direction will be much appreciated. And if I've completely f-ed this up, feel free to tell me. :)

(the only event in the load array method is the sort thus far)

The three click event arrays:
 //load the array from the listbox into the sort event.
        private void btnSort_Click(object sender, EventArgs e)
        {
            string[] sortNames = new string[5];

            lstNames.SetSelected(0, true);
            sortNames[0] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(1, true);
            sortNames[1] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(2, true);
            sortNames[2] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(3, true);
            sortNames[3] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(4, true);
            sortNames[4] = lstNames.SelectedItems[0].ToString();

  

        }
        //load the array from the listbox into the reverse event.
        private void btnReverse_Click(object sender, EventArgs e)
        {
            string[] reverseNames = new string[5];
            lstNames.SetSelected(0, true);
            reverseNames[0] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(1, true);
            reverseNames[1] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(2, true);
            reverseNames[2] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(3, true);
            reverseNames[3] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(4, true);
            reverseNames[4] = lstNames.SelectedItems[0].ToString();
          
            lstNames.Items.Clear();

            
        }

        //load the array from the list box into the search event.
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string[] searchNames = new string[5];

            lstNames.SetSelected(0, true);
            searchNames[0] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(1, true);
            searchNames[1] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(2, true);
            searchNames[2] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(3, true);
            searchNames[3] = lstNames.SelectedItems[0].ToString();

            lstNames.SetSelected(4, true);
            searchNames[4] = lstNames.SelectedItems[0].ToString();

            lstNames.Items.Clear();




The load array method
 }
        static void Main(string[]args)
        {
             string[] names = new string[5];
            
        }

        public static void LoadArray(string[] sortNames,searchNames,reverseNames)
        {
            int i;
            Array.Sort(sortNames);
            for (i = 0; i < sortNames.Length; ++i) ;
          
        }




Thanks in advance for any help. I'm sure I've taken something relatively simple and turned it into something much harder than it needs to be. Being a noob is painful. lol.

This post has been edited by Lava79: 21 February 2010 - 10:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing an array from a click event to a method

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Passing an array from a click event to a method

Posted 21 February 2010 - 10:14 PM

Change the return type from void:

      
public static String[] LoadArray(string[] sortNames,searchNames,reverseNames)
{
    int i;
    Array.Sort(sortNames);
    for (i = 0; i < sortNames.Length; ++i) ;
    // processing...
    return sortnames;      
}



Then in the click methods:

string[] sortedNames = LoadArray(sortNames , ...);


Was This Post Helpful? 1
  • +
  • -

#3 Lava79   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 145
  • Joined: 31-January 10

Re: Passing an array from a click event to a method

Posted 21 February 2010 - 11:33 PM

Thank you so much!

I did realize that I needed to change the sort / reverse to a foreach and now I have two errors that I'm not sure how to get rid of. Am I missing something? I've returned each back to their click events..

 public static string[] LoadArray(string[] sortNames, searchNames, reverseNames)
        {
            
            Array.Sort(sortNames);
            foreach (string name in sortNames) 

            return sortNames;

            Array.Reverse(reverseNames);
            foreach(string name in reverseNames)
            return reverseNames;

            
           string findThisString = "";
           int strNumber;
           int strIndex = 0;
           for (strNumber = 0; strNumber < searchNames.Length; strNumber++)

           strIndex = searchNames[strNumber].IndexOf(findThisString);
           { 
               if (strIndex >= 0)
             
            return searchNames;
           }
        }
 




The two errors:
Error 1 Identifier expected Form1.cs 25 73
Error 2 Identifier expected Form1.cs 25 87

I know where they are: They are at the end of both searchNames and reverseNames. The sortName doesn't cause an error.. That's what I don't quite understand..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1