|
-
Nov 9th, 2017, 03:26 PM
#1
Thread Starter
New Member
A question of more efficient coding.
I've just completed another lab for my VB Programming class and I've noticed that it seems lengthy. I'm sure it's an overuse of if statements and just being new to the programming world. Being new, I only use what I've been taught in class as anything beyond that is beyond me. Please bare that in mind. 
This program works (I believe the math is correct[?]), but ultimately, it looks somewhat sloppy to me. Any suggestions of methods for shortening code and making things less tedious for a newbie?
Code:
Public Class Form1
'Jaycoba
'11/09/2017
'Array Data
'Table1; AWG# (0-30) and Area
Dim Table1() As Single =
{105530, 83694, 66373, 52634, 41742, 33102,
26250, 20816, 16509, 13094, 10381, 8234, 6529,
5178.4, 4106.8, 3256.7, 2582.9, 2048.2, 1624.3,
1288.1, 1021.5, 810.1, 642.4, 509.45, 404.01,
320.4, 254.1, 201.5, 159.79, 126.72, 100.5}
'Table 2; Wire Metals and Resistivity (0 - 5)
Dim Table2() As Single =
{9.9, 10.37, 14.7, 17, 47, 74}
'Table 3; Fuze Sizes (in amperes, 0-49)
Dim Table3() As Single =
{1, 3, 6, 10, 15, 20, 25, 30, 35, 40, 45, 50,
60, 70, 80, 90, 100, 110, 125, 150, 175, 200,
225, 250, 300, 400, 450, 500, 600, 700, 800, 1000,
1200, 1600, 2000, 2500, 3000, 4000, 5000, 6000}
'Variables
Dim R, A, l, p, I As Single
Dim metal As Single
Dim V As Single = 120
'Retrieves Cross Sectional Area From Table1 by inputting a Wire Gage value from 0-30
Function AreaOutput(ByVal AWG As Single) As Single
Dim Area As Single
'Outputs an error message if input # is over 30 and Exits Function on OKAY
If AWG > 30 Then MsgBox("# Exceeds Highest Wire Gage", 0, "Error")
If AWG > 30 Then Exit Function
Area = Table1(AWG)
AreaOutput = Area
End Function
'Retrieves Resistivity of Metal from Table 2 by selecting a radio button
Function Resistivity(ByVal metal As Single) As Single
Dim ResistanceOfMetal As Single
ResistanceOfMetal = Table2(metal)
Resistivity = ResistanceOfMetal
End Function
'Retrieves Fuse Size as a result of the calculation performed for the Current
Function FuseSize(ByVal I As Integer) As Integer
Dim count As Integer
Dim Amperes As Single
For count = 0 To 39
If I < Table3(count) Then Amperes = Table3(count)
FuseSize = Amperes
If I > 6000 Then MsgBox("Fuze Size has reached its Max (6000). Try a different arrangement.", 0, "Error")
If I > 6000 Then Exit For
If FuseSize = Table3(count) Then Exit For
Next
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Radio Buttons for the 6 types of metals
If Ag.Checked = True Then metal = 0
If Cu.Checked = True Then metal = 1
If Au.Checked = True Then metal = 2
If Al.Checked = True Then metal = 3
If Ni.Checked = True Then metal = 4
If Fe.Checked = True Then metal = 5
'Assigning values
p = Resistivity(metal)
A = AreaOutput(MaskedTextBox1.Text)
l = TextBox1.Text
'Calculations for Resistance and Current, respectively.
R = p * l / A
I = V / R
'Output of the calculations
TextBox2.Text = I
TextBox3.Text = FuseSize(I)
End Sub
End Class
The names of variables are kind of repeated (I.E. A, Area, AreaOutput, etc.). I wasn't sure what to name them to reduce confusion. I apologize if they're too similar.
I hope to use the knowledge gained from this site for other computer languages as well, so please don't spare any details!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|