0

The idea is to reuse the string that was used in the previous iteration of the loop.

public void StringMain()
{
    string temp;
    int times = 0;

    do
    {
        string input;
        string reuse;

        if (times == 0)
        {
            input = Request();
            times++;
            temp = input;
        }

        else if (times > 0)
        {
            reuse = Reuse();

            if (reuse == "Y" || reuse == "y")
            {
                input = temp;
                // error here: unassigned local variable temp
            }
            else
            {
                input = Request();
                temp = input;
            }
        }
        // Do stuff with string

} while (Console.ReadLine() != "Q" || Console.ReadLine() != "q")

I thought that by equating the string temp variable to the initial input and storing the new temp outside of the do loop, I can set the input to the temp variable in the next iteration without having the temp variable be reset, should the user request it. (Reasoning > Default string initialization is empty). Thereby effectively copy pasting the previous string.

However, I get an error: unassigned local variable on temp in the noted line. I understand why, temp has no value at the moment but it will get a value after the first iteration.

Can I make it happen like this or have I approached this in an entirely wrong way?

If I have how do I copy the string used in the previous iteration of the loop?

The Request() and Reuse() methods just return strings and ask for user input. They are below if it's of use:

private string Request()
{
    Console.WriteLine("Input String:");

    return Console.ReadLine();
}

private string Reuse()
{
    Console.WriteLine("Reuse previous string?");
    Console.WriteLine("Y - N?");

    return Console.ReadLine();
}

Note: I do not want to use any predefined methods if at all possible.

I've looked at the following questions but they both pertain to arrays and are in java. They don't really even get close to what I'm trying to do neither so I wasn't able to use the same concepts.

Button to show previous string

add string to string array and display last/previous string in that string array

Thanks in advance!

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
James9oo0
  • 159
  • 12
  • Isn't this useless because strings in .net are immutable ? – slow Feb 19 '18 at 10:01
  • @sLowDowN No not really its still possible, have a read through [this answer](https://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning) – Master Yoda Feb 19 '18 at 10:05
  • @MasterYoda I meant the reuse of the string is useless because whatever he does, it always creates a new one because its immutable. – slow Feb 19 '18 at 10:09
  • Does changing `string temp;` to `string temp = "";` work? – mjwills Feb 19 '18 at 10:11
  • @sLowDowN Thats not going to make any difference though. Its just another reference pointer to a new string. So long as the new value is stored the compiler doesn't care what gets passed to it. Further down the line he may find that all of these references to new strings could cause memories or sync problems but for the most part it should work. – Master Yoda Feb 19 '18 at 10:15
  • @MasterYoda of course it could work, but i thought the question is to "reuse" the string? But the memory will not be reused again. That's what I was refering too. Or did I understand something wrong? – slow Feb 19 '18 at 10:17
  • @MasterYoda Lmao after spending an hour and a half trying to find an answer, 30 mins constructing the question I find that adding an equal sign and double quotes solves the problem :"D. Thanks a lot! – James9oo0 Feb 19 '18 at 10:19
  • @mjwills Yes, yes it did. – James9oo0 Feb 19 '18 at 10:19
  • @James9oo0 No problem, Hadnt realised i deleted my first comment :/ – Master Yoda Feb 19 '18 at 10:21
  • @sLowDowN the question referred to the contents of the string I guess? Honestly, I'm not fully understanding what you guys are talking about, I have a long way to go yet. – James9oo0 Feb 19 '18 at 10:22
  • @sLowDowN I think you might be looking too deep into the question although I can understand what you meant :) – Master Yoda Feb 19 '18 at 10:22
  • 1
    well, i guess that i misunderstood the question then. sorry :) – slow Feb 19 '18 at 10:25
  • @sLowDowN I'm just glad people are helping man, I might end up revisiting this question when I understand more ¯\_(ツ)_/¯ – James9oo0 Feb 19 '18 at 10:30

1 Answers1

0

The immediate reason for the error is the unassigned temp string. However, the deeper cause is too complex code, keep it simple:

// bool: Reuse is supposed to return true/false (i.e. yes/no) 
// static: You don't want any "this" in the context
private static bool Reuse() {
  Console.WriteLine("Reuse previous string?");
  Console.WriteLine("Y - N?");

  return Console
    .ReadLine()
    .Trim()     // Be nice and trim out spaces (esp. trailing ones)
    .Equals("y", StringComparison.OrdinalIgnoreCase);
}

public void StringMain() {
  //bool: we don't want count, say 15-th input, but a fact is it a first try
  bool firstTime = true; 
  string temp = null;    // Make compiler be happy, assign the local variable

  do {
    // We can reuse if and only if
    //  1. It's not the first Time run
    //  2. We allow it, i.e. Reuse() returns true
    input = !firstTime && Reuse() 
      ? temp
      : Request();

    firstTime = false;
    temp = input;

    // Do stuff with string 
  }
  while (!Console.ReadLine().Trim().Equals("q", StringComparison.OrdinalIgnoreCase));
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215