-
August 5th, 2012, 09:25 PM
#1
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?
-
August 5th, 2012, 09:37 PM
#2
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".
-
August 5th, 2012, 09:52 PM
#3
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?
Last edited by Danja91; August 5th, 2012 at 09:54 PM.
-
August 5th, 2012, 10:40 PM
#4
Re: Can I pass a new bool as a reference?
After each call to 'search', I would add:
if (breaknow)
break;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|