Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7401153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:23:33+00:00 2026-05-29T04:23:33+00:00

I want my object to be able to type in an double or a

  • 0

I want my object to be able to type in an double or a string such as getting an input for salary. I have my code working with a property that allows for a double only. I know that property overloading isn’t supported from the other postings at this site. I also know that setters are going to allow me to get an string input for salary. I don’t understand how to overload. I have some of the template code here:

private double salary = 20000;

public Employee()
{
}

public Employee(double sal)
{
    salary = sal;
}

public double Salary
{
    get { return salary; }
    set { salary = value; }
}

public void SetSalary(string sal)
{
    salary = Convert.ToString(sal);       
}

Error code:

can not implicitly covert type string to double

I want to be able to have an object be able to overload salary using a setter in C#. I am a student and understand most of the basics. Thanks ahead of time for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:23:36+00:00Added an answer on May 29, 2026 at 4:23 am
    public void SetSalary(string sal) 
    { 
        salary = Convert.ToString(sal);        
    } 
    

    You are converting the parameter, which is already a string, to a string, and trying to assign it to a field that is of type double.

    salary = Convert.ToDouble(sal); // one way
    salary = double.Parse(sal); // another way 
    

    Note that these conversions can fail if the string is not in the proper numeric format. double.TryParse could be useful, but it’s probably an exception that needs to propogated to your callers when they send an invalid input. With all of that said, I would leave it up to your callers to convert the value to the appropriate type and simply expose the double property. There’s no need to complicate matters in your class.

    For that matter, for a value that is supposed to represent a salary, you should consider using the more appropriate decimal type. It’s tailored for storing financial values.

     decimal salary; 
     // elsewhere
     salary = Convert.ToDecimal(sal);
     salary = decimal.Parse(sal);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to parse a string to an object that I
If I have an object that I want to be able to observe several
I want to be able to have an object extend Enumerable in Ruby to
I have a scraping object basically. I want to be able to add POST
In Delphi, I want to be able to create an private object that's associated
I have a object i that i list in HTML tables. But i want
I want to be able to get the current bound object in the ItemTemplate
I want to be able to apply a style at runtime to an object
I have an array of objects, and want the objects to be able to
Problem: I want disparate sections of my code to be able to access a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.