Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Casting generic parameters to int

Casting generic parameters to int

Scheduled Pinned Locked Moved C#
questiondatabasedata-structures
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jack Valmadre
    wrote on last edited by
    #1

    Hi I have an array of integers, where each one is an index for an array of objects, and I'm trying to sort the array by the .name property of the objects. I've set up a class using the IComparer interface to do this.

        List<int> SubjList = new List<int>();
        SubjectIndexComparer<int> subjectIndexComparer = new SubjectIndexComparer<int>();
        ...
        SubjList.Sort(subjectIndexComparer);
    
    public class SubjectIndexComparer<T> : IComparer<T>
    {
        ...
        public int Compare(T x, T y)
        {
            string textX = Singleton.Instance.subjList\[(int)x\].name;
            string textY = Singleton.Instance.subjList\[(int)y\].name;
            ...
        }
    }
    

    However, this doesn't work because I can't cast x and y from type T to int. I only want the function to work if T can be casted to an integer. How can I implement this? I don't actually need the function to be generic, it would be fine if it just took integers, but the List.Sort() method required that form. Thanks in advance. Jack

    M 1 Reply Last reply
    0
    • J Jack Valmadre

      Hi I have an array of integers, where each one is an index for an array of objects, and I'm trying to sort the array by the .name property of the objects. I've set up a class using the IComparer interface to do this.

          List<int> SubjList = new List<int>();
          SubjectIndexComparer<int> subjectIndexComparer = new SubjectIndexComparer<int>();
          ...
          SubjList.Sort(subjectIndexComparer);
      
      public class SubjectIndexComparer<T> : IComparer<T>
      {
          ...
          public int Compare(T x, T y)
          {
              string textX = Singleton.Instance.subjList\[(int)x\].name;
              string textY = Singleton.Instance.subjList\[(int)y\].name;
              ...
          }
      }
      

      However, this doesn't work because I can't cast x and y from type T to int. I only want the function to work if T can be casted to an integer. How can I implement this? I don't actually need the function to be generic, it would be fine if it just took integers, but the List.Sort() method required that form. Thanks in advance. Jack

      M Offline
      M Offline
      mikker_123
      wrote on last edited by
      #2

      Why don't you try with:

      public class SubjectIndexComparer : IComparer
      {
      ...
      public int Compare(int x, int y)
      {
      ...
      }
      }

      ? Does that helps?

      J 1 Reply Last reply
      0
      • M mikker_123

        Why don't you try with:

        public class SubjectIndexComparer : IComparer
        {
        ...
        public int Compare(int x, int y)
        {
        ...
        }
        }

        ? Does that helps?

        J Offline
        J Offline
        Jack Valmadre
        wrote on last edited by
        #3

        I tried with:

        public class SubjectIndexComparer : IComparer
        {
            ...
            public int Compare(int x, int y)
            {
                ...
            }
        }
        

        but I got: 'UQTimetable.SubjectIndexComparer' does not implement interface member 'System.Collections.IComparer.Compare(object, object)' I tried with:

        public class SubjectIndexComparer : IComparer
        {
            ...
            public int Compare(object x, object y)
            {
                ...
            }
        }
        

        but I got: The best overloaded method match for 'System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments Argument '1': cannot convert from 'UQTimetable.SubjectIndexComparer' to 'System.Collections.Generic.IComparer' Any more ideas?

        Jack

        D 1 Reply Last reply
        0
        • J Jack Valmadre

          I tried with:

          public class SubjectIndexComparer : IComparer
          {
              ...
              public int Compare(int x, int y)
              {
                  ...
              }
          }
          

          but I got: 'UQTimetable.SubjectIndexComparer' does not implement interface member 'System.Collections.IComparer.Compare(object, object)' I tried with:

          public class SubjectIndexComparer : IComparer
          {
              ...
              public int Compare(object x, object y)
              {
                  ...
              }
          }
          

          but I got: The best overloaded method match for 'System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments Argument '1': cannot convert from 'UQTimetable.SubjectIndexComparer' to 'System.Collections.Generic.IComparer' Any more ideas?

          Jack

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          Use this:

          public class SubjectIndexComparer : IComparer<int>
          {
          public int Compare(int x, int y)
          {
          ...
          }
          }

          M J 2 Replies Last reply
          0
          • D Daniel Grunwald

            Use this:

            public class SubjectIndexComparer : IComparer<int>
            {
            public int Compare(int x, int y)
            {
            ...
            }
            }

            M Offline
            M Offline
            mikker_123
            wrote on last edited by
            #5

            :( that's what I meant... tnx for correcting me Daniel.

            1 Reply Last reply
            0
            • D Daniel Grunwald

              Use this:

              public class SubjectIndexComparer : IComparer<int>
              {
              public int Compare(int x, int y)
              {
              ...
              }
              }

              J Offline
              J Offline
              Jack Valmadre
              wrote on last edited by
              #6

              Worked a treat! Thanks

              Jack

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups