Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

c# templates

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    c# templates

    I am attempting to move a small but useful template class from c++ to c#. rather than manage two variables and multiple functions to track max and min of an object, I constructed my own class which works brilliant in c++.

    When porting to c#, the compiler complains,
    Operator ‘<’ cannot be applied to operands of type ‘T’ and ‘T’
    which comes from third to last line in the code. It also complains about the minus sign in second last line.

    Google says to use the where keyword to constrain the template T, but still no joy. Google also implies that Templates (generics in c#) are provided, but not to be used for simple things like math ops or logic. Simply want to use this template on Doubles and Ints.

    Any c# experts in the room to offer advice on templates? Or is it easier to manage two classes, one for Double, and one for Int?

    thanks




    Code:
    	//template <class T>
    	class maximin<T> where T : struct{
    		private T _mmin;
    		private T _mmax;
    		
    	public T mmin{
    		get { return _mmin;}
    		set {_mmin = value;}
    	}
    	public T mmax{
    		get { return _mmax;}
    		set {_mmax = value;}
    	}	
    	
    	public	maximin( T a, T b ){	// constructor
    			_mmin = a;
    			_mmax = b;
    		}
    
    	public T getMax(){ return (_mmax); }
    	public T getMin(){ return (_mmin); }
    	public bool isBetween( T b ){ return ( b < _mmax && b > _mmin ? true : false ); }
    	public T getSize(){ return _mmax - _mmin; }
    	}

    #2
    Hello calipoe,

    Thank you for writing in. You cannot compare or modify T types in this way. For example, if <T> were a custom class or structure type, the compiler cannot assume it has overloaded the +, -, *, and / operators. Basically, the compiler does not know that you are only going to pass in a double or an int. This is why you are getting the errors you are getting.

    If you have provided your entire class and this is all it does, I would not use generics I would just write two classes.

    If there is significantly more to it and you want to use generics, you need to find the best way to compare/modify the types. You can either make your types implement some interface that has a member that can be used and constrain the class to that or you can write methods that accept a specific type and use that.

    Please let me know if you have any further questions.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Thank you Michael

      Comment


        #4
        General NinjaScript programming questions.

        NinjaTrader_MichaelM :thank you

        Comment


          #5
          Originally posted by caliope View Post
          Google also implies that Templates (generics in c#) are provided, but not to be used for simple things like math ops or logic.
          Exactly.

          Look at the lines giving you the errors. What are they doing? Yep, math ops or logic ... which is not allowed.

          Think of generics as holders of data, and that data could be of any type. As a holder of said data, it is not allowed to operate (much) on that data, but only to hold it.

          Btw, these methods,

          Code:
          public T getMax(){ return (_mmax); }
          public T getMin(){ return (_mmin); }
          appear redundant and useless, because these properties,

          Code:
          public T mmin{
              get { return _mmin;}
              set {_mmin = value;}
          }
          public T mmax{
              get { return _mmax;}
              set {_mmax = value;}
          }
          already provide public access to _mmax and _mmin via the getters.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Jon17, Today, 04:33 PM
          0 responses
          1 view
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          6 views
          0 likes
          Last Post Javierw.ok  
          Started by timmbbo, Today, 08:59 AM
          2 responses
          10 views
          0 likes
          Last Post bltdavid  
          Started by alifarahani, Today, 09:40 AM
          6 responses
          41 views
          0 likes
          Last Post alifarahani  
          Started by Waxavi, Today, 02:10 AM
          1 response
          21 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Working...
          X