Question Can't understand what is wrong with my code

Marcel1965

New member
Joined
Apr 6, 2023
Messages
3
Programming Experience
Beginner
I study C# with the book "John Sharp - Microsoft Visual C# Step by Step-Microsoft Press (2018) " .

In Chapter 3 there is an exercise "Create and call a method that returns a tuple.

I did all the steps and still i got an other output. Why can't i see any numbers after i push calculate.

Can somebody please help me
 

Attachments

  • exercise code.zip
    4.3 KB · Views: 11
  • expected outcome.jpg
    expected outcome.jpg
    46.7 KB · Views: 8
  • my outcome.jpg
    my outcome.jpg
    65.3 KB · Views: 9
Firstly, please post ONLY the relevant code and post it directly as text, formatted as code. Attaching so little code is pointless and attaching an entire project should be your last resort. It's a waste of our time to download a file, unzip it, open the project in VS and then find the relevant code when we could already be looking at it in your post.

Secondly, you need to debug your code for yourself. You're not just a user, so you don't just run your app as a user. You run the project in the debugger and you use the debugging tools available to you to diagnose the issue. If you don't know how to debug then you should stop what you're doing and learn, because it's something every developer needs to know. You would start by setting a breakpoint at the top of the relevant code and then stepping through it line by line, examining the state at each step and comparing it to your expectations. Whenever the two don't match, you have found an issue. You will probably be able to fix many such issue for yourself but, even if you can't, at least you can explain to use exactly where and how the code behaves other than as you expect. If the code behaves as you expect the whole way through but produces the wrong results then it's you expectations that are the problem.
 
Attaching so little code is pointless

Just wanted to clarify what I mean by this - it sounded ambiguous when I read it myself. If you narrow the code down to just the part(s) relevant to the problem then you will almost certainly have very little code and almost certainly not entire code files, so providing so little code as an attachment is pointless because it's easier to just post it directly. Attachments should be for large amounts of data or data that doesn't display well as text, neither of which should apply to your code that is relevant to the problem. If you feel the need to post large amounts of code, it's generally because you haven't done the appropriate debugging and diagnosis for yourself first. Unfortunately, debugging is not something that is pushed by online tutorials that are geared towards one specific topic or even many courses, which is a dereliction in my opinion. If you don't know how to debug then you really ought to work through a tutorial on that specifically first of all, as it is something that will apply to everything you do.
 
C#:
int division, remainder;
(division, remainder) = divide(leftHandSide, rightHandSide);
result.Text = $" remainder ";

You don't appear to use the calculated results

Perhaps you meant to write

C#:
result.Text = $"{division} remainder {remainder}";

In csharp we typically name methods with an uppercase initial letter; ShowResults etc
 
C#:
int division, remainder;
(division, remainder) = divide(leftHandSide, rightHandSide);
result.Text = $" remainder ";

You don't appear to use the calculated results

Perhaps you meant to write

C#:
result.Text = $"{division} remainder {remainder}";

In csharp we typically name methods with an uppercase initial letter; ShowResults etc

Thank you very much , that was the problem.
 
Firstly, please post ONLY the relevant code and post it directly as text, formatted as code. Attaching so little code is pointless and attaching an entire project should be your last resort. It's a waste of our time to download a file, unzip it, open the project in VS and then find the relevant code when we could already be looking at it in your post.

Secondly, you need to debug your code for yourself. You're not just a user, so you don't just run your app as a user. You run the project in the debugger and you use the debugging tools available to you to diagnose the issue. If you don't know how to debug then you should stop what you're doing and learn, because it's something every developer needs to know. You would start by setting a breakpoint at the top of the relevant code and then stepping through it line by line, examining the state at each step and comparing it to your expectations. Whenever the two don't match, you have found an issue. You will probably be able to fix many such issue for yourself but, even if you can't, at least you can explain to use exactly where and how the code behaves other than as you expect. If the code behaves as you expect the whole way through but produces the wrong results then it's you expectations that are the problem.

Sorry I wasted your time.
As for the amount of code this is the only code I have from that exercise, that's all there is to it.
But in order not to waste more of your time from now on, I will ask my questions elsewhere.
 
Recall that people on this forum contribute purely voluntary. So the more you do to make it easier for people to be able to help you, the more likely you will get responses.

Although the ancient page referred to below maybe out of date with the way most modern forums work, there is still a lot of relevant information about researching and preparing your question.

 
in order not to waste more of your time from now on, I will ask my questions elsewhere.

jmc contributes so massively to so many forums and q&a facilities that you may not succeed in avoidance.. While more more terse than some, the advice he posted here is merely imploring you to adopt a strategy of making yourself as easy to help as possible, and worth heeding.

This problem in particular, would likely have succumbed to a quick investigation with the debugger; if you aren't familiar with the notion of pausing a program while it is running and looking at the values of variables and working out how they came to be this, it's a worthwhile exercise to get into. You'd have run your code, paused it on the last line, pointed to the division and remainder variables, and seen they had a value, pointed to Text and seen it to be wrong, then shortly after asking yourself "so if that is correctly 3, why did the value 3 not make it into the string that got assigned to Text?".. and then I suspect you'd have seen that it couldn't
 
Back
Top Bottom