Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
3.80/5 (5 votes)
See more:
Does anyone know how to do the equivelant of this in C#?

#define BEGIN_BLOCK do
#define EXIT_BLOCK break;
#define END_BLOCK while(false);
Posted
Comments
shakil0304003 20-Oct-10 14:09pm    
Good question.
Toli Cuturicu 20-Oct-10 14:10pm    
What programming language is that?
Sauro Viti 21-Oct-10 11:58am    
It's C++, but used in a bad way...

Even if C# supported this kind of stuff, using compiler directives in such a way serves only to obfuscate the code and make it harder for new guys joining the team to come up to speed.

Bad practice, bad technique, and just plain bad idea.
 
Share this answer
 
Here's a more C#-ish way of doing what you did above:

C#
static class ExecuteMultiple
{
    public static bool Execute(params Func<bool>[] functions)
    {
        foreach (var function in functions)
        {
            if (function())
            {
                return false;
            }
        }

        return true;
    }
}

bool SomeFuncionNew()
{
    bool bRetVal = false;

    bRetVal = ExecuteMultiple.Execute
        (
            new Func<bool>[]
            {
                () => badResult1 = Foo1(),
                () => badResult2 = Foo2()
            }
        );

    if (!bRetVal)
    {
        //Do some other clean up work.
    }

    return bRetVal;
}
 
Share this answer
 
v2
Comments
ChuckO813 21-Oct-10 14:08pm    
Thank you. I'll give it a try.
Why would you want to? If you explain that, then perhaps people may be able to give you better solutions.
 
Share this answer
 
Comments
ChuckO813 21-Oct-10 11:37am    
Sure. And thanks for asking.
As soon as you get a bad result you drop out of the code. No need to set bRetVal true, false, or return false in the middle of a function without finishing.
In other words as soon as something bad happens you're out with no further manipulation of bRetVal.

bool SomeFuncion()
{
bool bRetVal = false;
do//BEGIN_BLOCK
{
if (badResult1)
{
break;//EXIT_BLOCK
}
if (badResult2)
{
break;//EXIT_BLOCK
}

bRetVal = true;
} while (false); //END_BLOCK

if (false == bRetVal)
{
//Do some recovery work.
}
return bRetVal;
}
ChuckO813 21-Oct-10 11:42am    
Sorry, I should have cleaned this up a bit.
bool SomeFuncion()
{
bool bRetVal = false;
do//BEGIN_BLOCK
{
if (badResult1)
{
break;//EXIT_BLOCK
}
if (badResult2)
{
break;//EXIT_BLOCK
}
bRetVal = true;
}while (false); //END_BLOCK
if (false == bRetVal)
{
//Do some recovery work.
}
return bRetVal;
}
Nish Nishant 21-Oct-10 11:51am    
Okay, I saw your example, but I don't really get what you would accomplish by using those macros instead of just doing exactly what you did above.
ChuckO813 21-Oct-10 12:08pm    
Just makes the code look cleaner. Thanks.
Nish Nishant 21-Oct-10 12:10pm    
Okay, I guess that's subjective. Anyway in C# people don't do that sort of thing (like they used to in C++). In fact modern C++ programmers are advised to avoid fancy-macros as it makes it a maintenance nightmare and also opens it up for all kinds of subtle bugs in the future.
C# doesn't support preprocessor directives C# doesn't support preprocessor directives in the same way as C++, so I suppose the equivalent would have to be to just write the code each time.
 
Share this answer
 
v2
Comments
Abhinav S 20-Oct-10 13:35pm    
Well they actually do - see http://msdn.microsoft.com/en-us/library/ed8yd1ha(VS.71).aspx.
However, is the OP looking for something he can use from that list?
Anthony Mushrow 20-Oct-10 14:09pm    
Good catch, I was partially right at least.
shakil0304003 20-Oct-10 14:10pm    
I saw your link, it is really interesting.
Not sure but I believe { } curly brackets do the same thing in C#.

There is a #region - #endregion directive. Are you looking for something like this[^]?
 
Share this answer
 
v2
Not answering my own question but wanted to clean this up a bit. So maybe this would be more clear. I've always found this very useful.


C#
bool SomeFuncion()
{
    bool bRetVal = false;
    do//BEGIN_BLOCK
    {
        if (badResult1)
        {
            break;//EXIT_BLOCK
        }
        if (badResult2)
        {
            break;//EXIT_BLOCK
        }

        bRetVal = true;
    } while (false); //END_BLOCK

    if (false == bRetVal)
    {
        //Do some other clean up work.
    }
    return bRetVal;
}
 
Share this answer
 
Comments
Sauro Viti 21-Oct-10 12:09pm    
Assuming that it is useful, you should simply use the keywords provided by the language. A macro that expands to only one keyword and nothing else, is only a way to obfuscate the source code and make harder to understand it for others.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900