exit/end precedure

cathiec

Freshman
Joined
Mar 8, 2004
Messages
28
is there a way in c# to exit out of a function like in vb (exit function)
i am using a variable and an if statement. if this variable is null i want to exit out of the function without continuing with the rest of the vode in the function.



try
{
SearchResult result = search.FindOne();
//i want to exit the procedure here if result is 0
if(result == null)
{
???????????
}

int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;

for( int propertyCounter = 0; propertyCounter < propertyCount;
propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];

equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}

}
catch(Exception ex)
{
throw new Exception("Error obtaining group names. " +
ex.Message);
}
 
You have to return the type that your function returns.
e.g., in your case, either "return """ or "return null". Even in VB, if you exit from the function without explicitly specifying a value, the function returns the default value for the return type - in your case VB would have returned "".
 
You should download the Instant C# vb.net to c# converter (free demo - www.instantcsharp.com). You'll get the answer to all "vb to c#" type questions just by running an example through the snippet converter:

VB:
public function ReturnString() as string
if SomeCondition then
exit function
else
ReturnString = "ok"
end function

C#:
public string ReturnString()
{
if (SomeCondition)
{
return "";
}
else
{
return "ok";
}
 
As a side note, it's generally considered a horribly bad idea to exit a function that expects a return type and NOT return that type. C# won't let you, VB let's you slide by, apparently (I wonder if there's an option to force you to explicity return something).

As a less interesting side note, you can also return "string.Empty" rather than "".

-nerseus
 
As a side note, (sound like Nerseus)

It's kinda bad programming (I think) to exit a function straight like that. We prefer to do only one return at the end of the function.

You only have to make a string variable (like your return type) and give it the value that it supposed to be when your function run. And when it reach the end... return this variable.

That's a better way of understanding the code... You don't have 25 exit point.

But that's only my opinion no ? :)

Have a nice rainy day ! (In montreal it rain... it s**)
 
thanks for the help everyone!!! i have being doing a little more research and i think that my problem is that FindOne() does not return an empty string or a null. It seems to return the first entry that it finds and skips past the null/empty ones. so my idea of exiting the function wont work here.
 
the function should bring back the groups that the user is part of. it does this but will never return the group "domain users" for any user. that is why i was trying to check to see if result was null and then just default to "domain users". i dont think that approach is going to work. i need to find out why the function will no recognise that the user is a member of "domain users". In the active directory the attribute member of contains the groups that a user is a member of and "domain users" is there but its like the function will not process domain users but will return all other groups. i know this isnt technically a c# question but its driving me crazy!! i would really appreciate any help!
 
Back
Top