Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this in my class

private double fcs_classwork;

C#
public object coresubjectSecond
       {
           get { return fcs_classwork; }
           set { fcs_classwork = value.ToString(); }
       }


but i get this error meesage can not implicitly convert type 'string' to 'double'
please how do i solve this.
Posted
Comments
RDBurmon 13-Jun-12 9:27am    
Thanks Everyone who replied to this thread , So Mike, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Here

the fcs_classwork declared as double data type.So you need to convert the value to double

C#
public object coresubjectSecond
       {
           get { return fcs_classwork; }
           set { fcs_classwork = Convert.ToDouble(value);  }
       }
 
Share this answer
 
Comments
mikeoabban 13-Jun-12 3:47am    
thanks alot it worked
Prasad_Kulkarni 13-Jun-12 3:53am    
Formally 'Accept solution', if its really helpful.
VJ Reddy 13-Jun-12 4:07am    
Good answer. 5!
Here
the fcs_classwork declared as double data type.So you need to convert the value to double
C#
public object coresubjectSecond
       {
           get { return fcs_classwork; }
           set { fcs_classwork = Convert.ToDouble(value);  }
       }
 
Share this answer
 
v2
Comments
VJ Reddy 13-Jun-12 4:08am    
Good answer. 5!
Manas Bhardwaj 13-Jun-12 5:15am    
Will Work +5!
using under code
C#
public object coresubjectSecond
       {
           get { return fcs_classwork; }
           set { fcs_classwork = Convert.ToDouble(value);  }
       }
 
Share this answer
 
v2
Comments
VJ Reddy 13-Jun-12 4:08am    
Good answer. 5!
Manas Bhardwaj 13-Jun-12 5:15am    
Will Work +5!
The Solutions 1 to 3 are good and resolve the above error.

But, it is worthwhile to review the design of the above property.

In the present case as seen from the code given in the question, there seems to be no requirement for having the Type of the property coresubjectSecond as object and the Type of the back end store fcs_classwork as double.

Since, double is a ValueType and object is Reference Type there will be an overhead of boxing and unboxing due to the above design, which may be costly particularly when the property is used excessively.

So, it is better to change the Type of back end store fcs_classwork to object or change the Type of the property coresubjectSecond to double as per the requirement, as a consequence the above error can be avoided as shown below

C#
private double fcs_classwork;

public double coresubjectSecond
{
   get { return fcs_classwork; }
   set { fcs_classwork = value; }
}

Further, as seen from the code the property is being used like a field without any additional code in getter and setter blocks of the property. Hence, if C# 3.0 and above is used, then the auto implemented property can be used in this case which will reduce the code clutter and improve the code readability as shown below
C#
public double coresubjectSecond {get; set;}
 
Share this answer
 
v3
Comments
Manas Bhardwaj 13-Jun-12 5:14am    
Well explained +5!
VJ Reddy 13-Jun-12 5:17am    
Thank you, Manas :)
Prasad_Kulkarni 13-Jun-12 5:19am    
Good explanation VJ!
VJ Reddy 13-Jun-12 5:20am    
Thank you, Prasad :)
bhaskarareddy 13-Jun-12 5:20am    
Good explanation with simple description

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