Generic class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Generic class

    Hi all,
    how can create a generic class tha has a "value" property that can be int or
    string?

    Thanks a lot.


    --
    Luigi

  • Steph

    #2
    Re: Generic class

    Luigi wrote:
    Hi all,
    how can create a generic class tha has a "value" property that can be int or
    string?
    >
    Thanks a lot.
    >
    >
    public class test
    {
    public object a_param
    {
    get{;}
    set{;}
    }
    }

    Comment

    • =?Utf-8?B?THVpZ2k=?=

      #3
      Re: Generic class

      "Steph" wrote:
      public class test
      {
      public object a_param
      {
      get{;}
      set{;}
      }
      }
      Is the same writing?

      public class Test<T>
      {
      public T value;
      }

      Comment

      • Steph

        #4
        Re: Generic class

        Luigi wrote:
        "Steph" wrote:
        >
        >public class test
        >{
        > public object a_param
        > {
        > get{;}
        > set{;}
        > }
        >}
        >
        Is the same writing?
        >
        public class Test<T>
        {
        public T value;
        }
        why not ! all depend of your use !
        your can use one of this two ways.

        the code class goto define the good way

        Comment

        • Marc Gravell

          #5
          Re: Generic class

          That doesn't use generics; while it will work (typos aside), it isn't
          exactly going to help much.

          Comment

          • Marc Gravell

            #6
            Re: Generic class

            You can't do much to restrict the available types to int or string, but
            if you are happy to allow any type:

            public class Foo<T{
            public T Value {get;set;}
            }

            or in C# 2.0:

            public class Foo<T{
            private T value;
            public T Value {
            get {return this.value;}
            set {this.value = value;}
            }

            The only way to disallow other types would be to use a type inintializer
            - but it probably isn't worth it. There are such things as generic
            constraints, but none that would fit here.

            Marc

            Comment

            • =?Utf-8?B?THVpZ2k=?=

              #7
              Re: Generic class

              "Marc Gravell" wrote:
              You can't do much to restrict the available types to int or string, but
              if you are happy to allow any type:
              >
              public class Foo<T{
              public T Value {get;set;}
              }
              >
              or in C# 2.0:
              >
              public class Foo<T{
              private T value;
              public T Value {
              get {return this.value;}
              set {this.value = value;}
              }
              >
              The only way to disallow other types would be to use a type inintializer
              - but it probably isn't worth it. There are such things as generic
              constraints, but none that would fit here.
              Thank you Marc, this sounds fine.

              Luigi

              Comment

              • Stanimir Stoyanov

                #8
                Re: Generic class

                Luigi, please see your previous post about this. It is not an implementation
                using generics but should fit your requirements.
                --
                Stanimir Stoyanov


                "Luigi" <ciupazNoSpamGr azie@inwind.itw rote in message
                news:59BE4A82-E7C3-449F-9C3E-A5812236D23A@mi crosoft.com...
                Hi all,
                how can create a generic class tha has a "value" property that can be int
                or
                string?
                >
                Thanks a lot.
                >
                >
                --
                Luigi
                >

                Comment

                • Anthony Jones

                  #9
                  Re: Generic class

                  "Marc Gravell" <marc.gravell@g mail.comwrote in message
                  news:u9dy1qbPJH A.5080@TK2MSFTN GP03.phx.gbl...
                  You can't do much to restrict the available types to int or string, but if
                  you are happy to allow any type:
                  >
                  public class Foo<T{
                  public T Value {get;set;}
                  }
                  >
                  or in C# 2.0:
                  >
                  public class Foo<T{
                  private T value;
                  public T Value {
                  get {return this.value;}
                  set {this.value = value;}
                  }
                  >
                  The only way to disallow other types would be to use a type inintializer -
                  but it probably isn't worth it. There are such things as generic
                  constraints, but none that would fit here.
                  >
                  I've used IConvertible as a constraint. Not as specific as int and string
                  but it does narrow things down considerably without excluding either of
                  those two.

                  --
                  Anthony Jones - MVP ASP/ASP.NET

                  Comment

                  Working...