Solved

Function Return Values in C#

Posted on 2006-07-23
5
378 Views
Last Modified: 2010-08-05
I'm currently learning C# and moderatly know VB.NET. I cannot figure out what I'm doing wrong in this practice example. I am getting an error when I compile the project: "cannot convert from method group to object" for ThisInvoice.GetInvoiceAmount. Any help would be appreciated. Heres the code -

using System;
using System.Collections.Generic;
using System.Text;

    class Invoice
    {
        private string partNumber, description;
        private int quantity;
        private decimal price;

        public Invoice()
        {
            partNumber = "";
            description = "";
            quantity = 1;
            price = 0;
        }
       
        public string PartNumber
        {
            get
            {
                return partNumber;
            }
            set
            {
                partNumber = value;
            }

        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public int Quantity
        {
            get
            {
                return quantity;
            }
            set
            {
                quantity = value;
            }
        }

        public decimal Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public decimal GetInvoiceAmount()
        {          
                return Quantity * Price;            
        }
    }
0
Comment
Question by:dynamk
  • 3
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17163084
Hi dynamk;

The posted class looks fine. How are you creating the class and initializeing the variables and calling GetInvoiceAmount()? Can you post the code where you do this?

Fernando
0
 

Author Comment

by:dynamk
ID: 17163160
Here's the calling class:

using System;
using System.Collections.Generic;
using System.Text;


    class InvoiceTest
    {            
        static void Main(string[] args)
        {
            Invoice ThisInvoice = new Invoice();
            ThisInvoice.PartNumber = "124578ab";
            ThisInvoice.Description = "Widget1";
            ThisInvoice.Price = Convert.ToDecimal(7.99);

            Console.WriteLine("Please enter quantity desired:");
            ThisInvoice.Quantity = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Your invoice details are as follows:\nPart Number: {0}\nDescription: {1}\nPrice: {2}\n",ThisInvoice.PartNumber,ThisInvoice.Description,ThisInvoice.Price);
            Console.WriteLine("Quantity: {0}  ", ThisInvoice.Quantity);
            Console.WriteLine("Total Amount: {0}", ThisInvoice.GetInvoiceAmount);
        }
    }
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 50 total points
ID: 17163182
Hi dynamk;

Change this line in your code

            Console.WriteLine("Total Amount: {0}", ThisInvoice.GetInvoiceAmount);

To this

            Console.WriteLine("Total Amount: {0}", ThisInvoice.GetInvoiceAmount());

Fernando
0
 

Author Comment

by:dynamk
ID: 17163277
Thanks, I really have to get used to the differences between VB.Net.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17163289
No problem, glad I was able to help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

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