how to display array of instances?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohit8536
    New Member
    • Apr 2010
    • 5

    #1

    how to display array of instances?

    hi,
    i am learning .net language.. and at the beginning level.i am having a problem in following code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace interface_IComparable_practice1
    {
        class cur : IComparable<cur>
        {
            int d, c;
            public cur(int d1,int c1)
            {
                d=d1;
                c=c1;
            }
            public int CompareTo(cur other)
            {
               
              int tval=(this.d*100)+this.c;
                int otval=(other.d*100)+other.c;
                Console.WriteLine("1->{0},2->{1}\n",tval.ToString(),otval.ToString());
                Console.WriteLine(tval.CompareTo(otval).ToString());
                return(tval.CompareTo(otval));
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                cur[] sal = { new cur(100, 00), new cur(70, 08), new cur(80, 53), new cur(20, 78) };
                Array.Sort(sal);
                //foreach(cur i in sal )
                for(i=0;i<4;i++)
                Console.WriteLine("\n{0}",sal[i].ToString());
                Console.Read();
            }
    
        }
    }


    Prob1: i am not able to print the value of sal[] which is array of instances.
    Prob2: Which sorting method is being used (by the system) in Array.Sort().
    Prob3: in output of above code,comparing is unnecessarily getting performed repeatedly.Am I missing any concept here?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    i am learning .net language.
    There is more than one .NET programming language.
    Since your code snippet is C#, your thread has been moved to the C# area.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Prob1: Why not? "I can't print" doesn't describe the error you are getting
      Prob2: Read about the Array.Sort method on MSDN
      Prob3: You are calling it more than once, so it is going to run more than once.
      Code:
                  Console.WriteLine(tval.[B][I]CompareTo[/I][/B](otval).ToString());
                  return(tval.[B][I]CompareTo[/I][/B](otval));

      Comment

      • rohit8536
        New Member
        • Apr 2010
        • 5

        #4
        hi tlhintoq,

        prob1: i am using following code to display sal[].
        Code:
                    for(i=0;i<4;i++)
                    Console.WriteLine("\n{0}",sal[i].ToString());
        but i am getting the following output for this code:
        [ouput]
        interface_IComa parable_practic e1.cur
        interface_IComa parable_practic e1.cur
        interface_IComa parable_practic e1.cur
        interface_IComa parable_practic e1.cur
        [/output]

        interface_IComa parable_practic e1 is the namespace of this project.

        Prob2:Solved. thanks tlhintoq for solving Prob2.

        Prob3:
        the code below is just used to display the output of method CompareTo (1,-1,0)
        Code:
        Console.WriteLine(tval.CompareTo(otval).ToString());

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          the code below is just used to display the output of method CompareTo (1,-1,0)
          But it does still call the CompareTo method, then takes that result and applies the ToString() method.

          Then you call CompareTo again as part of the return

          Put a breakpoint on this line and follow the execution with F11 to see this in operation

          Comment

          • rohit8536
            New Member
            • Apr 2010
            • 5

            #6
            Originally posted by tlhintoq
            But it does still call the CompareTo method, then takes that result and applies the ToString() method.

            Then you call CompareTo again as part of the return

            Put a breakpoint on this line and follow the execution with F11 to see this in operation
            prob1:still unsolved.. i do not know how to display array of instances ie. sal[]

            prob3:in starting i was thinking the same and removed the line to display (1,-1,0)..I am still getting the same no. of iterations of method CompareTo.. As per the quicksort algo used in Array.Sort(), CompareTo method should not be iterate this much time.. though i think that this increased iterations are not affecting the result..

            Comment

            • rohit8536
              New Member
              • Apr 2010
              • 5

              #7
              Originally posted by rohit8536
              prob1:still unsolved.. i do not know how to display array of instances ie. sal[]

              prob3:in starting i was thinking the same and removed the line to display (1,-1,0)..I am still getting the same no. of iterations of method CompareTo.. As per the quicksort algo used in Array.Sort(), CompareTo method should not be iterate this much time.. though i think that this increased iterations are not affecting the result..
              prob1:still unsolved.. i do not know how to display array of instances ie. sal[]

              prob3:in starting i was thinking the same and removed the line to display (1,-1,0)..I am still getting the same no. of iterations of method CompareTo.. As per the quicksort algo used in Array.Sort(), CompareTo method should not be iterate this much time.. though i think that this increased iterations are not affecting the result..

              Comment

              • Monomachus
                Recognized Expert New Member
                • Apr 2008
                • 127

                #8
                Originally posted by rohit8536
                prob1:still unsolved.. i do not know how to display array of instances ie. sal[]

                prob3:in starting i was thinking the same and removed the line to display (1,-1,0)..I am still getting the same no. of iterations of method CompareTo.. As per the quicksort algo used in Array.Sort(), CompareTo method should not be iterate this much time.. though i think that this increased iterations are not affecting the result..
                Hi, to resolve problem1 you need to overwrite your cur class ToString() method which is usually just will output the type of the object.

                A smallish example is this code:

                Code:
                internal class WeirdObject
                    {
                        public int X { get; set; }
                        public int Y { get; set; }
                
                        public override string ToString()
                        {
                            return string.Format("X={0}, Y={1}", X, Y);
                        }
                    }

                Comment

                • rohit8536
                  New Member
                  • Apr 2010
                  • 5

                  #9
                  Originally posted by rohit8536
                  prob1:still unsolved.. i do not know how to display array of instances ie. sal[]

                  prob3:in starting i was thinking the same and removed the line to display (1,-1,0)..I am still getting the same no. of iterations of method CompareTo.. As per the quicksort algo used in Array.Sort(), CompareTo method should not be iterate this much time.. though i think that this increased iterations are not affecting the result..
                  hi,thanks..now by using above snippet ,i am able to display.. can u plz explain the concept.. why do we need to override? why was i not able to display array of instances without overriding the class?
                  waitin for +reply

                  Comment

                  • Monomachus
                    Recognized Expert New Member
                    • Apr 2008
                    • 127

                    #10
                    Originally posted by rohit8536
                    hi,thanks..now by using above snippet ,i am able to display.. can u plz explain the concept.. why do we need to override? why was i not able to display array of instances without overriding the class?
                    waitin for +reply
                    Why would you think that the system would automaticaly output with .ToString() method the info that you would need? Isn't it kinda weird?

                    So the deal is that .ToString() method outputs the representation of current Object. By default for classes that you create, the representation is the name of the class, including namespace. To change this default functionality you would need to override this method and indicate what exactly would you want ToString method to output.

                    To read more about it follow the links.
                    Object.ToString Method
                    And this one How To: Override the ToString Method (C# Programming Guide)

                    Comment

                    Working...