|
well in the first case there is still a path through the method that doesnt assign a return value you know its like what if i is more than 2 let me just break off here to say how difficult it is to read unformatted code whatever thats just me in the second case all paths through the method assign a return value hope thats cleared things up for u
[EDIT fixed typo]
|
|
|
|
|
While the other both answers are correct, I only want to mention the reasoning behind it.
The code considers all possible exits in a program and if it doesn't achieve the desired result in it, it raises an error.
In your for loop, you have trapped all possible outcomes. So, whatever the number f be, there will be an answer. But what happens when the code doesn't enter the loop? what if i = 1?
Hence, there should be one more return statement after the loop ends.
|
|
|
|
|
take a look at this code..
using System;
class one
{
public int a;
int f;
public bool e(int i)
{
if (i <= 2) return false;
}
}
class control
{
public static void Main()
{
one obj = new one();
Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)");
int o = obj.a = int.Parse(Console.ReadLine());
obj.e(o);
if (obj.e(o)) Console.WriteLine("THE VALUE IS PRIME");
else
Console.WriteLine("THE VALUE IS NOT PRIME");
}
just look at the upper portion....it still doesnt return a value and gives the same error
now look at this code
using System;
class ChkNum {
// Return true if x is prime.
public bool IsPrime(int x) {
if(x <= 1) return false;
for(int i=2; i <= x/i; i++)
if((x %i) == 0) return false;
return true;
}
}
class ParmDemo {
static void Main() {
ChkNum ob = new ChkNum();
for(int i=2; i < 10; i++)
if(ob.IsPrime(i)) Console.WriteLine(i + " is prime.");
else Console.WriteLine(i + " is not prime.");
}
}
now look at this code(the upper section)..in this also it uses "if(x <=1) return false;"
in this code this statement works...but not in the first one...why is that!!
|
|
|
|
|
IMPORTANT:
Use 'pre' tags for your code. without those tags, the code is totally unreadable. Also, do preview your post. There are many errors in your post.
Now, the second code works because it considers all possible values of x. if it is less than 1, it returns false. otherwise, for all other values, it can be either true or false.
try changing the code to x<=0
|
|
|
|
|
that maybe...but i just wrote this code in the upper section
public bool e(int i)
<pre> {
<pre> if (i <= 2)
<pre> return false;
<pre> }
even then it gives the same error!!!....wats wrong in this code??
|
|
|
|
|
because the method returns a value only when i<=2. what if i>2? think how the code will proceed? where does the code get return statement if i>2?
|
|
|
|
|
alrite.....see this code
using System;
class one
{
public int a;
// Return true if x is prime.
public bool e(int x)
{
if (x <= 2 && x>=0) return true;
for (int i = 2; i<;x; i++)
if (x % i == 0) return false;
return true;
}
}
class control
{
public static void Main()
{
one obj = new one();
Console.WriteLine("ENTER THE NUMBER TO BE CHECKED(SHOULD BE GREATER THAN 2)");
int o = obj.a = int.Parse(Console.ReadLine());
obj.e(o);
if (obj.e(o))
Console.WriteLine("THE VALUE IS PRIME");
else
Console.WriteLine("THE VALUE IS NOT PRIME");
}
}
now see this code.....
using System;
class one
{
public int a;
public bool e(int i)
{
if (i <= 2 && i >= 0) return true;
for (int f = 2; f<i; f++)
<p=""> {
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}
}
wats the difference between the two???
the upper one works the lower one gives the same error!!!
|
|
|
|
|
What are you trying to do here buddy? Are you trying to prove that your inefficient coding techniques are right and C# language is incorrect?
Every programming language has a syntax. You need to follow them in order to use it.
You need to ask these questions yourself and tell us why one code works and why the other doesn't. When you can find the reason yourself, you will be able to write the correct code from next time onwards.
All the best.
|
|
|
|
|
i'm nt condeming any body.....
i just dont understand the difference...the syntax and logic in both codes are same..but one gives the error and the other doesnt....never before i have encountered such a problem....there is another way to do this program...but i wanted to use return types....and now it gives this error ....i am greatful to all your replies!!!
|
|
|
|
|
public bool e(int x)
{
if (x <= 2 && x >= 0) return true;
for (int i = 2; i >= x; i++)
if (x % i == 0) return false;
return true;
}
This is your first piece of code which works. I have only reformatted it to see the code flow. Here, for every value of x, there is a return path available. Hence this one is a legal code.
public bool e(int i)
{
if (i <= 2 && i >= 0) return true;
for (int f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}
As correctly pointed out by the compiler, not all paths return the value. Hence the code syntax is incorrect.
I suggest you think a bit more logically to see the difference in two cases. I also suggest to take a break from coding. it happens sometimes when you have done coding for a very long time. trust me.
Som
|
|
|
|
|
They are not the same.
They just look it because you don't see what is happening.
Indenting your code shows why they are different:
public bool e(int x)
{
if (x <= 2 && x>=0)
return true;
for (int i = 2; i<;x; i++)
if (x % i == 0)
return false;
return true;
}
and
public bool e(int i)
{
if (i <= 2 && i >= 0)
return true;
for (int f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
return true;
}
}
}
In the first case, there is always a return statement - in every exit path.
In the second case, the compiler assumes there is an exit path at the end of the loop that does not have a return statement. It doesn't need one, because your loop is crap - it will always return on the first iteration. But the compiler doesn't know that. It does not look at "i % 1" and say: that will always be 0 or 1, so there is a control path that ends in a return for all cases. It just looks and sees a control path after the loop that will never include a return.
Surprisingly, the compiler is not as intelligent as you.
I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in.
Apathy Error: Don't bother striking any key.
|
|
|
|
|
ohhh rite!!!!!!!
My God i'm literally blind at the moment.....!!!!
such a small error!!!!!
and the else was there in the for bracket!!!!...(no wonder why it was saying unreachable);
so it only see's the exits!!!!!!!!!!!!!!!!hmmmmmmmm..!!!!
now i finally understnd!!!
@som shekhar
ya i tuk a 1 hour rest..............it paid off!!!!!
thnx all for your support!!!!!!!
|
|
|
|
|
hi all
i have a ListBox and i want it to receive a Pair object and show me the Second value of the Pair in ListBox
but it shows me the ToString() method of Pair "System.Web.UI.Pair" witch means the property name is incorrect
what should i do?
my code is :
<br />
ArrayList list = new ArrayList();<br />
list.Add(new Pair("A", "B"));<br />
lstBox.DataSource = list;<br />
lstBox.DisplayMember = "Second";<br />
|
|
|
|
|
Unfortunately Pair contains only public fields named First and Seconds, but DisplayMember specify a name of the PROPERTY to display.
So you could make it work like this
{
private void Form1_Load(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
MyPair f = new MyPair("A", "B");
list.Add(f);
listBox1.DataSource = list;
listBox1.DisplayMember = "second";
}
}
class MyPair
{
public object First { get; set; }
public object Second { get; set; }
public MyPair(object a, object b)
{
this.First = a;
this.Second = b;
}
}
|
|
|
|
|
thank you for your attention
i really love this site with its responsible people
|
|
|
|
|
i have a small customer exe(a small application) that need to install prior to the main exe.i want to install both in the same setup , but that small application should be installed first. can anyone guide me for this ...
|
|
|
|
|
One way to do this would be to use a bootstrapper to install it. You can find more detail here[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
You could use either ClickOnce or a windows installer strategy.
|
|
|
|
|
thanx for your kind reply
|
|
|
|
|
I'm about to do some SNMP stuff and I have to do it in C#, so I have been looking for a good SNMP library.
I have found a couple, llike these:
http://www.snmpsharpnet.com/[^]
SNMP library[^]
Has anyone tried one of these?
Are they worth using?
If you know of somethig better please let me know
TIA,
- Anders
|
|
|
|
|
Hey Guys, I have some PDF Files stored in my database (SQL server 2005)
and now I want to retrieve them (this part is done) from database and view them in my winform application then be able to print them ... whats the best way to do this ?
regards,
K
|
|
|
|
|
Your original message is here[^]. 2 things to note:
1. Please don't delete messages.
2. Give people a chance to answer them first. Don't rush with your reposts; if you haven't had an answer after a couple of days then repost it by all means.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi
I want to disable the first item in the CheckedListBox control of C SHARP I checked for .selected property but i don find it in CheckedListBox property in Winform . checkedListBox2.Items[i].Equal, checkedListBox2.Items[i].GetHashcode, checkedListBox2.Items[i].GetType, checkedListBox2.Items[i].ToString() is only available.. i don find the checkedListBox2.Items[i].Enabled property... Pls help me
Regards,
Hema Krish.
|
|
|
|
|
checkedListBox1.SetItemCheckState(1, CheckState.Indeterminate);
|
|
|
|
|
Hi all.
I want listen to a telephone line via a modem programmatically and save transferred data over it to a file.
How can I do this?
Could you, please help me.
Thanks a'lot.
|
|
|
|