Results 1 to 9 of 9

Thread: [RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compilation error..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Resolved [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;

    }
    }

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Post 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:
    1. public static bool IsMember(List<string> ADGroups)
    2. {
    3.    System.Security.Principal.IPrincipal User1;
    4.    User1 = System.Web.HttpContext.Current.User;
    5.  
    6.    foreach (string xx in ADGroups)
    7.    {
    8.       string sADRole = "fhlbsf-i.com\\"+ xx.ToString();
    9.       if ((User1.IsInRole(sADRole) == true))
    10.       {
    11.          return true;
    12.       }
    13.    }
    14.    return false;
    15. }
    Last edited by David Anton; Sep 6th, 2006 at 10:02 AM. Reason: formatting
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Re: [2.0] "Not all code paths return a value" error. how to fix this compilation error..

    thanks a lot

    nath

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    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>

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Unhappy 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>

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] "Not all code paths return a value" error. how to fix this compi

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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