Solved

Function Return Values in C#

Posted on 2006-07-23
5
357 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 62

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 62

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 62

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

24 Experts available now in Live!

Get 1:1 Help Now