Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
| Search | Today's Posts | Mark Forums Read
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 4th, 2013, 09:31 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default operator overload casting a value

here is this piece of coding:-

Code:
 
 
// C# 4 Wrox , Page 122
// using generic structures , using Nullable<T>
// casting  Nullable<T> to T using operatoe overloads
public struct Nullable<T> where T:<struct>
{
public Nullable (T value)
{
this.hasValue = true;
this.value = value;
}
private bool hasValue;
public bool HasValue
{
get{ return hasValue;}
}
private T value
Public T value
{
get { if (!hasValue)
{throw new InvalidOperationException("no value")}
}
return value;
}
}
public static explicit operator T ( Nullable<T>  value)
{
return  value.Value;
public static implicit operator Nullable<T> (T  value)
{
return new  Nullable<T>(value);
}
public override string ToString(){
if(!HasValue)  
return  String.Empty;
return this.value.ToString();}
}
}
class MPEP  {public static void Main(){
 Nullable<int>  x;
x = 4;
X+ = 3;
if(x.HasValue){
int  y = x.Value;
}
x = null;
}}

here we see that a code says:-

return value.Value

q1 } the question is how does we access a property ( Value) through a variable (value)?

q2 } How does an operator onerload casts a value
 
Old September 4th, 2013, 10:09 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by amigo1 View Post
q1 } the question is how does we access a property ( Value) through a variable (value)?
What do you mean? By having a lowercase variable and uppercase Property?
Code:
private T value
public T Value
{
  get { 
    if (!hasValue)
    {
      throw new InvalidOperationException("no value");
    }
    return value;
  }
}

Quote:
Originally Posted by amigo1 View Post
q2 } How does an operator onerload casts a value
The operator overload does not cast, it is defined to change a Nullable<T> to T.
Code:
public static explicit operator T (Nullable<T>  value)
Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
amigo1 (September 6th, 2013)
 
Old September 6th, 2013, 08:23 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default Thank you

Thank you teacher
Regards
amigo1




Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
Overload for specialization? Raconteur Visual Basic 2005 Basics 2 May 7th, 2007 10:49 AM
Unary operator overload and inheritance Jonax C++ Programming 8 February 27th, 2006 08:50 AM
subtraction operator overload arnie6 C++ Programming 0 April 19th, 2005 07:57 AM
can we overload 'as' keyword g_natarajan_mca General .NET 0 August 30th, 2004 07:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.