• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

VB.net Issues

tgabe213

2[H]4U
Joined
Aug 27, 2007
Messages
3,684
Project is to create a solution that converts standard to metric. Input is miles, yards, feet, and inches and that input should get converted to kilometers, meters, centimeters, millimeters, and microns.

An example of a correct calculation is : 1 inch = 2cm, 5mm, 400 microns.

(1) I'm having 2 issues. I can't figure out how to get the calculations. The correct calculation was from her example showing some code to guide us along. I'm having issues with that and the output. Right now, if I put in 1 inch for the input, my output is '400 microns' and nothing else.

(2) I am now able to display all of my output, but how do I format it so each thing is on its own line?

Code:
'Tim Gabrhel Bus Adm 335-002 Project 1 3/1/2008

Option Explicit On
Option Strict On
Public Class Form1

    Private Sub xClearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xClearButton.Click

        'when clicking the clear button, all data is removed and focus sent to first text box
        Me.xFeet.Text = ""
        Me.xInches.Text = ""
        Me.xMiles.Text = ""
        Me.xYards.Text = ""
        Me.OutputLabel.Text = ""

        'sends focus to the miles input after cliking the clear button
        xMiles.Focus()

    End Sub

    Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
        'closes the application when clicking the exit button
        Me.Close()
    End Sub

    Private Sub xCovertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCovertButton.Click
        'declare variables
        Dim feet, inch, miles, yards As Long
        Dim kilo, meter, cm, mm, mic As Long

        'gets input from user
        Long.TryParse(Me.xMiles.Text, miles)
        Long.TryParse(Me.xYards.Text, yards)
        Long.TryParse(Me.xFeet.Text, feet)
        Long.TryParse(Me.xInches.Text, inch)

        'calculate the results
        'CONVERSION CALCULATIONS
        '1 foot = 12 inches | 1 yard = 3 feet | 1 mile = 1760 yards | 1 millimeter = 1000 microns
        '1 inch = 2.54 centimeters | 1 meter = 100 centimeters | 1 kilometer = 1000 meters

        'convert inches into microns
        mic = inch * 25400
        'number of centimeters
        cm = mic \ 10000
        'remaining microns
        mic = mic Mod 10000
        'number of millimeters
        mm = mic \ 1000
        'remaining microns
        mic = mic Mod 1000

        'display the results
        Me.OutputLabel.Text = "The metric length is:"
        Me.OutputLabel.Text = kilo.ToString() & " Kilometers"
        Me.OutputLabel.Text = meter.ToString() & " Meters"
        Me.OutputLabel.Text = cm.ToString() & " Centimeters"
        Me.OutputLabel.Text = mm.ToString() & " Millimeters"
        Me.OutputLabel.Text = mic.ToString() & " Microns"

    End Sub
End Class

Any solution or guidance would help tremendously!

edit: Focus issue fixed.
 
Here is the small .exe of what our program should do that she gave us.

EXE


What I don't understand is why the output is only displaying 1 line of code. If I delete the output for all of the variables, I get the text to show. I must be missing something that connects these..
 
Your last few lines can be simplified to this:

x = "a"
x = "b"
x = "c"
x = "d"
x = "e"

Perhaps that's enought of a hint...
 
I think I know what you mean. I'll try it in a few minutes.

Edit: You mean for the calculation or the output?
 
I know the calculation is there. It is only displaying the last line of code and I can't figure out why...
 
The problem is how you are placing the text into the textbox. Try commenting out the last line and see what results you get.

This is the first time I've helped someone in these forums, so I'm not sure on how to approach a situation like this.
 
Set a breakpoint at the beginning of xCovertButton_Click, step through your code watching the value of Me.OutputLabel.Text after each line of code executes.
 
I'm just going off of what I thought we were taught...

stop simply doing what you are taught and start thinking.
I was hoping you'd figure it out, but Pwyl_The_Destroyer's suggestion will solve your problem for you.
 
stop simply doing what you are taught and start thinking.
I was hoping you'd figure it out, but Pwyl_The_Destroyer's suggestion will solve your problem for you.

The value of the output is changing with each line, correct? That's why it is only displaying that last line. I thought about it on the way to work so I got it before his post.

At work now, but hopefully the RDP lag isn't too bad to work on it.


Edit:

Ok duh, that makes sense to me now. Maybe because I am a bit sick and tired last night. I've got it displaying all the output now, because I have it all in 1 line with &'s:

Code:
OutputLabel.Text = "The metric length is:" & kilo.ToString() & " Kilometers" & meter.ToString() & " Meters" _
        & cm.ToString() & " Centimeters" & mm.ToString() & " Millimeters" & mic.ToString() & " Microns"

Except obviously, it shows all in 1 line and wraps as necessary in the box. How do I format it without putting all of my own little spaces inbetween? Is there a way?
 
thats exactly it!
teaching coding is easy, teaching thinking is hard!
theres hope!

:D
 
I can't figure out how the existing calculation works either. mic, cm, mm, inch all start at zero (for the example calc), input 1 for inch, and run through the calculations. I just can't seem to get the right numbers. Maybe working it out on here will help, and you guys can see what I'm doing wrong (this is such simple stuff)!

'convert inches into microns
mic = inch * 25400
'number of centimeters
cm = mic \ 10000
'remaining microns
mic = mic Mod 10000
'number of millimeters
mm = mic \ 1000
'remaining microns
mic = mic Mod 1000

inch=0 | mm=0 | cm=0 | mic=0 < - start after declaration of variables
inch=1 | mm=0 | cm=0 | mic=0 < - input 1 for inches
inch=1 | mm=0 | cm=0 | mic=25400 < - mic = inch * 25400 mic = 1 * 25400
inch=1 | mm=0 | cm=2 | mic=25400 < - cm = mic \ 10000 cm = 25400\10000 (this result is the whole number only, right?
inch=1 | mm=0 | cm=2 | mic=54 < - mic = mic Mod 10000 mic = 25400 Mod 10000 (this assigns only the decimal of the result to mic)
inch=1 | mm=0 | cm=2 | mic=54 < - mm = mic \ 1000 mm = 54 \ 1000 (same as 2 lines up)
inch=1 | mm=0 | cm=2 | mic=.054 < - mic = mic Mod 1000
 
A few restrictions with the output are:
The number of meters should be less than 1000. The number of centimeters should be less than 100. The number of millimeters should be less than 10.

I really am not too sure where to go with this. The biggest thing is the calculation right now, of course I can't figure it out. Here's what I got so far....

Code:
        inputTemp = (miles * 63360) + (yards * 5280) + (feet * 12) + (inch * 1)
        inch = inputTemp

        'convert inches into microns
        mic = inch * 25400
        'number of kilometers
        kilo = mic \ 1000000
        'remaining microns
        mic = mic Mod 1000000
        'number of meters
        meter = mic \ 100000
        'remaining microns
        mic = mic Mod 100000
        'number of centimeters
        cm = mic \ 10000
        'remaining microns
        mic = mic Mod 10000
        'number of millimeters
        mm = mic \ 1000
        'remaining microns
        mic = mic Mod 1000
 
Check out the mod and floor functions.

mod:
11 % 4 = 3 (i.e., the remainder is 3 when you divide 11 by 3)

floor:

math.floor(11 / 4) = 2 (i.e., 4 goes into 11 2.75 times but we'll always round it down - i.e., 2)

Now look at the two pieces of information you have and see if you can figure out how to solve your problem :)

edit: I see you're already looking at the mod function... maybe you're even closer than you think =)
 
I really think I've got a hold of the concept. Say we have 150 dollars and we are showing how many of each bill we have. So that would be 150 \ 100 = 1 one-hundred dollar bill. Then we use the mod function to figure out what is left there. That would 150 Mod 100 = 50. And repeat, correct?

Maybe it is just a bit more confusing because of the values for the meters, cm, mm, etc but I don't know.

I'm also having difficulties figuring out what the best way to organize all of the input. My prof suggested converting everything to inches, which is what those first 2 lines are.
 
Remeber, every time you go:

mic = mic Mod 1000000

You've redefined mic... if you go on to use mic again later, it's no longer the original value.

Why don't you try adding some variables?

'remaining microns
remainder_mic = mic Mod 1000000
 
I'm also not worrying about the 'kilo must be less than 1000' right now. I'm pretty sure those will be simple if statements to subtract from kilo, add to mic
 
I can't even figure this crap out by hand now. This is really starting to drive me nuts

According to the demo program:

Input 1 mile

Output:
1 kilometer
609 meters
34 centimeters
4 millimeters
0 microns

Yeah, as you can see I am sort of thinking out loud with this too
 
Getting the calculations started and keeping meters under 1000 is really screwing me up. I totally understand the concept and when mic is actually changing.

  • input 1 mile, click calculate button
  • inputTemp = (miles * 63360) + (yards * 5280) + (feet * 12) + (inch * 1)
  • inputTemp = 63360
  • inch = inputTemp
  • mic = inch * 25400
  • mic = 1609344000
  • kilo = mic \ 1000000
  • kilo = 1609

And here is where I am stuck. Should this be an if function?
if (kilo > 1000) then
meter = kilo - 1000
kilo = kilo - meter
end if

  • meter = (mic \ 100000) + meter <the '+ meter' comes into play when there were over 1000 meters, and I took that value away from kilo


I see how it works to take 1000 meters from kilo when necessary, but what happens when there is 2000 stored in kilo right away? FUCK
 
Now kilo is somehow getting 1000 assigned to it. Remember, I am doing all of this by testing the output when input is 1 mile.
 
GETTING CLOSER! Ok, here are the calculations.

Code:
        inputTemp = (miles * 63360) + (yards * 5280) + (feet * 12) + (inch * 1)
        inch = inputTemp

        'convert inches into microns
        mic = inch * 25400
        'number of kilometers
        kilo = mic \ 1000000

        If (kilo > 1000) Then
            meter = kilo - 1000
            kilo = CLng((kilo - meter) / 1000)
        End If

        'remaining microns
        mic = mic Mod 1000000
        'number of centimeters
        cm = mic \ 10000
        'remaining microns
        mic = mic Mod 10000
        'number of millimeters
        mm = mic \ 1000
        'remaining microns
        mic = mic Mod 1000

FINALLY I have the correct output. However, meters cannot go above 1000. This works all fine and dandy right now, but when I put in 2 miles, I get:

  • 1 Kilometer
  • 2218 Meters
  • 68 centimeters
  • 8 millimeters
  • 0 microns

Advice to limit meters? :rolleyes:
 
GETTING CLOSER! Ok, here are the calculations.

Advice to limit meters? :rolleyes:

good job so far!

Instead of:

'number of kilometers
kilo = mic \ 1000000

Find a way to use mod so that kilo can go higher than 1 --> meters can't go higher than 1000.
 
You've already done all the work when you converted to cm, mm, microns... why are you treating kilometers differently?
 
Still... you're treating kilometers different from centimeters. Try treating them the same.
 
Referring to the if function...?

edit:

You must be referring to this line?

kilo = CLng((kilo - meter) / 1000)

When removing that, kilo's show as 1609 and meters show 609
 
No.. the if function is on kilometers and kilometers are not calculated correctly.

You're not using an if function on centemeters and they are being calculated correctly.
 
There will eventually be an if function on cm too because I will need to make sure they don't go higher than 100.
 
Ok I see what is wrong with the function, but I'm not sure how to fix it. I had it integer dividing by 1000.
 
Were you trying to say that I'm treating meters, not kilometers, different from centimeters? I just realized I don't have an actual calculation for meters in there.
 
I'm getting there, but now backtracking. the 1 mile calculation no longer works, but if I input 11 miles, kilometers is displaying more than 1.

Code:
        'convert inches into microns
        mic = inch * 25400

        'number of kilometers
        kilo = mic \ 1000000
        'remaining microns
        mic = mic Mod 1000000

        If (kilo > 1000) Then
            kilo = kilo \ 1000
        End If


        'number of meters
        meter = mic \ 100000
        'remaining microns
        mic = mic Mod 100000

        'number of centimeters
        cm = mic \ 10000
        'remaining microns
        mic = mic Mod 10000

        'number of millimeters
        mm = mic \ 1000
        'remaining microns
        mic = mic Mod 1000
 
So I was missing 3 zero's when calculating kilometers. I was using 1 million, instead of 1 billion. Running through the requirements to make sure I've done everything. :eek:


Code:
        inputTemp = (miles * 63360) + (yards * 5280) + (feet * 12) + (inch * 1)
        inch = inputTemp

        'convert inches into microns
        mic = inch * 25400

        'number of kilometers
        kilo = mic \ 1000000000
        'remaining microns
        mic = mic Mod 1000000000

        'number of meters
        meter = mic \ 1000000
        'remaining microns
        mic = mic Mod 1000000

        'number of centimeters
        cm = mic \ 10000
        'remaining microns
        mic = mic Mod 10000
        'number of millimeters

        mm = mic \ 1000
        'remaining microns
        mic = mic Mod 1000


        'display the results
        OutputLabel.Text = "The metric length is:" _
        & vbCrLf & kilo.ToString() & " Kilometers" _
        & vbCrLf & meter.ToString() & " Meters" _
        & vbCrLf & cm.ToString() & " Centimeters" _
        & vbCrLf & mm.ToString() & " Millimeters" _
        & vbCrLf & mic.ToString() & " Microns"

Thanks for the help!
 
Back
Top