Can I pass a new bool as a reference?
I have a quick question about how to condense two lines into one.
The following code works:
bool pass = false;
MyFunction(ref pass);
I would like to turn it into the following:
MyFunction(ref new bool(false))
But that doesn't work in the compiler. Is there a correct syntax for this?
Re: Can I pass a new bool as a reference?
The compiler error says it all - "a 'ref' or 'out' argument must be an assignable variable".
Re: Can I pass a new bool as a reference?
That's a little bit annoying. In that case, could you tell me if there's a better way to execute the following?
Code:
int main()
{
List<string> results = new List<string>();
Console.WriteLine("Press ESC to skip the current drive");
for (char i = 'A'; i <= 'Z'; i++)
{
string search = i + ":\\";
Console.WriteLine("Searching through drive {0}", search);
Console.SetCursorPosition(0, Console.CursorTop - 1); // replace previous statement
bool ESCnow = false;
results = search("searchforthis", ref ESCnow);
}
}
List<string> search(string searchstring, ref breaknow)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape) { breaknow = true; }
if (breaknow == true) { return; }
// execute my specialized search statements here
DirectoryInfo NewDirInf = new DirectoryInfo(Directory.GetCurrentDirectory());
foreach (DirectoryInfo SubDirInf in NewDirInf)
{
search (searchstring, ref breaknow);
}
}
Is there a better way to break out of the search function than via the ref bool?
Re: Can I pass a new bool as a reference?
After each call to 'search', I would add:
if (breaknow)
break;