If-else construct not working

Rinz

Member
Joined
Apr 9, 2020
Messages
21
Programming Experience
Beginner
I need a volunteer to review a C# code. Just some few issues with if-else construct not flowing as expected. Am very new to programming. Please advise.
 
Yes. Re-read that post that you just quoted.
 
If you want to understand why your code is not working. You should; and I implore you to run this code and tell me after you have read the Output window. Why foo and bar are no longer in the string array when foobar is initialised?
What seems obvious when you then look at this sample code code? This code is no different to the mistake you just made.

Once you work that out, then look at your own code and then tell us what the problem is?

C#:
        internal static void Main(string[] args) => new Thread(() => Request_Response()) { Name = "Executor" }.Start();
        internal static void Request_Response()
        {
            string[] args = new string[3];
            args[0] = "foo";
            args[1] = "bar";
            foreach (string str in args)
            {
                if (!string.IsNullOrEmpty(str))
                    Debug.WriteLine($"Before declaring args new string[] {str}");
            }
            args = new string[] { "foobar" };
            foreach (string str in args)
            {
                Debug.WriteLine($"After declaring args as new string[] {str}");
            }
        }
I've just had a bottle of wine, and I can still see your issue. :)
 
If you want to understand why your code is not working. You should; and I implore you to run this code and tell me after you have read the Output window. Why foo and bar are no longer in the string array when foobar is initialised?
What seems obvious when you then look at this sample code code? This code is no different to the mistake you just made.

Once you work that out, then look at your own code and then tell us what the problem is?

C#:
        internal static void Main(string[] args) => new Thread(() => Request_Response()) { Name = "Executor" }.Start();
        internal static void Request_Response()
        {
            string[] args = new string[3];
            args[0] = "foo";
            args[1] = "bar";
            foreach (string str in args)
            {
                if (!string.IsNullOrEmpty(str))
                    Debug.WriteLine($"Before declaring args new string[] {str}");
            }
            args = new string[] { "foobar" };
            foreach (string str in args)
            {
                Debug.WriteLine($"After declaring args as new string[] {str}");
            }
        }
I've just had a bottle of wine, and I can still see your issue. :)
Lol. Thanks. I have looked at your code. I sort of understand the logic but could not utilize the debug console effectively to figure out the flow. I will look closely at your spoiler for further instruction on C# programming.
 
Yes. Re-read that post that you just quoted.
What do you advise? Please find the attached. Same algorithm was called (directly from cmd) and the algorithm appears to have executed correctly though not sequentially (Here, It took all the parameters at once). I want to be able to utilize ```args.Length>1``` so that other parameters can be considered from the console.
 

Attachments

  • Console_Debug III.png
    Console_Debug III.png
    71.4 KB · Views: 11
I sort of understand the logic but could not utilize the debug console effectively to figure out the flow.
Add this using directive to the top of your code file.
using System.Diagnostics;
Then open the Output tab by going to view => Output or Crtl + W
What do you recognise as the problem with the printed data?

Because you are new, you will need to learn to troubleshoot this yourself. Because, if you had read the docs on Microsoft's site (MSDN) regarding initialising and re-initialising objects, you would have been able to spot the problem.
 
Did you read just the first page of @Sheepings 's debugging link, but not the succeeding pages? You should read all of them. But here's a quick hint on how to inspect variables while stepping through the code (recall that stepping through the code was in the first page):

Now examine the value of args right after you execute the line that says:
C#:
args = new String[] { Console.ReadLine() };
(That was line 37 of your post #11, or line 54 of your screenshot from post #15.)

What are the values of the args array? How does that compare to your expected "--input-image" on line 39 of post #11 or line 56 of post #15?
 
Now as something completely meta to your current code: Why are you using Console.ReadLine() to interactively get input from the user?

By convention the variable named args of a Main() method refers to the the command line arguments passed to the program. This means that these are the parameters passed to the program when you run the program in the CMD or PowerShell console. You are going to confuse other people who read (and use) your code when they see that your Main() takes command line arguments, but completely ignores them and instead interactively gets input from the user, but expects the user to type in the input as if they were command line parameters.
 
@Skydiver I think it is better to stick to the issue of why the values are no longer in the method after being first initialised. I think it would be better for our OP to first understand their reported problem, before pointing out what they are doing with the command args may also be a potential mistake. I also feel that our OP also plans to provide the .exe "as is" and not use it externally with a shell of any sort. But just to note, I completely do get your point in which you're making. +1 there!
 
Yes. Re-read that post that you just quoted.
Hello, Thanks again for the assistance so far. Can you advise how I can parse this line ``` args = new String[] { Console.ReadLine() };``` such that the parameters supplied can be split to array elements. args[0], args[1], args[2] etc
 
Now as something completely meta to your current code: Why are you using Console.ReadLine() to interactively get input from the user?

By convention the variable named args of a Main() method refers to the the command line arguments passed to the program. This means that these are the parameters passed to the program when you run the program in the CMD or PowerShell console. You are going to confuse other people who read (and use) your code when they see that your Main() takes command line arguments, but completely ignores them and instead interactively gets input from the user, but expects the user to type in the input as if they were command line parameters.
I really appreciate this. The program is to be deployed as a Console Application. I am meant to improve the current implementation of the present algorithm. The task is to ease up the program flow and make it more user friendly without tampering with the core libraries. I introduced a variable to be able to assess the value of args.length and I noticed the value is always 1 regardless of the different parameters provided. Do you think I can correct this with the <args.String.Split (' ').ToArray();> function?
 

Attachments

  • VS_Debug IV.png
    VS_Debug IV.png
    37.5 KB · Views: 11
Do you think I can correct this with the <args.String.Split (' ').ToArray();>
No.

You're creating the same problem. In order to understand what you're doing wrong, you need to go back to post #17, and test the code I gave you. Why? Because the mistake you are making, I've already wrote out. Now if you had used and tested the code in a new application, and actually debugged it, you would have found what you are doing wrong. I even have the debug write back to the output window in VS for you to identify the output provided by the method. Put a break point on Request_Response and step through the example I gave you and try to identify why foo and bar don't print after args is reinitialised. I and other have already pointed out what you're doing wrong, and I've also provided at least three different links, which if you had read; it would be abundantly clear what the problem is. So read back through the topic from post 13 onwards.
 
No.

You're creating the same problem. In order to understand what you're doing wrong, you need to go back to post #17, and test the code I gave you. Why? Because the mistake you are making, I've already wrote out. Now if you had used and tested the code in a new application, and actually debugged it, you would have found what you are doing wrong. I even have the debug write back to the output window in VS for you to identify the output provided by the method. Put a break point on Request_Response and step through the example I gave you and try to identify why foo and bar don't print after args is reinitialised. I and other have already pointed out what you're doing wrong, and I've also provided at least three different links, which if you had read; it would be abundantly clear what the problem is. So read back through the topic from post 13 onwards.
Ok. Thanks: I will get to it and update shortly.
 
Back
Top Bottom