Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code
Beginner having trouble with simple code Beginner having trouble with simple code
Beginner having trouble with simple code
Go Back  Xtreme Visual Basic Talk > > > Beginner having trouble with simple code


Reply
 
Thread Tools Display Modes
  #1  
Old 02-14-2012, 02:31 AM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default Beginner having trouble with simple code


Heya,
I'm making my first program in VB, first post on the forums and I'm not too sure where to post this; so I just stuck it here. Can't go wrong with general :P

The following information isn't really relevant: Well, I'm making a program which solves physics equations (don't worry if you don't know physics, the problem isn't related to physics at all), by doing such a complex program it's forcing me to learn VB at a much faster rate than I would normally. I'm saying this because it's basically a warning that my code is terribly inefficient (I'm guessing).

Well, there is basically about 10 variables, each of which is assigned to a text/combo box. They are integers, but I only use three numbers: 0,1,2. 0 means the user hasn't even tried to enter anything in the text/combo box, this is left alone. 1 means the user has tried to enter something, but has failed, this causes the program to prevent the use of the information. 2 means they have tried and succeeded and the information is used.

Now, the piece of code I'm having trouble with is this:

Code:
 If HeightReady Or MassReady Or TimeReady Or ForceReady Or WorkReady Or MotionReady Or MomentumReady Or SVReady Or DisReady Or ThetaReady Or ImpulseReady Or FinalReady Or InitialReady Or AccelerationReady Or FrequencyReady Or DirectionReady = 1 Then
            MsgBox("Calculate will not be enabled unless all problems are fixed")
            btnCalculate.Enabled = False
            Calculate = False
        Else
            If Calculate = False Then
                btnCalculate.Enabled = True
                MsgBox("Calculate has been enabled")
                Calculate = True
            End If
        End If
As you can see, the first part is really, really clunky. I don't know a way around it yet, but here is the problem. By debugging the program bit by bit I've noticed that even if every single variable (i.e.ThetaReady) is 0, except one (i.e. MotionReady), then the program still displays the "Calculate will not be enabled..." message and doesn't enable the calculate button.

This is the same for every instance, not matter what is correct and what is unused, if there is nothing wrong with the data, this last bit of code still doesn't work properly.

I can upload the program, if you want, but it's a lot of fluff and terrible coding.

Thanks,
Norak.
Reply With Quote
  #2  
Old 02-14-2012, 02:44 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

The logic in your if says "If any one of these variables is True then..." So it would only make it into the Else is if all variables are false. That's not right.

But what you're after is "If any one of these variables is NOT true then..." You want to be checking with Not.
Code:
If Not Ready1 Or Not Ready2 then
   [Can't]
Else
   [Can]
End if
The alternative is to used Ands and check for True. Note that [Can] and [Can't] swap.
Code:
If Ready1 And Ready2 Then
   [Can]
Else
   [Can't]
End if
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 02-14-2012, 02:59 AM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default

Ok, if I understood you right, here's what I've done:
Changed the = to a <> after all the "xReady or yReady" and switched the commands after it around, so what was after the else is now switched with what happened automatically.

Problem I'm running into now is that it only does the "Calculate has been enabled", despite the variables being 1. I think I've misread what you've said.
Reply With Quote
  #4  
Old 02-14-2012, 03:11 AM
Flyguy's Avatar
FlyguyBeginner having trouble with simple code Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,162
Default

Instead of replacing "=" with "<>" you should have replaced "OR" with "AND" and leave the rest intact.
Reply With Quote
  #5  
Old 02-14-2012, 03:11 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Sorry, I never saw the "= 1" at the end of that statement (I hadn't scrolled).

I thought all the variables were boolean.

You have to check each variable equals the value.
Code:
If This = 1 Or That = 1 Or Whatever = 1 Then
Otherwise the compiler is converting your integers into booleans to do the comparison with (and -1 = True, 1 or 0 would = False)
__________________
There are no computers in heaven!
Reply With Quote
  #6  
Old 02-14-2012, 03:16 AM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default

I'm doing this really inefficiently, aren't I?
Well, I'll stick with it, but in future, any recommendations for code I could have used?

I just did what you said, and it works now!
So it wasn't so much carelessness when I was coding, but a lack of knowledge. Thanks a heap for the help!

[EDIT] Your replies were also very quick, I would have wasted so much time on this otherwise. I already took up 3 hours re-writing large chunks of my code thinking I made a mistake earlier. So thanks for that too!
Reply With Quote
  #7  
Old 02-14-2012, 03:26 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I don't know how you're setting HeightReady or the other variables so it's difficult to suggest alternatives. But I'd guess that you're setting them on events on those controls whereas you might be better off just calculating it when you need to. Kind of depends on what information you're trying to give the user in response to their actions.

But you do seem to have unnecessary variables. Like you have the Calculate variable which is nothing but a copy of btnCalculate.Enabled so you could pretty much get rid of the Calculate variable and just use btnCalculate.Enabled instead.
__________________
There are no computers in heaven!
Reply With Quote
  #8  
Old 02-14-2012, 03:31 AM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default

Ah, I see. It would be a bit hard without seeing the whole code.

The calculate variable is there so that if calculate (a button) is enabled then it uses the other piece of code. Unless you're suggesting that I replace it with btnCalculate.Enabled = True?

Can you do that with functions?

[EDIT] Okay, I just did it. Thanks, useless variables = bad variables.
Reply With Quote
  #9  
Old 02-14-2012, 03:41 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Where you check If Calculate = True you can say If btnCalculate.Enabled = True and achieve the same thing.

Not so much that useless variables are bad variables, but if you're having to make sure two variables are always in the same state then you're more likely to run into problems if you forget to set one of them correctly.

You should also have a look at Enums when you've got a set number of states. Instead of having an Integer variable that can be 0, 1 or 2 and have to remember what each state is you can turn that into an Enum and it makes the code more readable and it's not as easy to forget what the states are.

Apologies for any syntax errors, I don't VB6 much these days...
Code:
Enum MyStates
    NotTried ' This is given the value of 0
    TriedFailed ' This is given the value of 1 etc.
    Succeeded
End Enum

Dim HeightReady As MyStates = MyStates.NotTried

HeightReady = MyStates.TriedFailed

If HeightReady = MyStates.Succeeded Then
The code is not more "efficient" when running but it's much more efficient when reading it.
__________________
There are no computers in heaven!
Reply With Quote
  #10  
Old 02-14-2012, 03:54 AM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default

I've never heard of Enums before. I'll insert them into the code tomorrow, about to go to bed.

I've got a bunch of small questions, answer them at your whim:
How do I ask a question of the user, i.e. "Would you like to do this? Yes | No"
How do I make a new form and link from one form to another?
Does linking to a new form keep old variables?

Any you don't answer I'll just try to google, so don't feel any pressure. Thanks again.
Reply With Quote
  #11  
Old 02-14-2012, 04:02 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

For questioning the user you use MsgBox and check the response.
Code:
If Msgbox.Show("Do you want to?", vbQuestion + vbYesNo, "Really?") = vbYes Then
   [They want to]
Else
   [They don't want to]
End if
vbQuestion is the icon used in the dialog. vbYesNo is the buttons to use. There are other options (like vbOKOnly for just an OK button).

Form usage is a bit different in VB6 to .NET so I'm going to have to avoid those questions or I'll just get it wrong.
__________________
There are no computers in heaven!
Reply With Quote
  #12  
Old 02-14-2012, 06:44 AM
ElderKnightBeginner having trouble with simple code ElderKnight is offline
Senior Contributor

Forum Leader
 
Join Date: Oct 2003
Location: Central Florida
Posts: 1,275
Default

It might make things more readable if the result of that test were assigned to a single Boolean variable.

Code:
Dim All_OK as Boolean
Dim HeightReady as Boolean, MassReady as Boolean . . . [et c.]
.
.
.
All_OK =  HeightReady And MassReady And TimeReady And ForceReady And _ 
    WorkReady And MotionReady And MomentumReady And SVReady And _
    DisReady And ThetaReady And ImpulseReady And FinalReady And _
    InitialReady  And AccelerationReady And FrequencyReady And _
    DirectionReady
.
.
.
If All_OK Then
    [Go to it]
Else
    [Warning]
Endif
__________________
-- D.J.

I do not endorse any items advertised within this frame, and regret that the viewer is subjected to such.
Reply With Quote
  #13  
Old 02-14-2012, 06:55 AM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default

or you could assign each state a checksum. check the checksum for proper value and proceed from there.
Reply With Quote
  #14  
Old 02-14-2012, 08:42 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Without actually knowing how any of those variables are set one can only guess. My guess is that it's done through various events of the controls.

To me, that's a lot of work. And there's also the information given back to the user to bare in mind as well.

Like, in the code that's shown, if something is wrong then the user doesn't actually know what is wrong. Just that something is wrong. The program knows what's wrong. One of the variables says it is.

So I'd be tempted to not bother trying to handle various events to variable values but instead have a function that returns that the information entered is valid and also alert the user as to what information is not valid.

Again, my VB6 is crap so this is .Net...
Code:
Function ValidData As Boolean
    ' Check a value
    if string.isnullorempty(txtHeight.Text.Trim) Then
        ' The user must enter a height. Warn them
        messagebox.show("A height must be entered.")
        ' data is not valid
        return false
    else
        ' They've entered something, but is it correct. Let's say it's got to be a number
        if not isnnumeric(txtheight.text) then
             ' Warn them that the height has to be a number
             messagebox.show("Height must be a number.")
             ' data is not valid
             return false
        end if
    end if

    ' Check the other values...

    ' If everything has passed, return true
    return true
End Function
Then when you want go to perform the calculation...
Code:
 If ValidData Then
    If Calculate = False Then
        btnCalculate.Enabled = True
        MsgBox("Calculate has been enabled")
        Calculate = True
    End If
End if
I would also question having the calculate button disabled and then enabled on some action. Again, I have no idea how you're doing anything but it could raise the question of the user of why can't they press the calculate button.

You could just have the calculate button always enabled and run the validation check when they press it and only do the calculation if the ValidData function returns true. There's two ways of doing things. You can either disable stuff and stop people doing it, or you can leave it enabled and warn the user why they can't when they try. Personally I prefer the second option because it gives far more information to the user.
__________________
There are no computers in heaven!
Reply With Quote
  #15  
Old 02-15-2012, 03:10 AM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default

Wow, this is great, thanks for all the help guys!

Everything is running smoothly, and neater, thanks to you guys.
I've figured out all the form stuff, I think I'm spending too much time on this. It's getting too much fun and taking time away from everything else I should be doing :P

Well, if any of you understand writing to files, I've found a new dilemma. I've written some code to grab a bunch of data (which is now validated thanks to you guys <3) and write it to a bunch of files. Problem is, it only writes the first piece of data in all instances. Here is the code I used:
Code:
 If SVReady = 2 Then
                SW = IO.File.CreateText(Application.StartupPath + "\SV.dat")
                Temp = txtSV.Text
                Temp2 = cmbSV.Text
                Temp3 = cmbSVPer.Text
                Temp4 = cmbSVDirection.Text
                SW.Write(Temp, Temp2, Temp3, Temp4)
                SW.Flush()
                SW.Close()
                SW.Dispose()
            End If
<Solved>Also, you can see from my code that I currently have all my .dats going into a main folder, is there code which creates a folder, then puts the .dats in that specially made folder?</Solved>

Also (yeah, another one), how do I read files? Open and Read don't seem to work for me.

Once again, sorry for all the questions, you guys have just helped me learn so much faster than I did on my own so I want to use and abuse you more.

Last edited by Norak; 02-15-2012 at 03:32 AM.
Reply With Quote
  #16  
Old 02-15-2012, 01:33 PM
dilettante's Avatar
dilettanteBeginner having trouble with simple code dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,520
Default

This is the General forum for later versions (4, 5, and 6) of the actual Visual Basic programming language. You seem to be using VB.Net, and there are other forums here for that.
Reply With Quote
  #17  
Old 02-15-2012, 02:18 PM
passel's Avatar
passelBeginner having trouble with simple code passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Assuming you are actually using VB.net and not VB4,5 or 6, and your current questions are dealing with File I/O, I'm moving the thread to the .Net side of the forum and placing it in the File I/O area.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #18  
Old 02-15-2012, 10:54 PM
Norak Norak is offline
Newcomer
 
Join Date: Feb 2012
Posts: 16
Default

Ah, I thought that because the program I was using was VB5 Express Edition that I was using VB5. Nooby mistake to assume something like that.

I guess you were able to find out that I was using .net because of some code I posted?
Reply With Quote
  #19  
Old 02-16-2012, 05:20 AM
DrPunk's Avatar
DrPunkBeginner having trouble with simple code DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Woo, it's .Net not VB6. Hooray.

Your IO.File.CreateText gave the game away.

First things first, ditch Msgbox and use Messagebox.show instead.

Where are we at? It's probably worth you starting a new thread with your latest question.
__________________
There are no computers in heaven!
Reply With Quote
  #20  
Old 02-20-2012, 08:49 AM
passel's Avatar
passelBeginner having trouble with simple code passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Quote:
Originally Posted by Norak View Post
Ah, I thought that because the program I was using was VB5 Express Edition that I was using VB5. ...
There isn't a VB5 Express. There was a VB5 CCE (Control Creation Edition) version that was free, 10-15 years ago.

Perhaps you have VB 2005 Express.
Since the 2008 and 2010 versions are also free, you might want to update to the VB 2010 Express version to be current.

If someone posts a VB project/solution, it will most likely be a later version and will take some manual tweaking to get it open in VB 2005, and some funtionality used may not be available in the 2005 version.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code Beginner having trouble with simple code
Beginner having trouble with simple code
Beginner having trouble with simple code
 
Beginner having trouble with simple code
Beginner having trouble with simple code
 
-->