My First VB.NET Code
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.
- Module CountSum
- Sub Main()
- Dim number As Integer = 1
- Dim sum As Integer = number
- Do While number < 100
- Console.WriteLine(number & " ")
- number = number + 1
- Console.WriteLine(sum & " ")
- sum = number + sum
- Loop
- Console.WriteLine()
- Console.WriteLine("Total = " & sum)
- End Sub
- 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.
- Module CountSum
- Sub Main()
- Dim number As Double
- Dim sum As Double = number
- Dim max As Integer
- Dim addBy As Double
- ' prompt for number start with
- Console.WriteLine("Enter number to start at:")
- number = Console.ReadLine() ' input number
- ' prompt for number to add by
- Console.WriteLine("Enter number to add by:")
- addBy = Console.ReadLine() ' input number to add by
- ' prompt for maximum
- Console.WriteLine("Enter maximum number to count to:")
- max = Console.ReadLine() ' input maxium
- Do While number <= max
- Console.WriteLine(number & " ")
- number += addBy
- Console.WriteLine(sum & " ")
- sum += number
- Loop
- Console.WriteLine()
- Console.WriteLine("Total = " & sum)
- End Sub
- 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).
Related posts:
- Having Fun…
- Hello Bracket Hell World
- Excel: Data Validation
- Populate a Combo Box from an Array
- Programmatically Copy Excel Range with VB.NET
