My First VB.NET Code

VB_NET_logo This has been a heck of a week. I went to the corporate office and had my computer rebuilt. It is horrible being without a computer for a couple days. On top of that I have a heavy class load for school, so I got behind. It is hard to learn to code and only have the book to look at. You need Visual Studio to actually learn from playing with code!

Well this week had an interesting question, well for a newbie to Visual Basic. I had to make a console app that would count from 1 to 100 and add the count together (1 + 2 + 3 + 4… 100). The Sum of all the counts is 5050 and I used excel to make sure my math was write.

This was reasonably simple once I got understood that the declared variables (number / sum) are held in memory. So as long as the order is write, I can make both count.

Code Snippet
  1. Module CountSum
  2.     Sub Main()
  3.         Dim number As Integer = 1
  4.         Dim sum As Integer = number
  5.         Do While number < 100
  6.             Console.WriteLine(number & " ")
  7.             number = number + 1
  8.             Console.WriteLine(sum & " ")
  9.             sum = number + sum
  10.         Loop
  11.         Console.WriteLine()
  12.         Console.WriteLine("Total = " & sum)
  13.     End Sub
  14. End Module

So for my own entertainment I decided to add a little twist and make it start a user defined point, count by a user defined variable and count to a user defined number.

Code Snippet
  1. Module CountSum
  2.    Sub Main()
  3.       Dim number As Double
  4.       Dim sum As Double = number
  5.       Dim max As Integer
  6.       Dim addBy As Double
  7.       ' prompt for number start with
  8.       Console.WriteLine("Enter number to start at:")
  9.       number = Console.ReadLine() ' input number
  10.       ' prompt for number to add by
  11.       Console.WriteLine("Enter number to add by:")
  12.       addBy = Console.ReadLine() ' input number to add by
  13.       ' prompt for maximum
  14.       Console.WriteLine("Enter maximum number to count to:")
  15.       max = Console.ReadLine() ' input maxium
  16.  
  17.       Do While number <= max
  18.          Console.WriteLine(number & " ")
  19.          number += addBy
  20.          Console.WriteLine(sum & " ")
  21.          sum += number
  22.       Loop
  23.       Console.WriteLine()
  24.       Console.WriteLine("Total = " & sum)
  25.    End Sub
  26. End Module

I do have to get better at remembering to putting in more documentation notes into my code. It would best to start with good practices.

Now beyond the simple function I created, I love how “readable” VB.NET is compared to C based languages (C#, Java, C++, PHP). There are pluses and minuses to VB.NET, but readability is beyond anything the best one (IMO).

  • Share/Bookmark

Related posts:

  1. Having Fun…
  2. Hello Bracket Hell World
  3. Excel: Data Validation
  4. Populate a Combo Box from an Array
  5. Programmatically Copy Excel Range with VB.NET

Leave a Response