Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Calorie Counter Project Help Thread


  • Please log in to reply
21 replies to this topic

#1 centurion

centurion

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 24 February 2011 - 06:19 AM

http://forum.codecal...html#post294096

I'm not going to re-type it all, visit that thread if you want a more in depth description of what I'm trying to do. I'm brand new to visual basic and whilst I'd like to learn it along the way my main aim is just to complete my calorie tracking program.

I've created this thread as a place to post my problems as and when I come across them (there will be many)
  • 0

#2 Alexander

Alexander

    YOL9!

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh!
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Delphi/Object Pascal, Visual Basic .NET, Assembly, Fortran, Bash, Others

Posted 24 February 2011 - 06:13 PM

Feel free to pound away. :)

There aren't too many Visual Basic programmers here, but I am sure we can cover all the basics nicely.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 centurion

centurion

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 25 February 2011 - 04:50 AM

Okay, first problem is a simple one i think by I'm not sure why its going wrong.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim input_number As Integer
        input_number = Convert.ToInt32(TextBox1.Text)

        Dim total_number As Integer

        total_number = total_number + input_number

        TextBox2.Text = Convert.ToString(total_number)
    End Sub

I have an input text bos (1), a button and an output text box (2). I'm trying to make it such that when you type a number into text box 1 and click the button, it adds that number to the number in text box 2. Currently what happens is that whatever is typed into text box 1 is just copied into text box 2 when the button is pressed.

Help would be much appreciated, thanks.
  • 0

#4 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 25 February 2011 - 11:17 AM

Thats because your adding total_numer with input_number. And since total_number is never assigned its basically saying 0 + input_number.
This code should work, and i think you'll understand it.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click          
Dim input_number As Integer         
input_number = Convert.ToInt32(TextBox1.Text)    
     
[COLOR=red]Dim input_number2 As Integer
input_number2 = Convert.ToInt32(TextBox2.Text)[/COLOR]         

Dim total_number As Integer         
total_number = [COLOR=red]input_number2[/COLOR] + input_number          
TextBox2.Text = Convert.ToString(total_number)     
End Sub

Good luck with your project ~ Committed. :)
  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#5 centurion

centurion

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 25 February 2011 - 12:23 PM

I did think about that before but i tried to resolve by adding a total_number = 0 somewhere at the start of the program. I think it didn't run or something, can't remember.

Anyway, that works, thanks.

edit: I did have to type 0 into box2 initially though; is no text not considered a 0? I don't think this will be an issue but out of interest is there a way to remove that need?
  • 0

#6 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 25 February 2011 - 01:27 PM

edit: I did have to type 0 into box2 initially though; is no text not considered a 0? I don't think this will be an issue but out of interest is there a way to remove that need?

No text is not considered 0. You could just use a if loops, something to the effect of:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
Dim input_number As Integer  
[COLOR=black]Dim input_number2 As Integer[/COLOR]
[COLOR=black]Dim total_number As Integer   [/COLOR] 
[COLOR=black]input_number = Convert.ToInt32(TextBox1.Text)[/COLOR] 

[COLOR=red]If TextBox2.Text != "" Then       
[/COLOR][COLOR=red]input_number2 = Convert.ToInt32(TextBox2.Text)   
Else
[/COLOR][COLOR=red]input_number2 = 0   
EndIf [/COLOR]

[COLOR=black]total_number = [/COLOR][COLOR=black]input_number2 + input_number           
TextBox2.Text = Convert.ToString(total_number)  [/COLOR] 
  End Sub

I think that should work, but if not, its because the "if loop" syntax is wrong, I really dont code in VB, I just know enough C# to help you lol.

Good luck ~ Committed. :)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#7 centurion

centurion

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 25 February 2011 - 01:59 PM

'if loop' syntax was fine, not equal to is: <> :)
It works though and gave me a bit of practice with boolean stuff, cheers.
  • 0

#8 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 25 February 2011 - 02:30 PM

why not something like this?
Sub Main()
        Console.Write("Enter a number: ")
        Dim i As Integer
        If (Not Integer.TryParse(Console.ReadLine(), i)) Then
            i = 0
        End If

        Console.WriteLine(i)

        Console.WriteLine("Press <Enter> to terminate...")
        Console.ReadLine()
    End Sub

yes.. yes this cat knows VB in fact.... :c-cool:
  • 0

#9 centurion

centurion

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 25 February 2011 - 03:23 PM

I've just run that, works fine but I prefer working in the form mode. Dunno if you read my other post about the project? But the form mode is perfect for what I want. I'm gonna try and do some more work on it now having it store a running number count somewhere so that i can close and re-open the program and the total count remains. I'll definitely need some more help soon :)

update: I've now got calories in and calories burnt with a submit button and it will add those to the total appropriately. I am quite chuffed with myself. I need to know now how to store values in some sort of database so that I can have a stored total calorie number and then eventually store separate data for days and each individual input.

edit: Is it possible to put code already within a sub routine into another one so that it can be minimized? Not really necessary I guess but would help with organisation.

Edited by centurion, 25 February 2011 - 04:13 PM.

  • 0

#10 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 25 February 2011 - 03:48 PM

Didnt know you knew VB Sam. :P

Anyways centrurion, you could still use Sam's code with alil editing:
If (Not Integer.TryParse(TextBox2.Text, input_number2)) Then             input_number2 = 0         End If
I think that will work. ~ Committed.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#11 centurion

centurion

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 25 February 2011 - 04:17 PM

What does TryParse do?

The current method works fine although takes up space when there are several boxes that each need it applying to.

See my previous post for most recent questions, only just edited it.
  • 0

#12 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 25 February 2011 - 04:20 PM

TryParse is a beaut!

makes code really easy to read, and normally faster, heres why
if the string is not a number, or is even null, it will return false, if the string is parsed properly, it will return true

this way, you don't need to use a try/catch block. Try/Catch blocks have a lot of overhead.

I'll read back to your older post.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download