Click here to Skip to main content
15,914,221 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone,
I tried to return a string, but always there is a Problem.
This is the Task => https://pastebin.com/9pAmB0KU
I tried to convert time from AM/PM to military(24-Hour).
There is a solve in a void method => https://pastebin.com/DhULUe25
I try to this => https://pastebin.com/a9RjanZc

Thank you in advance!

What I have tried:

I tried to return a string, but there is always get Unhanled Exception
Posted
Updated 12-Jan-22 23:25pm
Comments
Georgiev Tihomir 13-Jan-22 5:14am    
Thanks for the clarifications on how to use the site, today I did the registration here. But as a comment I think, that I can not add code

Strings in C# are immutable - they cannot be changed once cretaed, and and attempt to change it creates a new string, just like it works with integers:
C#
int x = 666;
int y = x;
x = x + 1;
console.WriteLine($"{x}:{y}:{666}");
Will always print "667:666:666" because anything else would make life very difficult indeed!
Similarly with strings:
C#
string x = "Hello";
string y = x;
x = x + " world!";
Console.WriteLine($"{x}:{y}:{"Hello"});
Will always print "Hello World!:Hello:Hello" because the attempt to modify x created a new string, not modified the original.

In addition, in C# when you call a method, all the parameters you pass are passed by value, not by reference - which just means that the method gets a copy of the content of a parameter rather than the variable itself, and any changes made inside the method do not affect the "outside world".
Again, that's really necessary:
C#
void Foo(int x)
   {
   x = x + 1;
   Console.WriteLine(x);
   }

Call that and see what happens:
C#
inx x = 666;
Foo(x);
Console.WriteLine(x);
You get two lines:
667
666
Because the change inside the method affected the copy, not the original variable. Again, that makes sense - if it didn't work that way what would happen if you called it with a constant value:
C#
Foo(666);
Console.WriteLine(666);
If the Foo method changed the external value so you got thsi printed:
667
667
Then every reference in your code to the constant 666 would now use 667 instead, and life would become very complicated indeed!

So your method returning void can't update the "outside world" with the new value and you do ned to return a string in order to pass it back to the caller.
C#
static string Foo(string s)
   {
   return s + " World!";
   }

C#
string x = "Hello";
x = Foo(x);
Console.WriteLine(x);
Will print "Hello World!"

The "unhandled exception" is nothing to do with that, it's probably that what your user typed is not what you expected. Use the debugger: put a breakpoint on the line
C#
DateTime d = DateTime.Parse(s);
And step through looking closely at what happens.

And in future, pats yoru code directly into your question using the "code block" paste option instead of posting it elsewhere and expecting other to go there and look. It's a waste of your time and ours! (and many people won't go near links from newbies because you just don't know what will be on the other end ... I ran up a VM just to open them!
 
Share this answer
 
Comments
Maciej Los 13-Jan-22 12:55pm    
Excellent!
The description of Task is =>
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

timeConversion has the following parameter(s):

string s: a time in 12 hour format
Returns

string: the time in 24 hour format

Input Format
A single string s that represents a time in 12-hour clock format (i.e.:hh:mm:ssAM or hh:mm:ssPM).

Constraints
All input times are valid

Sample Input
07:05:45PM

Sample Output
19:05:45


And the code is =>
C#
using System;

class Result
{

    /*
     * Complete the 'timeConversion' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */

    public static string timeConversion(string s)
    {

    }

}

class Solution
{
    public static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        string s = Console.ReadLine();

        string result = Result.timeConversion(s);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}


When I write in a void method, there is no Problem, but this return string..I will "kill my self" :)
 
Share this answer
 

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