
- VBForums
- .NET and More
- C#
- [RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compilation error..
-
Sep 6th, 2006, 09:51 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compilation error..
I am having a trouble in C# with a very simple method.
the method is supposed to return a boolean value (true or false). Here is the code.
I get compilation error saying that "not all code paths return a value":
public static bool IsMember(List<string> ADGroups)
{
System.Security.Principal.IPrincipal User1;
User1 = System.Web.HttpContext.Current.User;
//check to see if the user trying to access the website is a member of the list of the ActiveDirectory groups that was passed.
foreach (string xx in ADGroups)
{
string sADRole = "fhlbsf-i.com\\"+ xx.ToString();
if ((User1.IsInRole(sADRole) == true))
{
return true;
}
else
{
return false;
}
}
-
Sep 6th, 2006, 10:00 AM
#2
Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..
Your example always returns on the first iteration of the loop - obviously not what you want...
The error is there because if there is nothing contained in ADGroups, then no return statement is executed.
You want something like the following:
VB Code:
public static bool IsMember(List<string> ADGroups)
{
System.Security.Principal.IPrincipal User1;
User1 = System.Web.HttpContext.Current.User;
foreach (string xx in ADGroups)
{
string sADRole = "fhlbsf-i.com\\"+ xx.ToString();
if ((User1.IsInRole(sADRole) == true))
{
return true;
}
}
return false;
}
Last edited by David Anton; Sep 6th, 2006 at 10:02 AM.
Reason: formatting
-
Sep 6th, 2006, 10:39 AM
#3
Thread Starter
Hyperactive Member
Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..
-
Sep 6th, 2006, 03:47 PM
#4
Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..
Can we use [code] tags and resolve threads?
-
Sep 6th, 2006, 05:00 PM
#5
Thread Starter
Hyperactive Member
Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..
I have not put resolve thread since they were not resolved. But I am very deligent about marking resolve thread.
what I am trying to do is, basically I want to put the ActiveDirectory groups (the users only in this group will be able access) into the web.config file.
If any user who doesn't belong to this AD group mentioned in the web.config file would access, he should be re-directed to an error page.
All this can be done using web.config file. Though I am able to restrict the users not belonging to the web.config file, they are not getting re-directed to the error page. they get an access denied error. which is not what I want.
web.config file:
<?xml version="1.0" encoding="utf-8"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<system.web>
<authentication mode="Windows"/>
<identity impersonate="false"/>
<authorization>
<allow roles="nath\CrystalEnterprise-HedgeAccountingManagers"/>
<deny users="*"/>
</authorization>
<customErrors mode="On" defaultRedirect=http://intrane.nathcom>
<error statusCode="401" redirect="http://intranet.nath.com"/>
</customErrors>
<compilation debug="true"/></system.web>
</configuration>
-
Sep 6th, 2006, 05:40 PM
#6
Thread Starter
Hyperactive Member
Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..
what I am trying to do is, basically I want to put the ActiveDirectory groups (the users only in this group will be able access) into the web.config file.
If any user who doesn't belong to this AD group mentioned in the web.config file would access, he should be re-directed to an error page.
All this can be done using web.config file. Though I am able to restrict the users not belonging to the web.config file, they are not getting re-directed to the error page. they get an access denied error. which is not what I want.
web.config file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<system.web>
<authentication mode="Windows"/>
<identity impersonate="false"/>
<authorization>
<allow roles="nath\CrystalEnterprise-HedgeAccountingManagers"/>
<deny users="*"/>
</authorization>
<customErrors mode="On" defaultRedirect=http://intrane.nathcom>
<error statusCode="401" redirect="http://intranet.nath.com"/>
</customErrors>
<compilation debug="true"/></system.web>
</configuration>
-
Sep 6th, 2006, 09:22 PM
#7
Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..
The topic of this thread was resolving the compilation error caused by your code not returning a value on all paths. That topic has been resolved. May I suggest that if you want to ask another, unrelated question that you do so in a new thread with an appropriate title. One topic per thread and one thread per topic keeps the forums easy to use for everyone.
-
Sep 7th, 2006, 04:27 PM
#8
Fanatic Member
Re: [RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compilation error..
Perhaps not wasting space blathering on about forum etiquette would make the forums easier to use for everyone.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Sep 7th, 2006, 08:17 PM
#9
Re: [RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compi
 Originally Posted by DNA7433
Perhaps not wasting space blathering on about forum etiquette would make the forums easier to use for everyone.
Once everyone knows how to use the forums properly and adheres to that etiquette there'll be no need for me to blather on about it.
Last edited by jmcilhinney; Sep 7th, 2006 at 08:20 PM.

- VBForums
- .NET and More
- C#
- [RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compilation error..
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
|