I am working on a program and I almost have it working the way I want except for two problems. The first is major; I can not for the life of me get my answer for the population to equal anything but zero. I have a feeling this is due to my combo box selection but Im not positive and Im not sure how to fix it. My second problem is with my combo boxes I can select a number and have something pop up in the list box but if I type in and enter my own number into the combo box nothing appears in my list box.

Public Class Form1

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim GrowthRate As Integer
        Dim NumDays As Integer
        Dim population As Integer
        Dim DailyPercent As Integer
        DailyPercent = CBool(txtAveDailyInc.Text)

        GrowthRate = 1 * (DailyPercent / 100)
        NumDays = CInt(cboDaysToMultiply.SelectedItem)
        Dim Day As Integer = 1

        lstFinalData.Items.Clear()

        For Day = 1 To NumDays
            population = GrowthRate * Day
            lstFinalData.Items.Add("end of day " & Day & " population is " & population)
        Next
    End Sub
End Class

Recommended Answers

All 9 Replies

I noticed a couple of things.

First of all, you should have posted this in the thread you already statrted for this problem. Other than that:

GrowthRate = 1 * (DailyPercent / 100)

should be

GrowthRate = 1 + (DailyPercent / 100)

But even that won't work because you have the wrong type for DailyPercent. It should be an integer or a double depending on whether or not you allow fractions of a percent. Making it bool essentially means it has the value True or False.

One last suggestion. It is good form to limit the scope of variables. Because Day is used only in the loop it is better not to declare it outside the loop. A better form would be

For Day As Integer = 1 To NumDays

Pythagoras would have wanted this, but alas growth rates don't very often express themselves as Integer. So why are you defining yor variables as integers instead of doubles? Converting to boolean on line 8, as RJ points out boils down to true and false. false = 0 and true is 1. So GrowthRate would be zero I guess. These kind of "tricks" is one of the reasons why VB is not my favorite language anymore.

I don't see how that is a VB specific problem. Using an inappropriate variable type is something that can be done in just about every language. A way around that would be to disable implicit casting.

No, I admit, it is'nt a VB specific problem. But Jim, did you ever tried C#?
You can't do this in C#.

bool b = false;
int i = (int)b; //will not work or compile

while in VB(and others) this would easily pass:

Dim b As Boolean = False
Dim i As Integer = b

In C# a boolean is a boolean and not an integer.

That's why they have Option Strict On. If you set this as a default option (as is advised for beginners) your second example gets flagged as an error and will not compile. I suppose I should have included that in my unsolicited advice from the previous thread. I've never used that option because I am very careful in my implicit casting. I find it obfuscates the code in cases like

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim btn As Button = sender
    .
    .
    .
End Sub

which is a little less typing (but a little sloppier, I admit) than

Dim btn As Button = DirectCast(sender, Button)

I suppose I should have a look at C#. I just can't seem to get motivated. It reminds me too much of java and I really hate java.

I don't like Java very much either. But C# is different, your code would simply become Button btn = sender as Button; No options or so required. But don't let that trick you, C# can be quite daunting. If you ever find the time and the need, you could perhaps read "C# programming" by Jonas Fagerberg.

I have a very large computer library that includes

C# 2008 Programmer's Reference
Professional C# 2012 and .NET 4
Beginning Visual C# 2012 Programming
Beginning Oriented Programming with C#
OOP with Microsoft Visual Basic .NET and Microsoft Visual C#.NET Step By Step

Lots of books, but like I said, just a lack of motivation. I start on a project in C# and stumble around, then decide I'd rather just blast it out quickly in VB. Usually when I start to write a program it's because I need it now.

Usually when I start to write a program it's because I need it now.
Haha, we're opposites I guess, what you do in VB, I do in C# with .NET in common.
It would be a dull world if we all think the same. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.