0

I am trying to create a new data type and have a property in property Value in the class.

I can access and manipulate the properties from within the class. However, while accessing the value from outside, I must use it as instance.Value.

Is it possible to set the property by assigning the value to the instance directly, perhaps like we use indexers for array based classes?

For example, what I want to do is this:

public class NewNumberType<T> 
{
    public T Value {get; set;}
}

From the calling class, I want to use the the NewNumberType as:

NewNumberType<double> d = new NewNumberType<double>();
// d.Value = 15; I dont want to do this
d = 15; // I want to refer the instance this way.

Using Structs for the job may not be ideal. For instance structs don't allow parameterless constructors.. It would throw "structs cannot contain explicit parameterless constructors" error.

It would be always desirable to use a class instead.

Bhairav Thakkar
  • 201
  • 2
  • 9
  • 3
    What you seem to want to do is to implicitly convert a double to your type. [Take a look here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit) – Shazi Dec 04 '18 at 20:12
  • 2
    You can create [implicit conversion operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit) but that would yield new instance each time you assign `double` to `NewNumberType`. – orhtej2 Dec 04 '18 at 20:12
  • The link mentioned by @Shazi does indeed move the problem forward.. However, it seems using the instance as regular variable seems to be not possible? Even the description in the link mentioned uses "dig2.val", where it would be great if we could use just "dig2". – Bhairav Thakkar Dec 04 '18 at 20:28
  • 1
    In context of final `Console.Writeline`? You can always overload `ToString` method to return `val.ToString()`. – orhtej2 Dec 04 '18 at 20:30
  • Yes @orhtej2, but using in computations would be cumbersome as we would seldom output the value straight after storing it. Usually it would go through lot of manipulations. – Bhairav Thakkar Dec 04 '18 at 20:32
  • There's a generic way of converting both ways: https://stackoverflow.com/a/6946547/7034621 – orhtej2 Dec 04 '18 at 20:56

1 Answers1

1

Yes, It's possible. You should define implicit conversion operator and if you want to show the value of the variable in console it's also worth to override ToString()

public class NewNumberType<T>
{
    public NewNumberType(T v)
    {
        Value = v;
    }

    public NewNumberType()
    {
    }

    public T Value { get; set; }

    public static implicit operator NewNumberType<T>(T v)
    {
        return new NewNumberType<T>(v);
    }

    public override string ToString()
    {
        return Value.ToString();
    }
}

Program.cs :

class Program
{
    static void Main(string[] args)
    {
        NewNumberType<double> d = new NewNumberType<double>();
        d = 15;
        Console.WriteLine(d);
        Console.ReadLine();
    }
}
svoychik
  • 1,188
  • 1
  • 8
  • 19