|
|
how want to preview my web application
|
|
|
|
|
First edit your post to properly format your code snippet.
Second, edit you post to actually ask a question.
You will find the responses you receive are related to the effort you put into the post and how well you comply with the guidelines here.
I know the language. I've read a book. - _Madmatt
"The OP herself was not sure about her question"
"The OP is from India and I know what she meant." - Shameel
|
|
|
|
|
To start, try giving the subject heading of your post something meaningful. Just the fact that your posting a question would suggest you are looking for help.
Here is a list:
You have unused using statements, they should be removed.
Don't use underscore _ in namespaces. Try something like Daniel.NewFormApp
Don't start class names with underscores, start with a capital letter.
Code isn't formatted, when posting at CP enter code inside "code block" tags
Don't leave empty methods in your code, previewButt_Click
Give variables meaningful names like NameTextBox, not TextBox1
Pascal/camel case variables strSgl not strsql
Don't use "" as you are creating strings in memory for no reason, use string.Empty
clearButt and submitButt are probably not a good choice of naming, try something like ClearButton
And actually asking a question will help, we're not all engineers.
Other than that, it looks good
"You get that on the big jobs."
|
|
|
|
|
RobCroll wrote:
And actually asking a question will help, we're not all engineers mind readers.
FTFY
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
|
|
|
|
|
This method return "StackOverflowException" error for some inputs;like 12.
where is Infinite loop point?
public void cycle(int f){
int i, j;
if (f == 0)
return;
else
{
for (i = f; i >= 0; i--)
{
for (j = 0; j <= 10; j++)
{
if (i == Math.Pow(2, j))
{
f = f - (int)Math.Pow(2, j);
switch (j)
{
case 1:
cbx1.Checked = true;
break;
case 2:
cbx2.Checked = true;
break;
case 3:
cbx3.Checked = true;
break;
case 4:
cbx4.Checked = true;
break;
case 5:
cbx5.Checked = true;
break;
case 6:
cbx6.Checked = true;
break;
case 7:
cbx7.Checked = true;
break;
case 8:
cbx8.Checked = true;
break;
case 9:
cbx9.Checked = true;
break;
case 10:
cbx10.Checked = true;
break;
}
break;
}
}
}
cycle(f);
}
}
|
|
|
|
|
Hi,
1.
at the very bottom of your cycle() method is a call to method cycle(), so when all the i,j combinations make your test
if (i == Math.Pow(2, j))...
fail, your method calls itself with the same input value, clearly ending up in an "infinite loop". This will happen for the majority of f-values, as most numbers simply aren't a power of 2.
2.
I don't know why you would want to call cycle again; it is even unclear to me why you would need two for loops. And if your supported input range is limited (as the presence of a switch construct suggests), you also !! don't need a switch !!
I think the code below would come close to what you want (it also clears some checkboxes, something your code didn't do at all):
void myBinaryCheckboxes1(int f) {
cbx1.Checked= (f & 0x0001)!=0;
cbx2.Checked= (f & 0x0002)!=0;
cbx3.Checked= (f & 0x0004)!=0;
cbx4.Checked= (f & 0x0008)!=0;
cbx5.Checked= (f & 0x0010)!=0;
cbx6.Checked= (f & 0x0020)!=0;
cbx7.Checked= (f & 0x0040)!=0;
cbx8.Checked= (f & 0x0080)!=0;
cbx9.Checked= (f & 0x0100)!=0;
cbx10.Checked=(f & 0x0200)!=0;
}
BTW: in binary (and in many other situations), things would get numbered starting by zero (so cbx0 up to cbx9).
3.
For the really advanced implementation, you could further simplify if you would have a collection (array, list, whatever) holding those checkboxes; it would suffice to put them in their own GroupBox (nothing else in that GroupBox is easiest). And then have the checkbox tags hold their bit value (the hex numbers in the above code). The final code could then be as simple compact as:
void myBinaryCheckboxes2(int f) {
foreach(Control c in myGroupBox.Controls) {
((CheckBox)c).Checked=(f & (int.Parse((string)f.Tag)!=0;
}
}
BTW: If that looks too weird/convoluted, just stick with the code in myBinaryCheckboxes1() !
[ADDED] Note 1, triggered by lukeer's reply: depending on how the Tag gets assigned, one might not need the int.Parse, one could just assign a numeric tag right away, then use ((CheckBox)c).Checked=(f & (int)f.Tag)!=0; [/ADDED]
Luc Pattyn [My Articles] Nil Volentibus Arduum
modified on Tuesday, August 23, 2011 8:11 AM
|
|
|
|
|
1.you said "your method calls itself with the same input value" but if if statement become true f-values will change;line before switch.
2.I explain this with my sample:12
with input 12 first loop check from 12 till down until 8.At 8 when j become 3 at second loop (second loop prepare all possibilities for 2 power j) at this point if statement will true and run
f = f - (int)Math.Pow(2, j) that turn f to 4 then run
cbx3.Checked = true; in the later round of first loop when i=4 and j=2 , if statement will be true again and will run
f = f - (int)Math.Pow(2, j); that turn f to 0 then run
cbx2.Checked = true;
3.The myBinaryCheckboxes1 is unclear for me specially (f & 0x0001). What means that?
|
|
|
|
|
1. the logic in your code is flawed, it isn't just too complex as I wrote earlier, it also is wrong. The easiest way to make this obvious is this: f is used once, to initialize the for-loop on "i"; modifying f later on has no net result, as f isn't used any more. This cannot possibly be correct.
I added some log statements, writing intermediate values to the console, and it clearly shows f going negative and all output being wrong. Here is what I ran:
using System;
class test {
static void Main() {
cycle(8);
cycle(12);
Console.WriteLine("done");
}
public void cycle(int f){
Console.WriteLine("testing with f="+f);
int i, j;
if (f == 0) return;
for (i = f; i >= 0; i--) {
Console.WriteLine("i="+i);
for (j = 0; j <= 10; j++) {
if (i == Math.Pow(2, j)) {
f = f - (int)Math.Pow(2, j);
Console.WriteLine("found bit "+j+"; f now equals "+f);
break;
}
}
}
}
I suggest you either learn to work with a debugger (such as Visual Studio) using single-stepping, breakpoints, watch variables, and the like. Or add log statements as I did in the above code.
2. 0x0001 is a hexadecimal literal value, and & is the bitwise AND operator. Both should be explained in all their detail in any introductory book on C#. I strongly recommend you get such a book, if you don't yet have one, and start studying the language.
|
|
|
|
|
Nice.
I would just change to use an int as tag right away. So there would be no need to parse.
Ciao,
luker
|
|
|
|
|
Oh yes! what was I thinking?
[ADDED] wait a moment, how do I get an int there (except with more code)? when I type something into Visual Designer, isn't the tag always going to be a string? [/ADDED]
Luc Pattyn [My Articles] Nil Volentibus Arduum
modified on Tuesday, August 23, 2011 8:08 AM
|
|
|
|
|
No, an object -- so you need to cast. But can't you simply derive your own control and add exactly the field/property you need?
|
|
|
|
|
1.
I agree a Control's Tag property officially is of type object , however entering abcde (or 123) in Visual Designer's property pane for a Button resulted in a statement this.btnOK.Tag = "abcde"; (or "123") in the Xyz.Designer.cs file, so I woulds say VD creates strings, and one can cast the Tag to string, Parse it, or Convert it.
Simply casting to an int as in int aha=(int)btnOK.Tag; throws an InvalidCastException in both cases.
2.
Of course I could createa derived CheckBox, however I don't do that just to eliminate a single cast/Parse/Convert operation, there is too big an unbalance between amount of code added and elegance gained.
|
|
|
|
|
Luc Pattyn wrote: entering abcde (or 123) in Visual Designer's property pane
Obviously, I never do that. I do it the right way.
Luc Pattyn wrote: unbalance between amount of code added and elegance gained.
That depends on how many you need to create and how often they are accessed.
|
|
|
|
|
PIEBALDconsult wrote: I do it the right way.
So you are not using one of the Visual Designer Extensions for Notepad then?
|
|
|
|
|
Notepad is fine for XML, but unsuitable for code.
|
|
|
|
|
Personally i avoid the use of the tag property at all costs.
Many people seem to disagree with me on that one but unless you have complete control over your controls (and a bloody good memory)there is always the risk that some wee-beastie is going to swap out your expected data with their own.
Not to mention its an object type and my general rule of thumb is 'if you find yourself declaring something as object, take a step back and check if you designed it right'
A better solution can usually be found through subclassing or extension methods at the very least (although, ofc, never quite as quick to write :P)
|
|
|
|
|
I mostly agree with your point in general, however there is nothing I would do (or not do) at all costs, every decision is based on a balance of costs vs benefits (both current and foreseeable).
I don't mind subclassing when I need added or altered behavior. For a single parameter, I do use the Tag, as that has been its purpose all along (and I often regret other built-in classes don't offer a similar feature). Furthermore, the degree of sophistication in my answers depends on the style of the original question.
Now if all people were like you, me using the Tag property would be quite safe.
|
|
|
|
|
cycle(f); <-- Here it is!
|
|
|
|
|
But f will change during if block and when become 0 ,cycle(0)run return;
|
|
|
|
|
Hi there
I am currently looking for help in writing a C# application that would 'trick' the OS into thinking there is a USB device connected. I would like to know if this is even possible and whether it is even possible in C#.
Well, I'll explain in greater detail what I am trying to do. The company I work for has bought software that interfaces with USB devices (Smart card readers), we develop smart cards, and we also have several applications that simulate smart cards to test with. It would help us a lot if it was possible to let the software we bought interface with our simulators instead of the smart card readers. This is unfortunately not possible, their software can't be configured to communicate with anything other than smart card readers.
I am hoping it is possible to create an application that is a virtual smart card reader, and this application would relay all the data their software sends to the virtual card reader to our simulator. (is this possible or am I living in a dream world?? which I tend to do a lot ). If this is possible, can it be done in C# or will I have to use WIN API calls?
I have found many C# projects that have classes to communicate with USB devices, but I have not found any that emulates a USB device. Any help will be greatly appreciated or if you can point me to reading material regarding the matter, that will also help me a lot.
Regards
Riaan
KOM UIT DAAAAA!!!
|
|
|
|
|
I am not sure but this[^] might be useful. (I think it's the reverse way...)
|
|
|
|
|
Hi, thanks for the reply. That project looks very interesting, however it seems like they use a cellphone or some other mobile device that runs the emulation software, it seems like it is only a replacement for an actual smart card reader, so this is not exactly what I am looking for.
thanks for the reply though.
KOM UIT DAAAAA!!!
|
|
|
|
|
There exist some virtualization softwares which let you access USB devices connected to a different computer as if they were connected to your local computer, e.g. http://www.fabulatech.com/usb-over-network.html[^]. Hence I think that your emulator should be possible, as the virtualization software somehow pretends the presence of a local USB device. But I do not know how to write it.
|
|
|
|
|
Thanks for the Reply.
I actually stumbled onto the same program, so I am also convinced it must be possible, but how remains the big question
KOM UIT DAAAAA!!!
|
|
|
|