Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Codeproject,

For one of my C# friends I had to create an infinite integer(Class, DLL).

However, there is one thing I've never known about C#.
Assuming that I have a class that looks like the following:

SQL
public class CustomString
{

}



I would then use this class to create an object out of it:

C#
CustomString cString = new CustomString()
;

And then I would set the class like so:

C#
cString = "Hello";


How could I use the class to retreive the value that it has been set to and then run a void accordingly to handle the information, and how is that with getting too?
Posted
Comments
Sergey Alexandrovich Kryukov 31-Oct-13 21:02pm    
Yvar, you are growing. I voted 4 for the question, for the first time. I'm curious: what would be the meaning of infinite integer object? What would be the operations with it, why?
Note that infinite numbers for floating-point numbers do exist, implemented in the CPU instructions and IEEE standard and are extremely useful. There are PositiveInfinity, NegativeInfinity and NaN.
—SA
Yvar Birx 1-Nov-13 10:18am    
Hey Sergey,

I'd love to discuss these matters via a private messenger(If possible Skype, and or Email) as I'm not going to spew my ideas over Codeproject(with that also my code as I know some people over here copy my code and claim their rights).

So if you are still interested please let me know somehow. :)

- Yvar
Sergey Alexandrovich Kryukov 1-Nov-13 11:08am    
Very good, if you are interested, as soon as you post, please also post me the links in response to this message.
—SA
Yvar Birx 1-Nov-13 11:22am    
I'll send you a private messaging holding my Skype and Email. ;)
BillWoodruff 1-Nov-13 1:45am    
The "implicit" modifier that allows you to define Classes, or Structs, in a way where they handle instantiation automatically with some default Type(s) for input, is mean to be used for conversion between Types.

I think it's a good idea to keep Microsoft's warning in mind when creating such Classes, or Structs: "However, because implicit conversions do not require programmers to explicitly cast from one type to the other, care must be taken to prevent unexpected results. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness."

Particularly if I were creating a code library for someone else to use, I would consider carefully providing other people objects that "behaves" in a way different than other .NET objects.

1 solution

Not sure if this is what you are looking for or not.

Reference for implicit conversion http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx[^]

C#
struct CustomString
{

    /// <summary>
    /// Prevents a default instance of the <see cref="CustomString"/> struct from being created.
    /// </summary>
    /// <param name="value">The value.</param>
    private CustomString(string value)
    {
        _value = value;
    }


    /// <summary>
    /// The internal value
    /// </summary>
    private string _value = string.Empty;

    /// <summary>
    /// Strings the specified c.
    /// </summary>
    /// <param name="c">The c.</param>
    /// <returns></returns>
    public static implicit operator string(CustomString e)
    {
        return e._value;
    }

    /// <summary>
    /// Customs the string.
    /// </summary>
    /// <param name="e">The e.</param>
    /// <returns></returns>
    public static implicit operator CustomString(string e)
    {
        return new CustomString(e);
    }

}


C#
class UseCustomString
{
    CustomString e = "Some Text";

    void Process()
    {
        if (e.Equals("Some Text"))
        {
            OtherProcess(e);
        }
    }

    void OtherProcess(CustomString s)
    {
        //do something
    }

}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 31-Oct-13 20:58pm    
Very good, a 5.
—SA
BillWoodruff 1-Nov-13 1:46am    
+5 Very interesting use of "implicit" ! But, please see my words of caution to the OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900