Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error      2      Argument 1: cannot convert from 'void' to 'string'

Posted on 2014-04-15
3
Medium Priority
?
1,569 Views
Last Modified: 2014-04-20
I have the following class and when I call the following method Console.WriteLine(dn.TotalDonation()); I get this error. Error      2      Argument 1: cannot convert from 'void' to 'string'      

Is it possible that I have declared my totalDonation method not correctly?
 public void TotalDonation() {
            TotalDonationLBS = DogFoodLBS + CatFoodLBS;
        //    
       
        //
        }



Here is my total code.
class Donations
    {
        private int DogFoodLBS { get; set; }
        private int CatFoodLBS { get; set; }
        public int TotalDonationLBS { get; set; }

        private int CashDonation { get; set; }

        public Donations(int donationdog, int donationcat, int cashdonation) {
            DogFoodLBS = donationdog;
            CatFoodLBS = donationcat;
            CashDonation = cashdonation;
        }

        public void TotalDonation() {
            TotalDonationLBS = DogFoodLBS + CatFoodLBS;
        //    
        
        //
        }

        public int _TotalDonation {
            get { return TotalDonationLBS; }
           
        
            }
        public int _DogFoodD {
            get { return DogFoodLBS; }
            set { DogFoodLBS = value; }
        
        }
        public int _CatFoodD {
            get { return CatFoodLBS; }
            set { CatFoodLBS = value; }
        }
    
    }

Open in new window


static void Main(string[] args)
        {
        Donations dn = new Donations (10,4,10);
        Console.WriteLine(dn.TotalDonation());
        Console.ReadLine();


        
        }

Open in new window

0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40002462
Based on your class definition you should be doing:
Donations dn = new Donations(10,4,10);
int totalDonation = dn._TotalDonation;

Console.WriteLine(totalDonation.ToString());

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40002469
Hi yguyon28;

The way the function TotalDonation is written it does not return a value. So when you execute the Console.WriteLine that it is expecting it to return the TotalDonation but that function does not return anything.

If you want to use TotalDonation in that way it must return a value something like this:

public int TotalDonation() 
{
    TotalDonationLBS = DogFoodLBS + CatFoodLBS;
    return TotalDonationLBS;
}

Open in new window

Or create another function to do the same.
0
 
LVL 1

Accepted Solution

by:
repu1sion earned 2000 total points
ID: 40002480
You need to declare TotalDonation() function as one returning "string" not "void".
And after that modify it a little to return real string value:

public string TotalDonation() {
            TotalDonationLBS = DogFoodLBS + CatFoodLBS;
             return TotalDonationLBS.ToString()
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question