|
-
Jun 24th, 2003, 07:43 PM
#1
Thread Starter
Sleep mode
"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:
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();
}
Last edited by Pirate; Jun 27th, 2003 at 10:44 AM.
-
Jun 24th, 2003, 10:16 PM
#2
Frenzied Member
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.
-
Jun 24th, 2003, 10:18 PM
#3
Thread Starter
Sleep mode
I want to return datarow obj . you mean I can't do it this way ??
-
Jun 24th, 2003, 10:32 PM
#4
Thread Starter
Sleep mode
btw , I did something similar (return datarow array) in VB.NET , with almost same code . so ...
-
Jun 24th, 2003, 11:21 PM
#5
Frenzied Member
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.
-
Jun 24th, 2003, 11:24 PM
#6
Thread Starter
Sleep mode
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
-
Jun 26th, 2003, 03:39 PM
#7
Thread Starter
Sleep mode
I can't find else ways to return datarow object !
anyone can hlep me ?
-
Jun 26th, 2003, 06:00 PM
#8
Frenzied Member
What are you trying to return??
-
Jun 26th, 2003, 06:04 PM
#9
Thread Starter
Sleep mode
DataRow objects . In this code it's " dr[1] " .
foreach(DataRow dr in ds.Tables[MainTableCat].Rows)
{
return dr[1];
}
-
Jun 26th, 2003, 06:14 PM
#10
Frenzied Member
Why dont you just return the dataset or the datatable?
-
Jun 26th, 2003, 06:26 PM
#11
Thread Starter
Sleep mode
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 .
-
Jun 27th, 2003, 10:42 AM
#12
Thread Starter
Sleep mode
Fixed . You're right DevGrp . Returning datatable is a good idea .
Thanks for your help .
-
Jun 27th, 2003, 07:28 PM
#13
-
Jun 28th, 2003, 10:15 AM
#14
yay gay
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/
-
Jun 28th, 2003, 10:37 AM
#15
Thread Starter
Sleep mode
Even though , I have to return that at the lower line of my function (last line ) .
-
Jun 28th, 2003, 11:15 PM
#16
yay gay
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/
-
Jun 28th, 2003, 11:18 PM
#17
Thread Starter
Sleep mode
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 .
-
Jun 28th, 2003, 11:20 PM
#18
yay gay
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/
-
Jun 28th, 2003, 11:24 PM
#19
Thread Starter
Sleep mode
The point is C# compiler is just a dumb . Why this does not happen in VB ? It's fuc king sensitive .
-
Jun 28th, 2003, 11:26 PM
#20
yay gay
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/
-
Jun 28th, 2003, 11:31 PM
#21
Thread Starter
Sleep mode
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();
}
-
Jun 28th, 2003, 11:33 PM
#22
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.
-
Jun 28th, 2003, 11:37 PM
#23
yay gay
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/
-
Jun 28th, 2003, 11:42 PM
#24
Thread Starter
Sleep mode
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 ;
}
-
Jun 28th, 2003, 11:45 PM
#25
yay gay
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/
-
Jun 28th, 2003, 11:45 PM
#26
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.
-
Jun 28th, 2003, 11:50 PM
#27
Thread Starter
Sleep mode
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 !
-
Jun 28th, 2003, 11:58 PM
#28
yay gay
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/
-
Jun 29th, 2003, 12:20 AM
#29
Thread Starter
Sleep mode
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.
}
-
Jun 29th, 2003, 12:23 AM
#30
yay gay
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/
-
Jun 29th, 2003, 12:33 AM
#31
Thread Starter
Sleep mode
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 ;
}
-
Jun 29th, 2003, 07:47 AM
#32
Frenzied Member
This is fun
-
Jun 29th, 2003, 09:11 AM
#33
yay gay
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/
-
Jun 29th, 2003, 11:07 AM
#34
PowerPoster
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.
-
Jun 29th, 2003, 01:47 PM
#35
Thread Starter
Sleep mode
[/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 ;
}
-
Jun 29th, 2003, 02:10 PM
#36
Frenzied Member
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.
-
Jun 29th, 2003, 02:28 PM
#37
Thread Starter
Sleep mode
DevGrp , I know that but what hellswraith said is wrong(quoted above) and that's really confusing . .
-
Jun 29th, 2003, 02:43 PM
#38
yay gay
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/
-
Jun 29th, 2003, 02:57 PM
#39
Thread Starter
Sleep mode
-
Jun 29th, 2003, 08:49 PM
#40
PowerPoster
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.
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
|