|
|
I understand that you populate items in flowlayout panel
but my dear friend just tell me which control are you using
to populate the items...
So that it is easier to give you solution
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
here is my code:
using System;
class one
{
public int a;
int f;
public bool e(int i)
{
for (f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
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 when i debug this program it returns an error "not all code paths return a value" pointing at e.
and gives a warning at f++ in the for statement "unreachable".....what is the fault??
|
|
|
|
|
By looking at your code, it seems that you have written a program to checked whether entered number is prime or not.
I am sorry my friend but logic of this program is incorrect.
wjbjnr wrote: not all code paths return a value
This error is coming because its not returning anything if program control doesn't entered inside "for" loop.
You have to make your logic correct.
P.S: Please provide code snippets in code block, it enhances readability of code.
|
|
|
|
|
I'll reformat your code so that we can read it:
using System;
class one
{
public int a;
int f;
public bool e(int i)
{
for (f = 2; f <= i; f++)
{
if (i % f == 0)
{
return false;
}
else
{
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");
}
} First of all, why f++ is unreachable. In your code, the for loop will always exit after the first run through - there is no way for it not to. Remove the return true to outside the loop and you'll solve both parts of your problem - the loop condition will be reachable and the code will return a value from all parts.
[Edit]I just want to point out that you need to code for an edge case here. What happens if I put 0 in as a value? The code will never enter the loop and will incorrectly report out that it's a prime number. You should add a check in here to ensure that it's not less than 2 and return the value accordingly.
"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
modified on Monday, May 10, 2010 7:27 AM
|
|
|
|
|
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 &lt;= 1) return false;"
in this code this statement works...but not in the first one...why is that!!
|
|
|
|
|
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
|
|
|
|