Solved

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

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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