#Using Classes in C#

1 messages · Page 1 of 1 (latest)

winged iris
#

Can you please give more details #📄・posting-guidelines

  • Explain what you're actually trying to do
  • Explain what you expected and what you got
  • Share the Code you have
tidal cipher
#

I used a method in another class to give my output but i want to return to my main (in another class) from where I've left off, the program just crashes if i try to use the return function

winged iris
#
  • Share the Code you have
tidal cipher
#

I dont know how to

winged iris
#

Look in the #📄・posting-guidelines

tidal cipher
#

i have but i dont how to do the methods it says

#

or i already would have

winged iris
#
  1. Write three back tick like this ```
  2. Add the language name ```csharp
  3. Add the code
    ```csharp
    System.Console.WriteLine("Hello World");
  4. Write three back tick at the end
    ```csharp
    System.Console.WriteLine("Hello World");
    ```

Result:

System.Console.WriteLine("Hello World");
winged iris
#

If you can do the ` on your keyboard just copy and past it

#

;compile

public class Calculator {
  int lastResult = 0;

  public int Add(int a, int b) {
    return lastResult += (a + b);
  }

  public int GetResult() {
    return lastResult;
  }
}

public class MyProgram
{
    public static void Main(string[] args)
    {
        Calculator calc = new Calculator();

        calc.Add(5, 5);
        calc.Add(10, 10);

        System.Console.WriteLine(calc.GetResult());
    }
}
ocean roseBOT
#
Program Output
30

winged iris
#

Here's a simple example of using a class with methods that return values

tidal cipher
#
Character character1 = new Character(); 
character1.Name(); //Calls the method in the other class 'Character'
Console.WriteLine("Back at main");

//In the 'Character' Class

    public void Name() 
    {
        bool stop = false;
        string correct = "";

        do
        {
            Console.Clear();
            Console.WriteLine("Enter your name.");
            name = Console.ReadLine();
            Console.WriteLine("You entered " + name + ". Is this correct?");
            Console.WriteLine("Yes/No");
            correct = Console.ReadLine();

            if (correct == "Yes")
            {
                stop = true;
            }
            else
            {
                stop = false;
            }
        } while (stop == false);
   }
#

so I'd just use return to return back to the main??

winged iris
#

It's not exactly what return does. Return simply return a value. If you have no value to return you don't need return.

The way programming work is sequencial. This means that In you mean, each operation will be called one by one. When a method is calls, it doesn't whatever it needs to do and when is done simply stop. Then, in the main will continue by itself.

tidal cipher
#

but then would it output "Back at main" once the method in the foreign class has ran?

winged iris
#

Have you seen my example? You can see that I call Add twice. Add does some calculation and then once is done return the value. But I could modify it and do this.

public class Calculator {
  int lastResult = 0;

  public void Add(int a, int b) {
    lastResult += (a + b);
  }

  public int GetResult() {
    return lastResult;
  }
}

public class MyProgram
{
    public static void Main(string[] args)
    {
        Calculator calc = new Calculator();

        calc.Add(5, 5);
        calc.Add(10, 10);

        System.Console.WriteLine(calc.GetResult());
    }
}

Now, Add does not return anything. But you can see that we still get the same result because the sequence is the same.

In thise case, what happen is this

[MAIN]

  • initalize calculator class
  • call method Add with 5 and 5
    • [CALCULATOR]
    • add 5 and 5 together and add add it to lastResult

[MAIN]

  • call method Add with 10 and 10
    • [CALCULATOR]
    • add 10 and 10 together and add add it to lastResult

[MAIN]

  • call calc.GetResult()
    • [CALCULATOR]
    • fetch and return lastResult
  • print the result
#

The problem in your case isn't that the method isn't returning anything. Your while loop is broken. If you enter your name and type Yes it should work fine. If you write no you willl have a infinite loop. I misread the condition. If it crashes, my guess is that it's elsewhere. Giving me the error message would help finding where.

tidal cipher
#

No i want it like that so the user get the right name

#

If Yes is entered it jumps out the loop

#

But when it jumps out the loop it doesnt return to the main

winged iris
#

Can you show me your main?

#

Do you do anything else after calling Name?

#

As I said, it's sequential. It works step by step. If you don't do anything else after, or do something that throws and error it will stop. I would need both the main and the character class to see if there's an error.

tidal cipher
#

Yeah i can do gimme like half an hour though. Thank a lot

winged iris
#
public class MyProgram
{
    public static void Main(string[] args)
    {
        Calculator calc = new Calculator(); // step 1

        calc.Add(5, 2); // step 2
        calc.Add(8, 12); // step 3

        int result = calc.GetResult(); // step 4
        System.Console.WriteLine(result); // step 5
        
        // no more operations/steps - the program will stop
    }
}