Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: "not all code paths return a value"[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    "not all code paths return a value"[Resolved]

    What's wrong here ? It shows zigzag line under "PopulateMainCat" and the error msg says : "not all code paths return a value"

    VB Code:
    1. public object PopulateMainCat(string MainTableCat)
    2. {
    3. OpenDB();
    4. DataSet ds =new DataSet();
    5. string sqlstr ="SELECT * FROM " + MainTableCat + "";   
    6. OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);
    7.  
    8. adp.Fill(ds,MainTableCat);
    9. foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
    10. {
    11. return dr[1];
    12. }
    13. DBSpace.CloseDB.CloseDB1();
    14. }
    Last edited by Pirate; Jun 27th, 2003 at 10:44 AM.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Why are you putting your return statement in a loop?? The return statement should be the last line in the function. Thats why you are getting that message.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I want to return datarow obj . you mean I can't do it this way ??

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    btw , I did something similar (return datarow array) in VB.NET , with almost same code . so ...

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Originally posted by Pirate
    I want to return datarow obj . you mean I can't do it this way ??
    If the return statement is in the loop, that loop will only iterate once. On the first go, when it gets to the return statement it will break out of that function.

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by DevGrp
    If the return statement is in the loop, that loop will only iterate once. On the first go, when it gets to the return statement it will break out of that function.
    It makes sense . I'll try other ways if possible . Thanks

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I can't find else ways to return datarow object !
    anyone can hlep me ?

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    What are you trying to return??

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    DataRow objects . In this code it's " dr[1] " .

    foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
    {
    return dr[1];
    }

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Why dont you just return the dataset or the datatable?

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by DevGrp
    Why dont you just return the dataset or the datatable?
    Simply , I was doing adding the values returned from datarow into comboBox . Now , I thought I would need my function to be more dynamic so that I CAN add these values into listBox , CheckedListbox , listview ...etc . I know I can use overloading here but I'm sure there is another way (I prefer one function rather than 6 functions) . Returning DataSet or DataTable is not a solution , it would be the same thing I'm doing right now .

  12. #12

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Fixed . You're right DevGrp . Returning datatable is a good idea .

    Thanks for your help .

  13. #13
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    No prob

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    or just put the code lile u have in the beggining and in the end put a "return null;" but u'll have to check whenever the thing is null or not, if not null you can use, if null you know you cant use it
    \m/\m/

  15. #15

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Even though , I have to return that at the lower line of my function (last line ) .

  16. #16
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yes of course..if u say it to return something what would happen if it wouldnt return? it'd be very stupid..dont u think?
    \m/\m/

  17. #17

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I can't see your point ...lol . If I don't want it to return a value , then I would consider to make it void .

  18. #18
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    my point is just what u said, if u want to return nothing then u put it to void, but your code could return nothing(void) and if that happened all the prog would crash so there is REALLY a point in what the compiler does by checking if all paths return a value
    \m/\m/

  19. #19

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    The point is C# compiler is just a dumb . Why this does not happen in VB ? It's fuc king sensitive .

  20. #20
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    the point is vb.net compiler is ****..if ur app crashes the problem is yours because the STUPID compiler didnt point in the beggining the error..C# compiler just wont allow code that might be dangerous while vb.net one allows..
    \m/\m/

  21. #21

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    What's so dangerous here ??? This function is actually returning a value . It's generating that stupid error . What the hell ! while VB Compiler is clever enough and know it's returning a value . dude there is no danger at all .

    public object PopulateMainCat(string MainTableCat)
    {
    OpenDB();
    DataSet ds =new DataSet();
    string sqlstr ="SELECT * FROM " + MainTableCat + "";
    OleDbDataAdapter adp =new OleDb.OleDbDataAdapter(sqlstr,MyConnection);

    adp.Fill(ds,MainTableCat);
    foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
    {
    return dr[1];
    }
    DBSpace.CloseDB.CloseDB1();
    }

  22. #22
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    suppose there are no datarows in your dataset. then the foreach() loop is never entered, nothing is returned, and your program dies ungracefully.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  23. #23
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yea that was it

    also
    Code:
    adp.Fill(ds,MainTableCat);
    foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
    {
    return dr[1]; 
    }
    DBSpace.CloseDB.CloseDB1();
    }
    the code that is after return will never be used so think about puting it before the return statement!
    \m/\m/

  24. #24

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by sunburnt
    suppose there are no datarows in your dataset. then the foreach() loop is never entered, nothing is returned, and your program dies ungracefully.
    Hmm, What about this one here . It suppose to return null (nothing and it works fine )

    C#
    Code:
    public object a()
    {
    return null ;
    }

  25. #25
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    whats wrong in that? in the code of the for each ur returning VOID and in that u're returning NULL, its different things
    \m/\m/

  26. #26
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    right, because there's a return statement. this is more along the lines of what i'm talking about:

    Code:
    public object a()
    {
       
    }
    or, what the compiler is noticing in your previous example:

    Code:
    public int a(int myValue)
    {
         if ( myValue == 2) // this may not be true
              return 4;
    
        // so if it isn't...
        // no value is returned.
    
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  27. #27

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by sunburnt
    suppose there are no datarows in your dataset. then the foreach() loop is never entered, nothing is returned, and your program dies ungracefully.
    You are contradicting yourself here !

  28. #28
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    err no he isnt! where? if u say it because this:
    Code:
        // so if it isn't...
        // no value is returned.
    actually that code wont compile as an error will be thrown
    \m/\m/

  29. #29

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Here .
    suppose there are no datarows in your dataset. then the foreach() loop is never entered, nothing is returned, and your program dies ungracefully.

    I wonder why the below code won't work . It really gives compiling error . although return statement is at the last line .

    Code:
    public int a(int myValue)
    {
         if ( myValue == 2) // this may not be true
              return 4;
    
        // so if it isn't...
        // no value is returned.
    
    }

  30. #30
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ok i see where is your problem..i think that u dont know that in c# (and c++ and java and what-so-ever) if your if-clause wont exceed a single line you dont really need to put the brackets..so:
    Code:
    public int a(int myValue)
    {
         if ( myValue == 2) // this may not be true
              return 4;
    
        // so if it isn't...
        // no value is returned.
    
    }
    its the same as..
    Code:
    public int a(int myValue)
    {
         if ( myValue == 2) {// this may not be true
              return 4;
        }
        // so if it isn't...
        // no value is returned.
    
    }
    so as you can see it will throw an error as no value is returned!
    \m/\m/

  31. #31

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's completely wrong . wrong . What about this one below : It's returning damn nothing and you are saying "so as you can see it will throw an error as no value is returned!" . Who is wrong here ???
    Code:
    public object a()
    {
    return null ;
    }

  32. #32
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    This is fun

  33. #33
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    public object a()
    {
    return null ;
    }


    its returning null..so? whats the point? if u want to return null then return it! the compiler in loops etc just doesnt know what to do!

    if u are that guys that prefer that the compiler allows all the unsafe code ok! go to the options and set the warning/error level to 0(or 5 i dont remember) but then dont get mad when your customers get mad at you just because your code contains a lot of possible dangerous code and is crashing all the way, its just like one convert one type of var to another, you might see there is absolute no danger in that but the compiler will thrown an error saying that you MUST explicitly convert it so it knows it REALLY is gonna work
    Last edited by PT Exorcist; Jun 29th, 2003 at 09:14 AM.
    \m/\m/

  34. #34
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Pirate, you need to really start reading up on methods and how they work. You are definately the one in this conversation that is mixed up. PT Exorcist is just trying to show why the compiler is perfectly right in throwing an error.

    Think about it this way, when you specify a return type in your method signature, you MUST return that type. Sometimes when you have the return statements in a conditional statement, it may never be reached. So the compiler is looking through each way possible that the code can execute. If it finds a path through your code that doesn't return the variable required, it will throw the error.

    Lets break down your original one:

    foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
    {
    return dr[1];
    }

    That is cool if there is at least one datarow in the table. What if though, there is NO datarows in the table? That return statement will NEVER be called. Since you don't have any more return statements that can be reached in the method, the compiler wouldn't know what to return and knows it is illegal code. It throws the error that you got.

  35. #35

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    [/CODE]
    Originally posted by hellswraith

    when you specify a return type in your method signature, you MUST return that type.
    lol...

    [CODE]
    public ArrayList[] a()
    {
    return null ;
    }

  36. #36
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    lol, this **** is funny as hell . I gues the Pirate will not give up without a fight.
    But seriously thought, listen to what they are saying Pirate. Returning null and not returning anything is totally different. When you initialize a variable and set it to null. You are actually being safe. Here is an example.
    Code:
    string str = null; //pointing to empty area of memory;
    string str2; //pointing to garbage in memory
    Methods work the same way.
    Code:
    public string GetName()
    {
         return "John Doe";
    }
    
    string name = GetName(); //name will be pointing to the address where name is instantiated.
    
    public string GetAnotherName()
    {
        Console.WriteLine("I dont have a name");
    }
    
    string anotherName = GetAnotherName(); //error
    Hope that helped.

  37. #37

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    DevGrp , I know that but what hellswraith said is wrong(quoted above) and that's really confusing . .

  38. #38
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    you're making some kind of confusion..if i understood u right u mean that hellswraith is wrong because returning null is returning another type right? returning null is just setting the return as a type without nothing inside, while if u just doesnt return anything (if i am not in mistake) you're returning void and that is really a big mistake

    see this in this point of view: if you have an object as return type you can return ANY KIND of type because they all inherit from object..although they dont inherit from null you can in ALL kinds of type return null and no error will be thrown(sorry if i am saying some kind of stupidity but its the way i think you'll better understand )

    if it isnt what u meant then explain because i didnt get the point?
    \m/\m/

  39. #39

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by hellswraith
    when you specify a return type in your method signature, you MUST return that type.
    Isn't this confusing ? .


    This function should return arrays of arraylist objs but they the compiler didn't find it wrong and allowed it to return null . This contradict hellswraith' saying .

    Code:
    public ArrayList[] a()
    {
    return null ;
    }
    At the end , I'm sure this thread would be useless . Just crap so , enough...lol

  40. #40
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Pirate
    Isn't this confusing ? .


    This function should return arrays of arraylist objs but they the compiler didn't find it wrong and allowed it to return null . This contradict hellswraith' saying .

    Code:
    public ArrayList[] a()
    {
    return null ;
    }
    At the end , I'm sure this thread would be useless . Just crap so , enough...lol
    NO, I am not wrong at all. You see an ArrayList array can be null, so when you are returning null, you are returning a nulled arraylist. Any object can be null, therefor, a null version of that object is returned when you do a return null statement.

    As PT said, not returning anything at all is a void, and that is impossible to assign to objects. The compiler is acting just like it is supposed to, you just are not understanding what is going on.

    Yes, you return a null, but in the end, a nulled arraylist will be returned. Here is how to prove it:

    call your function and assign the result to a string. Since a string can contain a null value, and if you truly believe that the only thing being returned is a null value, you should be able to do this. You can't. The compiler won't let you. Now, assign the result to a ArrayList array like the return type is supposed to be, and you will find it will work. This is because the method is returning a nulled ArrayList array.

    I am done explaining this, if you don't get it now, then you are just being stubborn with your ways, and none of us can argue that. Hope you understand better.

Page 1 of 2 12 LastLast

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