Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# This assignment is already over but I have some confusion mainly in the try/catch portion of the method MedalWinner(). I keep getting an error

C#

This assignment is already over but I have some confusion mainly in the try/catch portion of the method MedalWinner(). I keep getting an error saying "Use of unassigned local variable 'result'".

Thanks in advanced for the help!

image text in transcribed

image text in transcribed

Debugging in the Console App Create a new Console App named DebuggerLastName Code Add the following code to the Main method, add your first and last name on the final comment line. Read the comment, it informs you of the intent of the code: static void Main(string[] args) { // Call a method that returns // user's first name first letter capitalized only // and the last name all capitalized followed by rank string winner1 = MedalWinner("Megan", "rapinoE", "1"); string winner2 = MedalWinner("USA", "Women's Soccer","1"); string winner3 = MedalWinner ("USA", "Women's Soccer", "1.5"); Console.WriteLine("And the winner is {O:G}", winneri); Console.WriteLine("Expected: Megan RAPINOE 1"); Console.WriteLine("And the winner is ... {O:G}", winner2); Console.WriteLine("Expected: Usa WOMEN'S SOCCER 1"); Console.WriteLine("And the winner is ... {0:G}", winner3); Console.WriteLine("Expected: Unable to set rank"); } Add the method Medal Winner() above Main: private static string MedalWinner(string fName, string lName, int rank) { string result; result = fName + Name + "" + Convert.ToInt32(rank).ToString(); return result; } Notice that you cannot run the code in its current form. It's time to start Debugging! Compile Error Notice the underlines in the Method calls to Medal Winner with the "1", "1", and "1.5". That is because those are strings. The method parameterlist indicates that rankis an integer (int rank). There are two possible fixes. Make them integers OR change the method signature to string rank. When you make this change, you are deciding what rank could be, should it be an int? Can be anything else for this use of Medal Winner? For this lab assume the user is only sending the method strings. This is a fair assumption for input, especially in the Console App environment you are working with. In GUIs you often can limit the input type to numbers. You have seen this in Web applications for input like phone numbers and dates. Break Point Run the program once you have fixed the compile error. Notice the Unhandled Exception: returs result Exception Uimhandled Syaration puring was not in acest format View Details Copy Detail store shore Baption Gaming Set a break point in the method Medal Winner. . Take a screen shot of the break point, make sure the breakpoint and all the code is visible. (Screenshot #1) Run the code and take a screen shot with the code stopping at the error, make sure the code window is visible (Screenshot #2) . Debug Coding involves a lot of debugging, finding errors and fixing them. . o Fix the unhandled exception by adding try/catch code in the method Medal Winner() 0 Add try to one line only, keep the return statement below the try/catch Be sure to catch the appropriate Exception. You will see it as the Exception Unhandled. In catch, set result to return "Unable to set rank." This business logicis also visible in the Main method WriteLine of Excepted output. Fix the business logic (fist name format and last name format) The comment tells you // user's first name first letter capitalized only // and the last name all capitalized followed by rank Do not change the method call in the Main Take a screen shot of the code with ALL errors fixed, make sure the code window is visible (Screenshot #3) o . . Notice the types of errors you fixed. First, a compile error, next an unhandled exception, and finally a business logic error. The end user informs the business logic (what the program should do, how the input should look, etc). First you used the IDE itself, to notice you could not compile the code. Once you could compile the code, you found the unhandled exception in the IDE. Finally, you used the IDE's debugger to help fix exceptions and the business logic errors. Debugging is an important part of programming. Submit all 3 Screenshots, either in a doc file, pdf file, or as 3 separate screenshots

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions