Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can't figure out why the Main method won't call the GetData method.Can someone point me in the right direction? Thanks

C#
// GetData() method accepts order number and quantity
// that are used in the Main() method
// Price is $3.99 each
using System;
class DebugEight1
{
    static void Main()
    {
        
        int orderNum = 0, quantity = 0;
        double total;
        const double PRICE_EACH = 3.99;
        GetData(  out orderNum, out quantity);

        total = quantity * PRICE_EACH;

        Console.WriteLine(" Order #(), Quantity ordered ()",
            orderNum, quantity);
        Console.WriteLine("Total is {0}", total.ToString("C"));
       
    }
    private static void GetData( out int orderNum, out int quantity)
    {
        
        String s1, s2;
        Console.Write("Enter order number ");
        s1 = Console.ReadLine();
        Console.Write("Enter quantity ");
        s2 = Console.ReadLine();
        orderNum = Convert.ToInt32(s1);
        quantity = Convert.ToInt32(s2);
    }
}
Posted
Updated 5-Mar-15 8:14am
v2
Comments
Richard MacCutchan 5-Mar-15 13:08pm    
If it does not compile (which I suspect) then please show us the error messages and the lines on which they occur.
Member 11403574 5-Mar-15 13:11pm    
Actually there are no errors. It opens the window and then it simply says "press any key to continue" and then closes when you press enter or any other key. So I guess it is compiling just not correctly.
Sergey Alexandrovich Kryukov 5-Mar-15 13:26pm    
Apparently, it does call it. Use the debugger. Are you sure this is the application assembly? Are you sure it even compiles?
—SA
Member 11403574 5-Mar-15 13:27pm    
I do use the debugger. And it opens the window and then it simply says "press any key to continue" and then closes when you press enter or any other key.

Well it should do - unless you have another method called Main, that isn't in that class?
So start by checking if it executes that Main method: put a breakpoint on the first line, and run your app in the debugger. If it hits the breakpoint, your app will stop and wait for you. Step though until you do (or don't) get into the GetData method.


As Dudi suggested, it doesn't compile, because of the parameter problems.
You can fix that, it's not complex:
C#
static void Main()
    {

    int orderNum, quantity;
    double total;
    const double PRICE_EACH = 3.99;
    GetData(out orderNum, out quantity);
    total = quantity * PRICE_EACH;
    Console.WriteLine(" Order #{0}, Quantity ordered = {0}",
        orderNum, quantity);
    Console.WriteLine("Total is {0}", total.ToString("C"));

    }
private static void GetData(out int orderNum, out int quantity)
    {
    String s1, s2;
    Console.Write("Enter order number ");
    s1 = Console.ReadLine();
    Console.Write("Enter quantity ");
    s2 = Console.ReadLine();
    orderNum = Convert.ToInt32(s1);
    quantity = Convert.ToInt32(s2);
    }
But the code that results, is a bit...um...poor.
Instead, try doing this:
C#
const double PRICE_EACH = 3.99;
static void Main()
    {
    int orderNum = GetValue("Enter Order number : ");
    int quantity = GetValue("Enter Quantity     : ");
    double total = quantity * PRICE_EACH;
    Console.WriteLine("Order #{0}, Quantity ordered = {0}", orderNum, quantity);
    Console.WriteLine("Total is {0}", total.ToString("C"));
    }

private static int GetValue(string prompt)
    {
    int value;
    do
        {
        Console.Write(prompt);
        } while (!int.TryParse(Console.ReadLine(), out value));
    return value;
    }

This doesn't crash if the user enters a "bad" value - it prompts him and gets it again.
It also uses a method in a more "normal" way - to return the value you are interested in.

Try it. See what happens.
 
Share this answer
 
v2
Comments
Member 11403574 5-Mar-15 12:45pm    
I have put a break point in every conceivable position and it won't even call the Main method.
OriginalGriff 5-Mar-15 14:10pm    
Answer updated.
C#
// GetData() method accepts order number and quantity
// that are used in the Main() method
// Price is $3.99 each
using System;
class DebugEight1
{
    static void Main()
    {
        
        int orderNum = 0, quantity = 0;
        double total;
        const double PRICE_EACH = 3.99;
        GetData(  out orderNum, out quantity);
 
        total = quantity * PRICE_EACH;
 
        Console.WriteLine(" Order #(), Quantity ordered ()",
            orderNum, quantity);
        Console.WriteLine("Total is {0}", total.ToString("C"));
       
    }
    private static void GetData( out int orderNum, out int quantity)
    {
        
        String s1, s2;
        Console.Write("Enter order number ");
        s1 = Console.ReadLine();
        Console.Write("Enter quantity ");
        s2 = Console.ReadLine();
        orderNum = Convert.ToInt32(s1);
        quantity = Convert.ToInt32(s2);
    }
}
 
Share this answer
 
v2
Comments
Member 11403574 5-Mar-15 13:42pm    
I appreciate your help, but I don't want to use your code, I would rather discover why mine doesn't work.
HKHerron 5-Mar-15 13:48pm    
This is your code. All I did was fix it!
phil.o 5-Mar-15 22:49pm    
Then an explanation of what you fixed and why is more useful than just posting some code without any explanation.
Member 11403574 5-Mar-15 13:51pm    
Thank You
HKHerron 5-Mar-15 13:53pm    
OK, for starters, your code wont work because your GetData method does not return any information back to the Main method. Unless declared properly, variable declared in methods are not accessible to other methods. By using a class, you can pass this information between methods!
The problem is that this code doesn't compile.
In C# you must initialize variables before you can use them unless you use them as out parameters.
Trying to figure out your intention writing this code, I think what you are really trying to do is to call the GetData() method and pass the parameters by reference.

In order to make it work you have two choices:
1. Initialize the variables orderNum and quantity and put the keyword "ref" before them in the call to the GetData() method and inside the GetData() method declaration like this:
C#
int orderNum = 0, quantity = 0;
GetData(ref orderNum, ref quantity);

private static void GetData(ref int orderNum, ref int quantity)
{
....
....
}


2. Just put the keyword "out" before the orderNum and quantity variables before calling the GetData() method like this:
GetData(out orderNum, out quantity);


And change the method declaration of GetData() to this:

C#
private static void GetData(out int orderNum, out int quantity)
{
....
....
}




Good luck!
Dudi Avrahamov
Software Consultant
il.linkedin.com/in/dgsoft
 
Share this answer
 
v2
Comments
Member 11403574 5-Mar-15 13:01pm    
I have tried both methods you suggested and strangely enough, it still doesn't compile. I'm puzzled.
You need to change your code as follows:
C#
static void Main()
{
	int orderNum, quantity;
	double total;
	const double PRICE_EACH = 3.99;
// GetData needs the 'out' attribute on the two varuiables
	GetData(out orderNum, out quantity);
	total = quantity * PRICE_EACH;
// the display line needs identifiers '{x}' for the parameters
	Console.WriteLine(" Order #({0}), Quantity ordered = ({1})",
		orderNum, quantity);
	Console.WriteLine("Total is {0}", total.ToString("C"));

}
// use 'out' as above
private static void GetData(out int orderNum, out int quantity)
{
	String s1, s2;
	Console.Write("Enter order number ");
	s1 = Console.ReadLine();
	Console.Write("Enter quantity ");
	s2 = Console.ReadLine();
	orderNum = Convert.ToInt32(s1);
	quantity = Convert.ToInt32(s2);
}
 
Share this answer
 
Comments
Member 11403574 5-Mar-15 14:08pm    
Yes sir, I did do that but it still doesn't work. I would re post my code so you can see what I have now but I don't know if that's allowed here
Richard MacCutchan 6-Mar-15 2:54am    
I don't know what you did but I can assure you that the above code works. You can go to your original post above, click on Improve question and edit the content to show the exact code you have now. And if you have any compile errors then please show them also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900