Solved

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

Posted on 2014-04-15
3
1,313 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
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 62

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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now