how to use events in buuble sort application in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yogeshtiwarijbp
    New Member
    • Aug 2007
    • 29

    how to use events in buuble sort application in c#

    Iam creating an apllication using generics in c# that sort the strings as well as integers.Can i use events in that? i mean to say can i fire some event say after the sorting is done or some thing like that?
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by yogeshtiwarijbp
    Iam creating an apllication using generics in c# that sort the strings as well as integers.Can i use events in that? i mean to say can i fire some event say after the sorting is done or some thing like that?
    Sure, you can create an event then signal that event when it completes. You can also use callbacks.
    Last edited by RedSon; Oct 18 '07, 03:12 PM.

    Comment

    • yogeshtiwarijbp
      New Member
      • Aug 2007
      • 29

      #3
      Originally posted by RedSon
      Sure, you can create an event then signal that event when it completes. You can also use callbacks.
      Can u provide me some example?

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by yogeshtiwarijbp
        Can u provide me some example?
        No, but you can read this: http://msdn2.microsoft.com/en-us/lib...39(VS.71).aspx

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Wouldn't your code block while the sorting is happening and when it "unblocks" you know the sorting is complete?

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by Plater
            Wouldn't your code block while the sorting is happening and when it "unblocks" you know the sorting is complete?
            Yes, but in a multi threaded app, you might want to be able to tell the other threads when the sort is complete.

            Comment

            • yogeshtiwarijbp
              New Member
              • Aug 2007
              • 29

              #7
              Originally posted by Plater
              Wouldn't your code block while the sorting is happening and when it "unblocks" you know the sorting is complete?

              This is my code . now can u explain me how i can call events or fire events or raise an event in this code.
              plz. help

              yogesh

              [CODE=cpp]public delegate void Sorting<T>(T[]array) where T:IComparable;
              public class SortingGen
              {
              public static void bubble_sort_gen eric<T>(T[] array) where T : IComparable
              {
              for (int i = 0; i < (array.Length - 1); i++)
              {
              for (int j = i + 1; j < (array.Length); j++)
              {
              if (array[i].CompareTo(arra y[j]) > 0)
              {
              T temp = array[j];
              array[j] = array[i];
              array[i] = temp;
              }
              }
              }
              }


              public static void selectionSortGe neric<T>(T[] a) where T : IComparable
              {

              for (int sortedSize = 0; sortedSize < a.Length; sortedSize++)
              {
              int minElementIndex = sortedSize;
              T minElementValue = a[sortedSize];
              for (int i = sortedSize + 1; i < a.Length; i++)
              {
              if (a[i].CompareTo(minE lementValue) < 0)
              {
              minElementIndex = i;
              minElementValue = a[i];
              }
              }
              a[minElementIndex] = a[sortedSize];
              a[sortedSize] = minElementValue ;
              }
              }

              }


              class Class1:SortingG en
              {
              static void Main(string[] args)
              {
              System.Console. WriteLine("Ente r 1 for STRING Sort or 2 for NUMERIC Sort");
              int num = Convert.ToInt32 (System.Console .ReadLine());

              switch (num)
              {
              case 1:
              string[] myArray1 = new string[5];
              System.Console. WriteLine("Ente r the elements:");
              for (int i = 0; i < 5; i++)
              {
              myArray1[i] = System.Console. ReadLine();
              }

              bubble_sort_gen eric(myArray1);

              Console.WriteLi ne("After Sorting");
              for (int i = 0; i < 5; i++)
              {
              Console.WriteLi ne(myArray1[i]);
              }
              Console.ReadLin e();
              break;
              case 2:
              int[] myArray = new int[5];
              System.Console. WriteLine("Ente r the elements:");
              for (int i = 0; i < 5; i++)
              {
              myArray[i] = Convert.ToInt32 (System.Console .ReadLine());
              }

              //Sorting thru Selection Sort
              selectionSortGe neric(myArray);

              //bubble_sort_gen eric(myArray);

              Console.WriteLi ne("After Sorting");
              for (int i = 0; i < 5; i++)
              {
              Console.WriteLi ne(myArray[i]);
              }
              Console.ReadLin e();
              break;

              }
              }
              }
              }[/CODE]
              Last edited by RedSon; Oct 18 '07, 04:08 PM. Reason: Added [CODE] tags.

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Please enclose your posted code in [code] tags (See How to Ask a Question).

                This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                Please use [code] tags in future.

                Also, you double posted the same response to both my and Plater's response. Doing this is a violation of site policy. Please do not do this again.

                MODERATOR
                Last edited by RedSon; Oct 18 '07, 04:11 PM.

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Originally posted by yogeshtiwarijbp
                  This is my code . now can u explain me how i can call events or fire events or raise an event in this code.
                  plz. help

                  yogesh
                  No, I am not going to hold your hand or spoon feed your the answer. Read the tutorial that I gave you a link to. After you have read that and tried it out, if you still have questions you can ask again.

                  Comment

                  • yogeshtiwarijbp
                    New Member
                    • Aug 2007
                    • 29

                    #10
                    Originally posted by RedSon
                    No, I am not going to hold your hand or spoon feed your the answer. Read the tutorial that I gave you a link to. After you have read that and tried it out, if you still have questions you can ask again.
                    Soory sir

                    I will read the article and try to make things done

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Originally posted by yogeshtiwarijbp
                      Soory sir

                      I will read the article and try to make things done
                      Good,

                      If you have problems with the article, or certain parts don't make sense, you can ask about those parts. But remember no one is going to do your work for you, we will help you do your work on your own.

                      Comment

                      Working...