• What do you need help with? Version 1
    5,001 replies, posted
What I don't get is why you created a GetState() Method. Couldn't you just do [cpp] if (telephonebutton.IsChecked/whateverthatcomeswithjava) { code = 1;} else if (xbutton.IsChecked) { code = 2;} etc. [/cpp] ? [editline]i[/editline] [highlight]Woops, my bad, forget everything I said[/highlight] I thought getState() was not Java's way of checking it. I should Sleep(28000000). Like, now :v:
huh? getState is the method for those radio buttons to check their state.
[QUOTE=ZeekyHBomb;25094272]It seems to work too good for inStoreBox :v: What happens if you check inStoreBox.getState first?[/QUOTE] [code] public int getCode() { int code = 0; if (inStoreBox.getState()) { code = 2; } else if (telephoneBox.getState()) { code = 1; } else if (outsideBox.getState()) { code = 3; } return code; } [/code] [IMG]http://imgur.com/Sfc60.png[/IMG]
[QUOTE=Klownox;25088709]Tried that out, still not having any luck.. [code] public int getCode() { int code = 0; if (telephoneBox.getState()) { code = 1; } else if (inStoreBox.getState()) { code = 2; } else if (outsideBox.getState()) { code = 3; } return code; } [/code][/QUOTE] Nullsquared is that you?
I'm not nullsquared :frown:
[QUOTE=turb_;25097507]Nullsquared is that you?[/QUOTE] In his defense, Java Swing's radio buttons are horribly broken. There's no easy way to make a group of radio buttons and just get info about the currently selected one. So you basically have to make an ugly if-else tree unless you use complicated enumerators and compare ButtonModels. There's a big long article about it here: [url]http://www.javaworld.com/javaworld/javatips/jw-javatip142.html[/url] If you don't mind using borrowed code, you can use the code in the article to make a button group that actually makes sense (uses a vector, etc.). But if it's a school project, you basically have to grin and bear it.
[QUOTE=Klownox;25093415]So, theoretically, this should work? [code] public int getCode() { int code = 0; if (telephoneBox.getState()) { code = 1; } else if (inStoreBox.getState()) { code = 2; } else if (outsideBox.getState()) { code = 3; } return code; } [/code][/QUOTE] I'd recommend using an [url=http://download.oracle.com/javase/tutorial/java/javaOO/enum.html]enum[/url] instead of [url=http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants]magic constants[/url] for the codes.
[QUOTE=Wyzard;25097923]I'd recommend using an [url=http://download.oracle.com/javase/tutorial/java/javaOO/enum.html]enum[/url] instead of [url=http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants]magic constants[/url] for the codes.[/QUOTE] Okay, how exactly would I do that for this example(if that's not too much to ask)? I'm not entirely sure.
[QUOTE=Klownox;25098414]Okay, how exactly would I do that for this example(if that's not too much to ask)? I'm not entirely sure.[/QUOTE] I'm guessing he means something like this: [code]public enum code_t { TELEPHONE_BOX, INSTORE_BOX, OUTSIDE_BOX } public code_t getCode() { code_t code; if (telephoneBox.getState()) { code = TELEPHONE_BOX; } else if (inStoreBox.getState()) { code = INSTORE_BOX; } else if (outsideBox.getState()) { code = OUTSIDE_BOX; } return code; }[/code]
Ah, okay. I'll try it out in a bit. Thanks.
Shill, Java enums don't work quite like C/C++ enums; the values are scoped to the enum itself, so you have to refer to each enum value with the enum name and a dot, just like referring to static variables in a class (since that's what they actually are). A name like "CommisionType" would be much more descriptive, and consistent with Java naming conventions, than "code_t"; that's a sort of name I might expect to find in a C program. I'd also rename the method accordingly. In addition to the above changes, I'd make a slight structural change to the if/else structure: [code] public CommissionType getCommissionType() { if (telephoneBox.getState()) { return CommissionType.TELEPHONE_BOX; } else if (inStoreBox.getState()) { return CommissionType.IN_STORE_BOX; } else if (outsideBox.getState()) { return CommissionType.OUTSIDE_BOX; } else return null; } [/code] This works the same, but makes it more obvious to the reader that null is returned when none of the types match.
:love:
oh dear god, don't do multiple returns (actually it probably doesn't matter in a managed environment, but you guessed right, I'm a C/C++ programmer, so it irks me)
[QUOTE=shill le 2nd;25100546]oh dear god, don't do multiple returns[/QUOTE] Multiple returns can be confusing in a method with lots of logic where it may not be obvious that it bails out early in some cases. But for code like this, where the logic is trivial and the returns follow an obvious pattern, I see nothing wrong with it. I'd write it the same way in C/C++ too. However if multiple returns are verboten, I'd write it like this: [code] public CommissionType getCommissionType() { final CommissionType commissionType; if (telephoneBox.getState()) { commissionType = CommissionType.TELEPHONE_BOX; } else if (inStoreBox.getState()) { commissionType = CommissionType.IN_STORE_BOX; } else if (outsideBox.getState()) { commissionType = CommissionType.OUTSIDE_BOX; } else commissionType = null; return commissionType; } [/code] Explicitly setting commissionType to null in the last case provides the readability benefit, and use of the [url=http://en.wikipedia.org/wiki/Final_(Java)#Blank_final]blank final[/url] makes the compiler enforce that commissionType is set exactly once in each branch (since that's the intention, and any reassignments or omitted assignments would be a bug).
My hat's off to you, sir, you are a top-notch Java programmer.
[QUOTE=VeryNiceGuy;25057672]How can I access the VB.NET Web Browser control from one form to another, when it's being used in tabs? I'm trying to use it across another form to browse to the contents of a .txt file but I can't wrap my left nut around it..:v: I tried a simple thing that came to my head: [code] CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ListBox1.SelectedItem)[/code] And remembered that it was in another form, so I tried [code] Form1.CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ListBox1.SelectedItem)[/code] And gave me the errors: [code]'Form1' is not declared. It may be inaccessible due to its protection level. 'TabControl1' is not declared. It may be inaccessible due to its protection level. 'WebBrowser' is a type and cannot be used as an expression. [/code] And then I asked for help! (I recently went ~22 hours without sleep so I'm "recovering".)[/QUOTE] Nothing? :(
[QUOTE=shill le 2nd;25100982]My hat's off to you, sir, you are a top-notch Java programmer.[/QUOTE] Thank you. :smile: Now, having changed projects recently at work, if only I were a top-notch Pascal programmer...
I want to start developing in C++, but people I have spoke to about it have said C++ is horrid and that I should go with C, what I'm worried about is that C is not object oriented, although you can do pretty much the same in C as you can do with C++, it commonly ends with a hacky solution. I have read the pdf [url=http://www.fefe.de/c++/]here[/url] ([url=http://tvcdev.net/c++-talk.pdf]mirror[/url]) and it points out a lot of pitfalls in C++. Currently I am at a loss on what to choose, I enjoy object oriented programming and it is something I use a lot, but I don't want to hit snags later on.
[QUOTE=TVC;25103044]I want to start developing in C++, but people I have spoke to about it have said C++ is horrid and that I should go with C[/QUOTE] These are the coolest people ever. [editline]04:38AM[/editline] [QUOTE=TVC;25103044]it commonly ends with a hacky solution.[/QUOTE] No :\ You just can't set out to write C++ code in C. If you enjoy OOP, go for C++. Both C and C++ have their problems. Personally, I loathe OOP, so I stick to C whenever possible.
[QUOTE=ROBO_DONUT;25103286]These are the coolest people ever. [editline]04:38AM[/editline] No :\ You just can't set out to write C++ code in C. If you enjoy OOP, go for C++. Both C and C++ have their problems. Personally, I loathe OOP, so I stick to C whenever possible.[/QUOTE] OOP is a poor man's closures! :v:
[QUOTE=TVC;25103044]I want to start developing in C++, but people I have spoke to about it have said C++ is horrid and that I should go with C, what I'm worried about is that C is not object oriented, although you can do pretty much the same in C as you can do with C++, it commonly ends with a hacky solution. I have read the pdf [url=http://www.fefe.de/c++/]here[/url] ([url=http://tvcdev.net/c++-talk.pdf]mirror[/url]) and it points out a lot of pitfalls in C++. Currently I am at a loss on what to choose, I enjoy object oriented programming and it is something I use a lot, but I don't want to hit snags later on.[/QUOTE] If you want to do OOP, then use C++. If you don't, and you want to do even more low-level (and probably more difficult) stuff then use C. It's not hard to work that out
Compile C code with a C++ compiler and piss off everybody.
[QUOTE=Chandler;25104167]OOP is a poor man's closures! :v:[/QUOTE] Is it bad that I had to look up what a closure was? [QUOTE=Chris220;25104355]more low-level (and probably more difficult) stuff then use C.[/QUOTE] Low-level? Definitely. Tedious? Maybe. Difficult? No. I find it much easier to write than "correct" C++.
[QUOTE=ROBO_DONUT;25107430]Is it bad that I had to look up what a closure was? Low-level? Definitely. Tedious? Maybe. Difficult? No. I find it much easier to write than "correct" C++.[/QUOTE] Nah, closures might not be a familiar concept to everyone. Also, C++ has some stricter rules, for example with type casting, so that could be seen as easier or harder. And seeing as technically anything that's not syntactically wrong or UB is correct in both C and C++...
[QUOTE=ROBO_DONUT;25107430]Is it bad that I had to look up what a closure was? Low-level? Definitely. Tedious? Maybe. Difficult? No. I find it much easier to write than "correct" C++.[/QUOTE] I said "If you don't, and you want to do even more low-level (and probably more difficult) stuff then use C." Implying that lower-level programs are harder to write than higher level ones. Correct me if I'm wrong, but I'm fairly sure that's the case a lot of the time
[QUOTE=Chris220;25109379]I said "If you don't, and you want to do even more low-level (and probably more difficult) stuff then use C." Implying that lower-level programs are harder to write than higher level ones. Correct me if I'm wrong, but I'm fairly sure that's the case a lot of the time[/QUOTE] I find the opposite to be true.
I'm pretty sure TVC doesn't. But who knows, maybe he does.
You're making assumptions you shouldn't be making. Not that I'm trying to steer anyone away from C++ here, because I'm not. I'm not implying that C should be easier for the majority of people, either.
Ok you're right.
-snip-
Sorry, you need to Log In to post a reply to this thread.