CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    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?

  2. #2
    Join Date
    Aug 2005
    Posts
    198

    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".
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  3. #3
    Join Date
    May 2010
    Location
    .Net 4.0
    Posts
    58

    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.

  4. #4
    Join Date
    Aug 2005
    Posts
    198

    Re: Can I pass a new bool as a reference?

    After each call to 'search', I would add:
    if (breaknow)
    break;
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured