cathiec Posted May 31, 2004 Posted May 31, 2004 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); } Quote
wessamzeidan Posted May 31, 2004 Posted May 31, 2004 I think you can use 'return' Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
cathiec Posted May 31, 2004 Author Posted May 31, 2004 i tried this but i get the error: An object of a type convertible to 'string' is required Quote
Jaco Posted May 31, 2004 Posted May 31, 2004 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 "". Quote
Jaco Posted May 31, 2004 Posted May 31, 2004 You should download the Instant C# vb.net to c# converter (free demo - http://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"; } Quote
*Experts* Nerseus Posted June 1, 2004 *Experts* Posted June 1, 2004 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Arch4ngel Posted June 1, 2004 Posted June 1, 2004 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**) Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
cathiec Posted June 1, 2004 Author Posted June 1, 2004 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. Quote
cathiec Posted June 1, 2004 Author Posted June 1, 2004 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! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.