Solved

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

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

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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