Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Passing variables between methods

MavrixMavrix Posts: 7Member
[b][red]This message was edited by Mavrix at 2006-9-30 9:37:26[/red][/b][hr]
Hey all, a couple more assignments in, and I've ran into something else our text doesn't explain very well. Anyway, the exersize is:

===================================
Design a C# solution with a Main ( ) method that holds an integer variable named seconds to which you will assign a value. Create a method to which you pass this value. The method displays the seconds in minutes and seconds. As an example 66 seconds is 1 minute and 6 seconds.

Add a second method to the solution. This method displays a passed argument as hours, minutes and seconds. For example 3666 seconds is 1 hour, 1 minute and 6 seconds.

Add a statement to the Main ( ) method so that after it calls the methods to convert seconds to minutes and seconds, it passes the same value to the new method to convert it to hours, minutes and seconds
===================================

Our text covered creating multiple methods and passing variables.. The Methods I think I have down ok, but I'm not quite grasping passing variables.. Here's the code that I have, and the errors I'm getting upon compiling.

I'm getting so frustrated on just figuring out how to pass the variables, I'm pretty sure I don't have the calculations right, or even properly complete the assignment as it's asking, but once I can figure out how to manipulate the methods that shouldn't be a problem... I hope

---------
[code]

using System;
public class lab4
{
public static void Main()
{
int Seconds = 131985;
secondsMethod(Minutes, Seconds);
hoursMethod(Hours);
Console.WriteLine("131985 seconds equals {0} Hours {1} Minutes and {2} Seconds", Hours, Minutes, totalSeconds);

}
public static void secondsMethod(int Seconds)
{
int Minutes = Seconds / 60;
int totalSeconds = Seconds % Minutes;
}
public static void hoursMethod(int Minutes)
{
int Hours = Minutes / 60;
}
}

f:lab4class1.cs(7,17): error CS0103: The name 'Minutes' does not exist in the class or namespace 'lab4'
f:lab4class1.cs(8,15): error CS0103: The name 'Hours' does not exist in the class or namespace 'lab4'
f:lab4class1.cs(9,84): error CS0103: The name 'Hours' does not exist in the class or namespace 'lab4'
f:lab4class1.cs(9,91): error CS0103: The name 'Minutes' does not exist in the class or namespace 'lab4'
f:lab4class1.cs(9,100): error CS0103: The name 'totalSeconds' does not exist in the class or namespace 'lab4'

[/code]

---------------------------------------------

Any help on this would be appreciated, I'm thinking I should be using reference (ref) instead since I want to use the updated variables, but not sure, even when I try that though I get chalk full of errors.

Thanks guys & gals


Comments

  • MavrixMavrix Posts: 7Member
    Well I took another look at the text we have, and rewrote it a bit based on the one single example they give in the text we are using, but it still came back with similar errors...

    here's how I rewrote it and the errors:

    [code]
    using System;
    public class lab4
    {
    public static void Main()
    {
    int Seconds = 131985;
    secondsMethod(Seconds);
    hoursMethod(Seconds);


    }
    public static void secondsMethod(int Minutes, int Seconds)
    {
    int Minutes = Seconds / 60;
    int Seconds = Seconds % Minutes;
    Console.Out.WriteLine("131985 seconds is {0} minutes and {1} seconds", minutes, seconds);
    }
    public static void hoursMethod(int Minutes)
    {
    int Hours = Minutes / 60;
    int Minutes = Hours % Minutes;
    int Seconds = Minutes % 60;
    Console.Out.WriteLine("131985 seconds is {0} hours {1} minutes and {2} seconds", Hours, Minutes, Seconds);
    }
    }

    f:lab4class1.cs(7,3): error CS1501: No overload for method 'secondsMethod' takes '1' arguments
    f:lab4class1.cs(12,21): (Related location)
    f:lab4class1.cs(14,7): error CS0136: A local variable named 'Minutes' cannot be declared in this scope because it would give a different meaning to 'Minutes', which is already used in a 'parent or current' scope to denote something else
    f:lab4class1.cs(15,7): error CS0136: A local variable named 'Seconds' cannot be declared in this scope because it would give a different meaning to 'Seconds', which is already used in a 'parent or current' scope to denote something else
    f:lab4class1.cs(16,74): error CS0103: The name 'minutes' does not exist in the class or namespace 'lab4'
    f:lab4class1.cs(16,83): error CS0103: The name 'seconds' does not exist in the class or namespace 'lab4'
    f:lab4class1.cs(21,7): error CS0136: A local variable named 'Minutes' cannot be declared in this scope because it would give a different meaning to 'Minutes', which is already used in a 'parent or current' scope to denote something else

    Build complete -- 6 errors, 0 warnings
    [/code]













    : [b][red]This message was edited by Mavrix at 2006-9-30 9:37:26[/red][/b][hr]
    : Hey all, a couple more assignments in, and I've ran into something else our text doesn't explain very well. Anyway, the exersize is:
    :
    : ===================================
    : Design a C# solution with a Main ( ) method that holds an integer variable named seconds to which you will assign a value. Create a method to which you pass this value. The method displays the seconds in minutes and seconds. As an example 66 seconds is 1 minute and 6 seconds.
    :
    : Add a second method to the solution. This method displays a passed argument as hours, minutes and seconds. For example 3666 seconds is 1 hour, 1 minute and 6 seconds.
    :
    : Add a statement to the Main ( ) method so that after it calls the methods to convert seconds to minutes and seconds, it passes the same value to the new method to convert it to hours, minutes and seconds
    : ===================================
    :
    : Our text covered creating multiple methods and passing variables.. The Methods I think I have down ok, but I'm not quite grasping passing variables.. Here's the code that I have, and the errors I'm getting upon compiling.
    :
    : I'm getting so frustrated on just figuring out how to pass the variables, I'm pretty sure I don't have the calculations right, or even properly complete the assignment as it's asking, but once I can figure out how to manipulate the methods that shouldn't be a problem... I hope
    :
    : ---------
    : [code]
    :
    : using System;
    : public class lab4
    : {
    : public static void Main()
    : {
    : int Seconds = 131985;
    : secondsMethod(Minutes, Seconds);
    : hoursMethod(Hours);
    : Console.WriteLine("131985 seconds equals {0} Hours {1} Minutes and {2} Seconds", Hours, Minutes, totalSeconds);
    :
    : }
    : public static void secondsMethod(int Seconds)
    : {
    : int Minutes = Seconds / 60;
    : int totalSeconds = Seconds % Minutes;
    : }
    : public static void hoursMethod(int Minutes)
    : {
    : int Hours = Minutes / 60;
    : }
    : }
    :
    : f:lab4class1.cs(7,17): error CS0103: The name 'Minutes' does not exist in the class or namespace 'lab4'
    : f:lab4class1.cs(8,15): error CS0103: The name 'Hours' does not exist in the class or namespace 'lab4'
    : f:lab4class1.cs(9,84): error CS0103: The name 'Hours' does not exist in the class or namespace 'lab4'
    : f:lab4class1.cs(9,91): error CS0103: The name 'Minutes' does not exist in the class or namespace 'lab4'
    : f:lab4class1.cs(9,100): error CS0103: The name 'totalSeconds' does not exist in the class or namespace 'lab4'
    :
    : [/code]
    :
    : ---------------------------------------------
    :
    : Any help on this would be appreciated, I'm thinking I should be using reference (ref) instead since I want to use the updated variables, but not sure, even when I try that though I get chalk full of errors.
    :
    : Thanks guys & gals
    :
    :
    :

  • bmjcpobmjcpo Posts: 18Member
    Hi,
    there are some mistakes in your code, as said in your posted errors.
    First try to understand this:
    [code]
    using System;
    public class lab4
    {
    public static void Main()
    {
    int numberOfSeconds = 131985;
    secondsMethod(numberOfSeconds);
    }
    public static void secondsMethod(int ParameterNumberOfSeconds)
    {
    int minutes = ParameterNumberOfSeconds / 60;
    int seconds = ParameterNumberOfSeconds % Minutes;
    Console.Out.WriteLine("131985 seconds is {0} minutes and {1} seconds", minutes, seconds);
    }
    }
    [/code]
    The parameters of a method have to own a value. So calling a method like: secondsMethod(parameter) is only possible when you allready declared parameter (for example: int parameter = 12345).

    another example (read the commands in this code!!!):
    [code]
    using System;
    public class testClass
    {
    public static void Main()
    {
    string firstname = "Bob";
    string lastname = "Dylan";
    methodAggregateFirstAndLastName(firstname, lastname);
    }

    private void methodAggregateFirstAndLastName(string fname, string lname)
    {
    //In this method you can do whatever you want
    //but this method only knows the strings fname and lname
    //Because the method makes the strings fname and lname,
    //and fills those with the values which they have before
    //you call the method.

    //for example you can do this:
    string fullname = fname + " " + lname;
    Console.Out.WriteLine("The name is: " + fullname);

    //the output in the console will be:
    //The name is: Bob Dylan
    }
    }
    [/code]

    I hope this will make it a little bit clear. If it isn't clear enough, just ask.

    Good luck.



    : Well I took another look at the text we have, and rewrote it a bit based on the one single example they give in the text we are using, but it still came back with similar errors...
    :
    : here's how I rewrote it and the errors:
    :
    : [code]
    : using System;
    : public class lab4
    : {
    : public static void Main()
    : {
    : int Seconds = 131985;
    : secondsMethod(Seconds);
    : hoursMethod(Seconds);
    :
    :
    : }
    : public static void secondsMethod(int Minutes, int Seconds)
    : {
    : int Minutes = Seconds / 60;
    : int Seconds = Seconds % Minutes;
    : Console.Out.WriteLine("131985 seconds is {0} minutes and {1} seconds", minutes, seconds);
    : }
    : public static void hoursMethod(int Minutes)
    : {
    : int Hours = Minutes / 60;
    : int Minutes = Hours % Minutes;
    : int Seconds = Minutes % 60;
    : Console.Out.WriteLine("131985 seconds is {0} hours {1} minutes and {2} seconds", Hours, Minutes, Seconds);
    : }
    : }
    :
    : f:lab4class1.cs(7,3): error CS1501: No overload for method 'secondsMethod' takes '1' arguments
    : f:lab4class1.cs(12,21): (Related location)
    : f:lab4class1.cs(14,7): error CS0136: A local variable named 'Minutes' cannot be declared in this scope because it would give a different meaning to 'Minutes', which is already used in a 'parent or current' scope to denote something else
    : f:lab4class1.cs(15,7): error CS0136: A local variable named 'Seconds' cannot be declared in this scope because it would give a different meaning to 'Seconds', which is already used in a 'parent or current' scope to denote something else
    : f:lab4class1.cs(16,74): error CS0103: The name 'minutes' does not exist in the class or namespace 'lab4'
    : f:lab4class1.cs(16,83): error CS0103: The name 'seconds' does not exist in the class or namespace 'lab4'
    : f:lab4class1.cs(21,7): error CS0136: A local variable named 'Minutes' cannot be declared in this scope because it would give a different meaning to 'Minutes', which is already used in a 'parent or current' scope to denote something else
    :
    : Build complete -- 6 errors, 0 warnings
    : [/code]
Sign In or Register to comment.