|
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.
|
|
|
|
|
See my comment in C++ forum.
It's time for a new signature.
|
|
|
|
|
am new to linking c# to a database
am supposed to add OleDbDataAdapter to my code....is this a method and kindly show me how to do this
Regards,
Elizabeth
|
|
|
|
|
|
Hi All,
I have this string: "1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60"
Now i want to split this string like this:
1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05
1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55
2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10
2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60
I was tring to load this string into an array of char, but the performance will be affected since i have a pretty large string input. Are there any other ways to split this string.
Thanks very much.
|
|
|
|
|
You can do this with a fairly naive regex. I'm no regex expert, but I quickly knocked this one together which should satisfy your requirements here:
Regex regex = new Regex(
@"\d+x\s\#\w*[^$]+\$\d+\.\d+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.ExplicitCapture
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
"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
|
|
|
|
|
Thanks for your posting, but you regular expression is not working. and this is supposed to be: @"([0-9][x]\s#[0-9]+\s[A-Z,a-z,\s,',0-9]+\s-\s[A-Z,a-z,\s,',0-9]+\s-\s[A-Z,a-z,\s,',0-9]+\s-\s[0-9]+\s-\s[0-9]+mL\s=\s[$][0-9,.]+)", but it is verbose, and i need to refine it.
|
|
|
|
|
I worked out, here is the solution:
string input = "1x #234 Greenock Creek - Alice's - Shiraz - 2001 - 750mL = $578.05 1x #242 Greenock Creek - Cornerstone - Grenache - 2001 - 750mL = $297.55 2x #363 Maxwell - Ellen Street - Shiraz - 2002 - 750mL = $309.10 2x #369 Maxwell - Meracus 53 - Shiraz Grenache - 2002 - 750mL = $226.60";
string pattern = "([0-9][x] #)";
string[] substrings = Regex.Split(input, pattern);
for (int i = 0; i < substrings.Length; i++)
{
if (substrings[i] != "")
{
int j = i + 1;
Console.WriteLine(substrings[i] + substrings[j]);
i++;
}
}
Console.ReadLine();
|
|
|
|