2

I am working with C# for the first time and I am facing a strange issue.

I am building my own class for a plugin, but copied parts of the code from an existing class. Basically it's

var sanInput = Console.ReadLine();
alternativeNames = sanInput.Split(',');
sanList = new List<string>(alternativeNames);

but somehow this doesn't work. The debug console says System.Console.ReadLine returned jogi,philipp string, but sanInput keeps null as its value.

Even stranger is the fact, that the next step works "a bit". string.Split returned {string[2]} string[], so it returns an array of [jogi, philipp], but still sanInput, alternativeNamesand sanList stay as null.

How is it possible, that the second line works if sanInput has no value and how can I fix this problem? When I work with the existing class with the same code everything works as expected.

//EDIT: Looks like a quite complicated issue. Here is the complete method:

    public override void HandleMenuResponse(string response, List<Target> targets)
    {

        if (response == "r")
        {
            Console.WriteLine("Which hosts do you want to configure? Enter numbers separated by a comma.");
            var hostsInput = Console.ReadLine();
            int[] hosts = null;

            string[] alternativeNames = null;
            List<string> sanList = null;

            hosts = hostsInput.Split(',').Select(int.Parse).ToArray();
            Console.Write("Generating certificates for ");
            foreach (int entry in hosts)
            {
                Console.Write(targets[entry - 1].Host + ", ");
            }
            Console.Write("\n \n");

            foreach (int entry in hosts)
            {
                int entry2 = entry - 1;

                if (Program.Options.San)
                {
                    Console.WriteLine("Enter all Alternative Names for " + targets[entry2].Host + " seperated by a comma:");
                    // Copied from http://stackoverflow.com/a/16638000
                    int BufferSize = 16384;
                    Stream inputStream = Console.OpenStandardInput(BufferSize);
                    Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, BufferSize));

                    var sanInput = Console.ReadLine();
                    alternativeNames = sanInput.Split(',');
                    sanList = new List<string>(alternativeNames);
                    targets[entry2].AlternativeNames.AddRange(sanList);
                }

                Auto(targets[entry - 1]);
            }
        }


        if (response == "e")
         {
             string[] alternativeNames = null;
             List<string> sanList = new List<string>();

             if (Program.Options.San)
             {
                 Console.WriteLine("Enter all Alternative Names seperated by a comma:");
                 // Copied from http://stackoverflow.com/a/16638000
                 int BufferSize = 16384;
                 Stream inputStream = Console.OpenStandardInput(BufferSize);
                 Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, BufferSize));
                 var sanInput = Console.ReadLine();
                 alternativeNames = sanInput.Split(',');
             }

             if (alternativeNames != null)
             {
                 sanList = new List<string>(alternativeNames);
             }

             foreach (var entry in targets)
             {
                 Auto(entry);
             }
         }   
    }

I know the code isn't pretty and efficient. All in all it let's the user decide if he wants to use all detected hosts (response e) or only single ones (response r). But the mentioned problem occurs only in the second if-method. If I switch them it's again the latter one. So maybe the reason lies in the main program or in this BufferSize-Stuff? I don't know.

//EDIT 2: I think I found the problem: Somehow the integer BufferSize (shortly before the Console.Read()) is set to 0, so of course without any buffer it can't read the input. So the question remains: Why?

//EDIT 3: Okay, I'm done. It looks like I can't use the same name for the variables although they are in two different if-methods. I just named them sanInput2, alternativeNames2 etc. and now everything works.

der_eismann
  • 355
  • 1
  • 3
  • 8
  • 2
    Please show where and how is `sanList defined`. I run a sample with the very same 3 statements you put here (except for the fact that I declare sanInput as string and assign it jogi,philipp) and sanList has 2 elements. Show the entire function where this block is defined. – Veverke May 30 '16 at 10:39
  • I compiled your snipped with the addiditon that I put `var` before each not defined variable and it worked as expected see: [**here**](http://www.tutorialspoint.com/compile_csharp_online.php?PID=0Bw_CjBb95KQMbFpLUVpiUm53MW8). So I think yoru problem could be a variable definition one. – Maximilian Ast May 30 '16 at 10:43
  • Could you please be a bit more specific as to what is failing in your code. I ran your code with proper datatypes and it works just fine. Check the code here - https://dotnetfiddle.net/IwFc31 – bala May 30 '16 at 10:45
  • Are you running in debug mode or release mode? Debugging in release mode often will muddle the debugger. – Matt Houser May 30 '16 at 11:15
  • Exactly what error are you getting? What line of code? – Matt Houser May 30 '16 at 11:23
  • Hey guys, I updated the question. I think the reason is that the program sets the BufferSize to 0, but I have no idea why. – der_eismann May 30 '16 at 11:45
  • Why are you defining sanInput etc inside the if block, why aren't you defining them outside the if block? – RobertKenny Jun 02 '16 at 14:39

2 Answers2

0

try this, all of the variables are having values(you can use var also for all the variables):

   var sanInput = Console.ReadLine();
   string[] alternativeNames = sanInput.Split(',');
   List<string> sanList = new List<string>(alternativeNames);
sumngh
  • 566
  • 2
  • 10
0

The problem you mention is, that debugging code in VS do assignements in two steps. First is to execute Console.ReadLine() (therefore you see Console.Readline returned message) and AFTER that is is assigned into sanInput. Same situation is after Split. Function is called, but not assigned yet. My recommendation: use rather the step over instead of step inside. After time, you get used to this functionality and appreciate it.

Zoka
  • 2,312
  • 4
  • 23
  • 33