At the moment i have this class
public class Currency
{
private int _Amount;
public Currency(){... }
public Currency(int amount){_Amount = amount;}
public override string ToString()
{
return _Amount + " Gold.";
}
}
I want this class to have all the functionality of an integer so i can do things like this
Currency curr = new Currency();
Currency curr2 = new Currency(100);
curr = 50;
curr += 50;
curr += curr2;
i found kinda of what i needed here : Integer c++ wrapper
but this is for C++. Can someone tell me how i do this in C#?
public operator Currency() { return _Amount; }
Doesn't work, nor adding implicit/explicit anywhere.
class Currency
{
...
// User-defined conversion from Digit to double
public static implicit operator int(Currency d)
{
return d._Amount;
}
}
See implicit (C# Reference) for more info. The second thing you want to check, is operator overloading.
What you want is to declare it as struct (so it doesn't get boxed/unboxed, nor cant be null unless you mark it Nullable) and have implicit/explicit converters (can be converted without casts)
public struct Currency
public static implicit operator Currency(decimal value)
{
return new Currency(value);
}
public static implicit operator decimal(Currency me)
{
return me.value;
}
Related
Here is demonstration of the problem:
class Program
{
static double Func(double a, double b) { return a * 1000 + b * b; }
static void Main(string[] args)
{
var a = 1.1d;
var b = 2.2d;
Console.WriteLine(Func(a, b));
// this is the problem, function doesn't recognize when a and b
// "accidentally" exchanged, target is to make this row a compile-time error
Console.WriteLine(Func(b, a));
}
}
This become an issue if there are methods with many parameters (e.g. ten of double type):
double Func(double parameter1, double parameter2, ..., double parameter10);
Question: is there a way to validate parameters when calling method, so that programmer is less prone to do a mistake?
This is not an issue if parameter types are different. I thought what maybe wrapping into new types will help:
class A
{
private double _value;
public static implicit operator A(double value) { return new A() { _value = value }; }
public static implicit operator double(A value) { return value._value; }
}
class B
{
private double _value;
public static implicit operator B(double value) { return new B() { _value = value }; }
public static implicit operator double(B value) { return value._value; }
}
class Program
{
static double Func(A a, B b) { return a * 1000 + b * b; }
static void Main(string[] args)
{
A a = 1.1d;
B b = 2.2d;
Console.WriteLine(Func(a, b));
Console.WriteLine(Func(b, a)); // compile-time error! yay!
Console.WriteLine(Func(a, b) + 123.123d - a * 2); // implicit conversion power
Console.ReadKey();
}
}
And it does, but I am quite unsure if this method is efficient. I have doubts if this is a good idea at all. Is it? Or is there better one?
I know what I can be absolutely safe if I always call method like this (using named arguments method call)
Func(a:a, b:b);
This shouldn't bring any overhead in code, but a lot of typing. Wrapping is better because it is done once (creating new type is easy), but it probably has overhead.
If two arguments are of the same type, it's not possible to detect at compile-time, run-time or otherwise that the name of the argument variable corresponds to the name of the parameter. This is kind of an open question, but I will offer you a couple ideas.
As Mehrzad suggested, consider grouping parameters by some type. For example, instead of double Distance(double x1, double y1, double x2, double y2), consider double Distance(Point p1, Point p2)
In general, if your method has more than 4-5 parameters, consider refactoring. Maybe your method is trying to do too many things and the logic can be divided?
If what you actually want to do is to perform some check such as ensuring that a < b, consider looking into Code contracts. You could also use Debug.Assert(), but this only works at run-time.
I wouldn't recommend the kind of implicit conversion you propose. For me, it feels hacky and unintuitive that A a = 1.1 should have no semantic purpose other than compile-time checking parameters. Your ultimate goal is to make code more maintainable overall.
You should never have 10 parameters for a method.
Once you have around 4 parameters, begin to consider the use of a new class to contain those parameters... As an example, consider the preferences of a user navigating on a website...
void Main()
{
UserPreferences preference = new UserPreferences
{
BackgroundColor = "#fff",
ForegroundColor = "#000",
Language = "en-GB",
UtcOffSetTimeZone = 0
};
User aydin = new User(preference);
}
public class User
{
public User(UserPreferences preferences)
{
this.Preferences = preferences;
}
public UserPreferences Preferences { get; set; }
}
public class UserPreferences
{
public string BackgroundColor { get; set; }
public string ForegroundColor { get; set; }
public int UtcOffSetTimeZone { get; set; }
public string Language { get; set; }
}
Use an inherited class something like this
class Program
{
static double Func(List<Parent> l) { return l[0]._value * 1000 + l[1]._value * l[1]._value; }
static void Main(string[] args)
{
A a = 1.1d;
B b = 2.2d;
Console.WriteLine(Func(new List<Parent>() {a,b}));
Console.WriteLine(Func(new List<Parent>() { a, b })); // compile-time error! yay!
Console.WriteLine(Func(new List<Parent>() { a, b }) + 123.123d - a * 2); // implicit conversion power
Console.ReadKey();
}
}
class Parent
{
public double _value { get; set; }
}
class A : Parent
{
public static implicit operator A(double value) { return new A() { _value = value }; }
public static implicit operator double(A value) { return value._value; }
}
class B : Parent
{
public static implicit operator B(double value) { return new B() { _value = value }; }
public static implicit operator double(B value) { return value._value; }
}
How could Reflection be used to determine the ways an object is cast from inside a method?
Example:
Given this type, which can be implicitly cast and assigned as an int, float, or string:
public class VersatileType {
public int intVal = 10;
public float floatVal = 1.5f;
public string stringVal = "words";
// implicit convertions
// ints
public static implicit operator int(VersatileType vt) {
return vt.intVal;
}
public static implicit operator VersatileType(int val) {
VersatileType vt = new VersatileType();
vt.intVal = val;
return vt;
}
// floats
public static implicit operator float(VersatileType vt) {
return vt.floatVal;
}
public static implicit operator VersatileType(float val) {
VersatileType vt = new VersatileType();
vt.floatVal = val;
return vt;
}
// strings
public static implicit operator string(VersatileType vt) {
return vt.stringVal;
}
public static implicit operator VersatileType(string val) {
VersatileType vt = new VersatileType();
vt.stringVal = val;
return vt;
}
}
And the given method which does some implicit casting and assigning:
public VersatileType obj;
public void CastAndAssignObj() {
obj = 0;
string text = obj;
}
Is there a way to use Reflection (or any other process) to determine how "obj" was cast/assigned from inside CastAndassignObj()?
I'd like to end up with a collection containing the Types int and string for the above example.
Many thanks-
Reflection will do you no good here because it will always produce VersatileType and that has 3 properties of type string, int and float reflection won't tell you which one is being used. I think the simplest way to do what you want is either to add a flag (isInt, isFloat, isString) or stop initializing those values so you can say
if (myVersatileType.StringVal != null)
// guess we're a string
You can't use reflection because it does not create different types. There is only one type, which has three values, and only one of those is used.
Have this code:
string abc = "123456";
To convert to int should I use convert:
int abcInt = Convert.ToInt32(abc);
The problem is that if not a number I have an exception see returning zero so my final code will look like:
try{ int abcInt = Convert.ToInt32(abc); }catch(Exception e){ int abcInt = 0; }
So you see that I decided to create a book that made me an object returning zero numeric without exception if it failed, so could keep most flexible programming without much junk code:
int abcInt = Libs.str.safeInt(abc);
The code is:
public int safeInt(object ob)
{
if ((ob == null) || (String.IsNullOrEmpty(ob.ToString())))
return 0;
try
{
return Convert.ToInt32(
System.Text.RegularExpressions.Regex.Replace(ob.ToString(), #"#[^Ee0-9\.\,]+#i", "").
ToString(CultureInfo.InvariantCulture.NumberFormat)
);
}
catch (FormatException e)
{
return 0;
}
}
But I want to go one step further and do something like this:
int abcInt = (safeInt)abc;
how to do?
Can not convert type 'string' to 'Libs.safeInt.safeInt'
You should just use Int32.TryParse:
int abcInt;
if(!Int32.TryParse(abc, out abcInt)) {
abcInt = 0;
}
// abcInt has been parsed to an int, or defaulted to zero
Note that this can be shortened to
int abcInt;
Int32.TryParse(abc, out abcInt);
if all that you want is the default value to be zero because:
When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
I actually recommend against writing it this way because now you can't distinguish between abc = "0" and abc = "garbage"; both with exhibit exactly the same behavior with the above two lines of code. With the initial version above (i.e., the if, you can distinguish the two cases if you need to; silently ignoring errors is generally a bad idea).
That said, if you absolutely are dying to know how to implement an explicit cast operator, you proceed like this:
class SafeInt32 {
private readonly int value;
public int Value { get { return this.value; } }
private readonly string source;
public string Source { get { return this.source; } }
private readonly bool successful;
public bool Successful { get { return this.successful; } }
public SafeInt32(string source) {
this.source = source;
this.successful = Int32.TryParse(source, out this.value);
}
public static explicit operator SafeInt32(string source) {
return new SafeInt32(source);
}
public static implicit operator int(SafeInt32 safeInt32) {
return safeInt32.Value;
}
}
Usage:
int abcInt = (SafeInt32)"123456";
Note that we had to define an explicit cast operator to cast a string to a SafeInt32, and an implicit cast operator to cast a SafeInt32 to an int to achieve your desired syntax. The latter is necessary so that the compiler can silently convert the result of (SafeInt32)"123456" to an int.
Again, I recommend against this; use Int32.TryParse.
You can leverage implicit and explicit operators to do what you want, yes. You can also use int.TryParse to avoid using exceptions for control flow.
public struct SafeInt
{
public int Value { get; private set; }
public static implicit operator int(SafeInt safeInt)
{
return safeInt.Value;
}
public static explicit operator SafeInt(string obj)
{
return new SafeInt() { Value = SafeParse(obj) };
}
public static int SafeParse(object value)
{
int output;
int.TryParse((value ?? "0").ToString(), out output);
return output;
}
}
I mean, you should use int.TryParse, but if you're dead-set on the cast syntax:
public class SafeInt
{
private int _value;
private SafeInt() {}
public static explicit operator SafeInt(string str)
{
int x;
int.TryParse(str, out x);
SafeInt si = new SafeInt();
si._value = x;
return si;
}
public static implicit operator int(SafeInt x)
{
return x._value;
}
public override string ToString()
{
return _value.ToString();
}
}
You can then use it like this:
int x = (SafeInt)"234234";
First, let me just go on record saying that you may not want to do this.
Silently ignoring problems like this can cause other types of problems, such as a customer asking "Why is this total over here always wrong?".
Having said that, let's see how you can do what you want before I give you a better option:
void Main()
{
int a = (SafeInt)"123";
a.Dump();
int b = (SafeInt)"xyz";
b.Dump();
}
public struct SafeInt
{
private readonly int _Value;
public SafeInt(int value)
{
_Value = value;
}
public SafeInt(int? value)
{
_Value = value ?? 0;
}
public int Value
{
get
{
return _Value;
}
}
public static implicit operator int(SafeInt s)
{
return s.Value;
}
public static implicit operator SafeInt(string s)
{
try
{
return new SafeInt(Convert.ToInt32(s));
}
catch (FormatException)
{
return new SafeInt();
}
}
}
This will print out:
123
0
Now, my advice is to stay away from this. Instead, use this:
void Main()
{
TryParse("123").Dump();
TryParse("xyz").Dump();
}
public static int TryParse(string s, int errorValue = 0)
{
int result;
if (int.TryParse(s, out result))
return result;
return errorValue;
}
Note that if you always want 0 as the value to return upon an error, there's even a much simpler way built into the system, this:
int value;
int.TryParse("123", out value);
Here we disregard the Boolean result from TryParse, because if TryParse fails, it'll set the parameter to 0.
I'd recommend that you do not do this. I find explicit and implicit conversions to be hard to discover, read, and use, compared to simpler static methods and/or constructors. Also, are you aware of the int.TryParse method? That might be a better solution for you:
public static int SafeInt(object value)
{
int i;
int.TryParse(value.ToString(), out i);
return i;
}
Or, more directly to answer your question, you can use explicit and implicit conversions on a SafeInt class to do this:
public class SafeInt
{
public int Value { get; set; }
public static implicit operator int(SafeInt si)
{
return si.Value;
}
public static explicit operator SafeInt(String str)
{
return new SafeInt { Value = Libs.str.safeInt(str) };
}
}
Use like:
int i = (SafeInt)"123";
Given the code:
public class Filter<T>
{
private bool selected = false;
public bool Selected { get { return selected; } }
private T value;
public T Value { get{ return this.value; } set { this.value = value; selected = true; }
}
public class Test
{
public void filter()
{
DateTime a= new DateTime();
Nullable<DateTime> b = new DateTime(); //Work Like a Charm
Filter<DateTime> c = new DateTime(); //Dosent Work
}
}
In Nullable<T> the new DateTime() can be assigned directly into the variable. In my class, it doesn't work. I want to understand what I'm missing.
I think that is something simple. But I couldn't put it on words to find the answer.
You have to implement implicit operators:
public static implicit operator Filter<T>(T value)
{
return new Filter<T>() { Value = value };
}
An implicit operator will allow you to cast the types without explicitly writing Filter<T> filter = (Filter<T>)value; (explicit cast), but rather only Filter<T> filter = value; (implicit cast).
You need to use an implict conversion operator:
See: Implicit cast operator and the equality operator
This allows you to write code to construct your custom type from another type.
As it is MyClass x = 120;, is it possible to create such a custom class?
If so, how can I do that?
It's generally considered a bad idea to use implicit operators, as they are, after all, implicit and run behind your back. Debugging code littered with operator overloads is a nightmare. That said, with something like this:
public class Complex
{
public int Real { get; set; }
public int Imaginary { get; set; }
public static implicit operator Complex(int value)
{
Complex x = new Complex();
x.Real = value;
return x;
}
}
you could use:
Complex complex = 10;
or you could ever overload the + operator
public static Complex operator +(Complex cmp, int value)
{
Complex x = new Complex();
x.Real = cmp.Real + value;
x.Imaginary = cmp.Imaginary;
return x;
}
and use code like
complex +=5;
Not sure if this is what you want but you may get there by implementing the implicit operator:
http://msdn.microsoft.com/en-us/library/z5z9kes2(VS.71).aspx
Create an implicit operator:
http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx
For example:
public struct MyStruct // I assume this is what you meant, since you mention struct in your title, but use MyClass in your example.
{
public MyClass(int i) { val = i; }
public int val;
// ...other members
// User-defined conversion from MyStruct to double
public static implicit operator int(MyStruct i)
{
return i.val;
}
// User-defined conversion from double to Digit
public static implicit operator MyStruct(int i)
{
return new MyStruct(i);
}
}
"Is this a good idea?" is debatable. Implicit conversions tend to break accepted standards for programmers; generally not a good idea. But if you're doing some large value library, for example, then it might be a good idea.
yes, here's a short example ...
public struct MyCustomInteger
{
private int val;
private bool isDef;
public bool HasValue { get { return isDef; } }
public int Value { return val; } }
private MyCustomInteger() { }
private MyCustomInteger(int intVal)
{ val = intVal; isDef = true; }
public static MyCustomInteger Make(int intVal)
{ return new MyCustomInteger(intVal); }
public static NullInt = new MyCustomInteger();
public static explicit operator int (MyCustomInteger val)
{ if (!HasValue) throw new ArgumentNullEception();
return Value; }
public static implicit operator MyCustomInteger (int val)
{ return new MyCustomInteger(val); }
}