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!