Saturday, November 10, 2012

User-Defined Conversions in C#: implicit or explicit

C# allows programmers to declare conversions on classes or structs so that classes or structs can be converted to and/or from other classes or structs, or basic types. Conversions are defined like operators and are named for the type to which they convert.

In C#, conversions can be declared either as implicit, which occur automatically when required, or explicit, which require a cast to be called. All conversions must be static, and must either take the type the conversion is defined on, or return that type.

Example:
 using System;  
   
 namespace ImplicitConversion  
 {  
   class Conversion  
   {  
     static void Main(string[] args)  
     {  
       ConvertibleDouble cdouble;  
   
       // Perform an implicit conversion from a int to a ConvertibleDouble  
       // No cast is required because implicit cast is defined  
       // i.e. public static implicit operator ConvertibleDouble(double val)  
       cdouble = 200;  
       System.Console.WriteLine(cdouble);  
   
       // Perform an implicit conversion from a ConvertibleDouble to a double  
       // No cast is required because implicit cast is defined   
       // i.e. public static implicit operator double(ConvertibleDouble val)  
       double a = new ConvertibleDouble(999);  
       System.Console.WriteLine(a);  
   
       // Perform an explicit conversion from a string to a ConvertibleDoubl  
       // cast is required because explicit cast is defined   
       // i.e. public static explicit operator ConvertibleDouble(string val)  
       string obj = "$400.00";  
       ConvertibleDouble b = (ConvertibleDouble)obj;  
       System.Console.WriteLine(b);  
   
   
       // Perform an explicit conversion from a ConvertibleDoubl to a string  
       // cast is required because explicit cast is defined   
       // i.e. public static explicit operator ConvertibleDouble(string val)       
       string d = (string)b;  
       System.Console.WriteLine(d);  
   
   
       System.Console.WriteLine("Press any key to exit.");  
       System.Console.ReadKey();  
     }  
   }  
   
   struct ConvertibleDouble  
   {  
     private double _val;  
   
     public ConvertibleDouble(double val)  
     {  
       this._val = val;  
     }  
   
     public static implicit operator ConvertibleDouble(double val)  
     {  
       return new ConvertibleDouble(val);  
     }  
   
     public static implicit operator double(ConvertibleDouble val)  
     {  
       return val._val;  
     }  
   
     public static explicit operator ConvertibleDouble(string val)  
     {  
       return new ConvertibleDouble(Convert.ToDouble(val.Trim().Replace("$", "")));  
     }  
   
     public static explicit operator string(ConvertibleDouble val)  
     {  
       return string.Format("${0:N2}", val._val);  
     }  
   
     public override string ToString()  
     {  
       return _val.ToString();  
     }  
   }  
 }  
   


OutPut:
 200  
 999  
 400  
 $400.00  
 Press any key to exit.  
   

Friday, November 2, 2012

[GIVE AWAY] 1AVShare, the ultimate secure file sharing software for free.

PCWinSoft brings to you an exciting offer. The company is giving away free licenses of 1AVShare, the ultimate file sharing software. 1AVShare costs $49.95 but in here you get it for FREE.

1AVShare is a cool lightweight gadget that allows you to share files with your friends and co-workers in a safe and secure environment: 1AVShare publishes a 128-bit encrypted password protected website where you can safely share movie files, music files, documents, executable files, and files of other extensions.


1AVShare utilizes Windows native security to safe guard your shared content. So, breaking up 1AVShare security is as difficult as breaking Windows security so it is safe to say 1AVShare security is unbreakable.

After you start 1AVShare web server it creates a website for you and you can send the link to your friends and co-workers and you are also in charge of granting access rights and you can do that on a user base, so for one user you can allow access to the documents page, and for another user you can allow access to the images page only, you can grant access to one or all pages. You the web master defines the access roles, usernames and passwords of your visitors.


1AVShare is very lightweight and you can leave it running 24x7. The website it creates can be accessed from anywhere in the World without the need to install client components and it works for all web browsers, no exception.

It will run on 2000/XP/2003/Media Center/Vista/Windows 7/Windows 8 and requires at least a 1.3gHz processor and 256mb of memory.

To receive your free copy of 1AVShare simply register here: http://www.pcwinsoft.com/promotion/geektechie123/registration.asp