Skip to content

Commit

Permalink
code updated implicit conversion from msdn
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarTheTrainer committed Oct 22, 2018
1 parent 66a211f commit c8b1f70
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions Ch11-P4-CustomConversions/Ch11-P4-CustomConversions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Base.cs" />
<Compile Include="Digit.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rectangle.cs" />
Expand Down
35 changes: 35 additions & 0 deletions Ch11-P4-CustomConversions/Digit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch11_P4_CustomConversions
{
class Digit
{
public double val;
// ...other members

public Digit(double d) // Consructor
{
val = d;
}

public static implicit operator double(Digit d)
{
return d.val;
}

public static implicit operator Digit(double d)
{
return new Digit(d);
}

//// User-defined conversion from double to Digit
//public static implicit operator Digit(double d)
//{
// return new Digit(d);
//}
}
}
24 changes: 19 additions & 5 deletions Ch11-P4-CustomConversions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,26 @@ static void Main(string[] args)

#region Defining Implicit Conversion Routines

Square s3 = new Square { Length = 83 };
// Attempt to make an implicit cast?
//Rectangle rect2 = s3;
Rectangle rect2 = (Rectangle)s3;
//Square s3 = new Square { Length = 83 };
//// Attempt to make an implicit cast?
////Rectangle rect2 = s3;
//Rectangle rect2 = (Rectangle)s3;

//Console.WriteLine(rect2);

#endregion

#region Internet Surfing

// added a digit class

//Digit dig = new Digit(4);
////double num = dig.val; // it is fine
//double num = dig; // it is fine

//Digit dig2 = 12;


Console.WriteLine(rect2);

#endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1c6796e2a7b82ca27c61282efcb26c3fe40b1477
0095dc4b0b1b5e5266c992246e08d0ebe13895e5

0 comments on commit c8b1f70

Please sign in to comment.