Solved

Function Return Values in C#

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

776 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