none
.net wrapper for float. RRS feed

  • Question

  • Hi.
    I need some class which would be like type "float" but would be other type. As i know we can't inherit from "float". Maybe there is some generic container which would help me for that? Thanks.

    Wednesday, April 4, 2012 2:21 PM

Answers

  • You can create a struct with implicit conversions and implementing IConvertible. The following code is a nice start point:

    public struct Float
    {
        private float m_value;
    
        private Float(float value)
        {
            this.m_value = value;
        }
    
        public override bool Equals(object obj)
        {
            return this.m_value.Equals(obj);
        }
    
        public override int GetHashCode()
        {
            return this.m_value.GetHashCode();
        }
    
        public override string ToString()
        {
            return this.m_value.ToString();
        }
    
        public static bool operator ==(Float a, Float b)
        {
            return (a.m_value == b.m_value);
        }
    
        public static bool operator !=(Float a, Float b)
        {
            return !(a == b);
        }
    
        public static implicit operator float(Float value)
        {
            return value.m_value;
        }
    
        public static implicit operator Float(float value)
        {
            return new Float(value);
        }
    }
    

    Hope this helps,

    Miguel.

    Wednesday, April 4, 2012 2:32 PM

All replies

  • You can create a struct with implicit conversions and implementing IConvertible. The following code is a nice start point:

    public struct Float
    {
        private float m_value;
    
        private Float(float value)
        {
            this.m_value = value;
        }
    
        public override bool Equals(object obj)
        {
            return this.m_value.Equals(obj);
        }
    
        public override int GetHashCode()
        {
            return this.m_value.GetHashCode();
        }
    
        public override string ToString()
        {
            return this.m_value.ToString();
        }
    
        public static bool operator ==(Float a, Float b)
        {
            return (a.m_value == b.m_value);
        }
    
        public static bool operator !=(Float a, Float b)
        {
            return !(a == b);
        }
    
        public static implicit operator float(Float value)
        {
            return value.m_value;
        }
    
        public static implicit operator Float(float value)
        {
            return new Float(value);
        }
    }
    

    Hope this helps,

    Miguel.

    Wednesday, April 4, 2012 2:32 PM
  • What problem are you trying to solve?
    Wednesday, April 4, 2012 10:25 PM
  • I've solved my problem through wrapper which was offered by Miguel. But i'm just wondering about standard decision. For example some generic which lets don't write code over and over. Just something which allows to define some type though other.
    Friday, April 6, 2012 8:59 AM
  • Hello again, you can use generics to wrapper a struct changing the above code:

    public struct StructWrapper<T> where T: struct
    {
        private T m_value;
    
        private StructWrapper(T value)
        {
            this.m_value = value;
        }
    
        public override bool Equals(object obj)
        {
            return this.m_value.Equals(obj);
        }
    
        public override int GetHashCode()
        {
            return this.m_value.GetHashCode();
        }
    
        public override string ToString()
        {
            return this.m_value.ToString();
        }
    
        public static bool operator ==(StructWrapper<T> a, StructWrapper<T> b)
        {
            return (a.m_value.Equals(b.m_value));
        }
    
        public static bool operator !=(StructWrapper<T> a, StructWrapper<T> b)
        {
            return !(a == b);
        }
    
        public static implicit operator T(StructWrapper<T> value)
        {
            return value.m_value;
        }
    
        public static implicit operator StructWrapper<T>(T value)
        {
            return new StructWrapper<T>(value);
        }
    }
    

    Hope this helps,

    Miguel.

    Friday, April 6, 2012 2:26 PM