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. Generic programming help

Generic programming help

Scheduled Pinned Locked Moved C#
helpdata-structuresquestionannouncement
5 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    shivamkalra
    wrote on last edited by
    #1

    Hi, I need to make a generic version of Binary Search Tree, I've non generic version which uses comparable as a data on the node. Therefore I need to define generic type such that it should be an sub class of Comparable interface. Note: Comparable it self is a generic type. This what I've done so far but its giving me compiler errors.

    Comparable data;

    void addNode(Node n)
    {
    //Some code
    int c = data.compareTo(n.data);
    }

    In the code above, data is of type Comparable thus it has method compareTo(E data), but when I cast ((E)n.data) it gives me error that its not type safe. But I dunno why when I've defined Comparable data, then why can't I cast data?? Any help will be appreciated.

    D 1 Reply Last reply
    0
    • S shivamkalra

      Hi, I need to make a generic version of Binary Search Tree, I've non generic version which uses comparable as a data on the node. Therefore I need to define generic type such that it should be an sub class of Comparable interface. Note: Comparable it self is a generic type. This what I've done so far but its giving me compiler errors.

      Comparable data;

      void addNode(Node n)
      {
      //Some code
      int c = data.compareTo(n.data);
      }

      In the code above, data is of type Comparable thus it has method compareTo(E data), but when I cast ((E)n.data) it gives me error that its not type safe. But I dunno why when I've defined Comparable data, then why can't I cast data?? Any help will be appreciated.

      D Offline
      D Offline
      David1987
      wrote on last edited by
      #2

      note: Comparable<T> is the Java version, in C# it's IComparable<T> The problem is that data would have to be IComparable<E> and E both at the same time - that could be the case, but it is not guaranteed (you could try to feed it an invalid type, and C# generics aren't C++ templates). So you must constrain the type E to be Comparable<E> in the class definition of the Binary Search Tree (and/or Node, depending on whether it is a nested type or not)

      class BST where E : IComparable
      {
      E data;
      // etc
      }

      S 1 Reply Last reply
      0
      • D David1987

        note: Comparable<T> is the Java version, in C# it's IComparable<T> The problem is that data would have to be IComparable<E> and E both at the same time - that could be the case, but it is not guaranteed (you could try to feed it an invalid type, and C# generics aren't C++ templates). So you must constrain the type E to be Comparable<E> in the class definition of the Binary Search Tree (and/or Node, depending on whether it is a nested type or not)

        class BST where E : IComparable
        {
        E data;
        // etc
        }

        S Offline
        S Offline
        shivamkalra
        wrote on last edited by
        #3

        And how do you constaring generic types in class definition for JAVA. I read on Google that you can constraint in method definition not in beginning of the class.

        D 1 Reply Last reply
        0
        • S shivamkalra

          And how do you constaring generic types in class definition for JAVA. I read on Google that you can constraint in method definition not in beginning of the class.

          D Offline
          D Offline
          David1987
          wrote on last edited by
          #4

          I don't know, try the Java forum

          S 1 Reply Last reply
          0
          • D David1987

            I don't know, try the Java forum

            S Offline
            S Offline
            shivamkalra
            wrote on last edited by
            #5

            NVM, I got it. In java code is this way..

            class BinarySearchTree>

            Where

            ? super T

            denotes any super class of type T. Thanks for the help. :) Shivam Kalra

            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