Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
install app on other machine 13 32
Iterate a dictionnary to change values 4 53
How to LOAD up a .NET Framework website with data? 2 39
Runtime Error 2 27
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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

948 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

21 Experts available now in Live!

Get 1:1 Help Now