473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Imbedded for/next?

Hi all. I'm starting on my next part of my teach myself vb program. What I am trying to do is on button click open a textfile showing the math as it would be done long hand. I started writing the code and now I think There might be a better way of doing it than what I have been doing. Here's the code as far as it goes.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         ' Declarations
  4.         Dim sw As StreamWriter
  5.         Dim sr As StreamReader
  6.         Dim path1 As String = "Showme.Txt"
  7.         Dim longest As Integer
  8.         Dim length1 As Integer
  9.         Dim length2 As Integer
  10.         Dim length3 As Integer
  11.         Dim length4 As Integer
  12.         Dim length5 As Integer
  13.         Dim line1 As String = AddBox1.Text
  14.         Dim line2 As String = AddBox2.Text
  15.         Dim line3 As String = AddBox3.Text
  16.         Dim line4 As String = AddBox4.Text
  17.         Dim line5 As String = AddBox5.Text
  18.         Dim AnsLine As String = "_"
  19.         Dim Inta As Integer
  20.         Dim Intb As Integer
  21.         Dim Intc As Integer
  22.         Dim Intd As Integer
  23.         Dim Inte As Integer
  24.         Dim intf As Integer
  25.  
  26.         length1 = line1.Length
  27.         length2 = line2.Length
  28.         length3 = line3.Length
  29.         length4 = line4.Length
  30.         length5 = line5.Length
  31.         Dim addspace As String = " "
  32.         ' Deletes file if exists then recreates a new file to write to
  33.         If File.Exists(path1) Then
  34.             File.Delete(path1)
  35.         End If
  36.         sw = File.CreateText(path1)
  37.         sw.WriteLine("Welcome to Show Me")
  38.         sw.WriteLine()
  39.         sw.WriteLine("Here we will take the numbers you have entered and show")
  40.         sw.WriteLine("you how to add them to get an answer.")
  41.         sw.WriteLine()
  42.         sw.WriteLine("First lets get the sumbers you entered")
  43.         sw.WriteLine()
  44.         'Checks to see if an entry was made in each textbox and writes 
  45.         'to file only if true. Does this for all 5 textboxes
  46.         If length1 > 0 Then
  47.             sw.WriteLine((Val(AddBox1.Text)))
  48.         End If
  49.         If length2 > 0 Then
  50.             sw.WriteLine((Val(AddBox2.Text)))
  51.         End If
  52.         If length3 > 0 Then
  53.             sw.WriteLine((Val(AddBox3.Text)))
  54.         End If
  55.         If length4 > 0 Then
  56.             sw.WriteLine((Val(AddBox4.Text)))
  57.         End If
  58.         If length5 > 0 Then
  59.             sw.WriteLine((Val(AddBox5.Text)))
  60.         End If
  61.  
  62.         ' More text
  63.         sw.WriteLine()
  64.         sw.WriteLine("And line up the last digits.")
  65.         sw.WriteLine()
  66.  
  67.         ' Checks the length of each string and sets "longest" 
  68.         ' to the length of the longest string
  69.         If length1 >= length2 Then
  70.             longest = length1
  71.         Else
  72.             longest = length2
  73.         End If
  74.  
  75.         If longest >= length3 Then
  76.             longest = longest
  77.         Else
  78.             longest = length3
  79.         End If
  80.  
  81.         If longest >= length4 Then
  82.             longest = longest
  83.         Else
  84.             longest = length4
  85.         End If
  86.  
  87.         If longest >= length5 Then
  88.             longest = longest
  89.         Else
  90.             longest = length5
  91.         End If
  92.  
  93.         ' Insert a space at the beginning of the string for each
  94.         ' instance of a string until all strings are the same length
  95.         ' this will line up the last digits of each entry
  96.         Do Until length1 = longest
  97.             line1 = line1.Insert(0, addspace)
  98.             length1 = line1.Length
  99.         Loop
  100.         'Writes the new string if it is numeric
  101.         If (IsNumeric(line1)) Then
  102.             sw.WriteLine(line1)
  103.         End If
  104.         Do Until length2 = longest
  105.             line2 = line2.Insert(0, addspace)
  106.             length2 = line2.Length
  107.         Loop
  108.         If (IsNumeric(line2)) Then
  109.             sw.WriteLine(line2)
  110.         End If
  111.         Do Until length3 = longest
  112.             line3 = line3.Insert(0, addspace)
  113.             length3 = line3.Length
  114.         Loop
  115.         If (IsNumeric(line3)) Then
  116.             sw.WriteLine(line3)
  117.         End If
  118.         Do Until length4 = longest
  119.             line4 = line4.Insert(0, addspace)
  120.             length4 = line4.Length
  121.         Loop
  122.         If (IsNumeric(line4)) Then
  123.             sw.WriteLine(line4)
  124.         End If
  125.         Do Until length5 = longest
  126.             line5 = line5.Insert(0, addspace)
  127.             length5 = line5.Length
  128.         Loop
  129.         If (IsNumeric(line5)) Then
  130.             sw.WriteLine(line5)
  131.         End If
  132.  
  133.         ' Creates a line of proper length to place under the math problem
  134.         Do Until AnsLine.Length = longest
  135.             AnsLine = AnsLine.Insert(0, "_")
  136.         Loop
  137.  
  138.         ' More text
  139.         sw.WriteLine(AnsLine)
  140.         sw.WriteLine()
  141.         sw.WriteLine("Now that we have lined up the last digit of each number")
  142.         sw.WriteLine("we can begin to add our numbers together.")
  143.         sw.WriteLine("First we'll the last numbers, or the numbers farthest right.")
  144.         sw.WriteLine()
  145.  
  146.  
  147.         If line1.Length > 0 And (IsNumeric(line1)) Then
  148.             Inta = line1.Substring(line1.Length - 1, 1)
  149.             sw.WriteLine((Val(Inta)))
  150.         End If
  151.         If line2.Length > 0 And (IsNumeric(line2)) Then
  152.             Intb = line2.Substring(line2.Length - 1, 1)
  153.             sw.WriteLine((Val(Intb)))
  154.         End If
  155.         If line3.Length > 0 And (IsNumeric(line3)) Then
  156.             Intc = line3.Substring(line3.Length - 1, 1)
  157.             sw.WriteLine((Val(Intc)))
  158.         End If
  159.         If line4.Length > 0 And (IsNumeric(line4)) Then
  160.             Intd = line4.Substring(line4.Length - 1, 1)
  161.             sw.WriteLine((Val(Intd)))
  162.         End If
  163.         If line5.Length > 0 And (IsNumeric(line5)) Then
  164.             Inte = line5.Substring(line5.Length - 1, 1)
  165.             sw.WriteLine((Val(Inte)))
  166.         End If
  167.  
  168.         intf = ((Val(Inta)) + (Val(Intb)) + (Val(Intc)) + (Val(Intd)) + (Val(Inte)))
  169.         sw.WriteLine()
  170.         sw.WriteLine("Now let's add up the numbers and see what our answer is.")
  171.         sw.WriteLine()
  172.         sw.WriteLine((Val(intf)))
  173.  
  174.         If intf > 9 Then
  175.             sw.WriteLine()
  176.             sw.WriteLine("Since our answer is over 9 we cant simply write the answer")
  177.             sw.WriteLine("Because if we did our final answer would be wrong.")
  178.             sw.WriteLine("We will need to do what is called Carrying.")
  179.             sw.WriteLine()
  180.             sw.WriteLine("This means that we are going to take the first number of our")
  181.             sw.WriteLine("answer and carry it over to the next row and place it on top")
  182.             sw.WriteLine("of it like so.")
  183.  
  184.             Dim Carry As String = intf
  185.             Dim CarryLen As Integer = Carry.Length   
  186.             Do Until CarryLen = longest
  187.                 Carry = Carry.Insert(0, addspace)
  188.                 CarryLen = Carry.Length
  189.             Loop
  190.  
  191.             Dim carrydrop As String = Carry.Substring(longest - 1, 1)
  192.             Dim droplen As Integer
  193.             Do Until droplen = longest
  194.                 carrydrop = carrydrop.Insert(0, addspace)
  195.                 droplen = carrydrop.Length
  196.             Loop
  197.  
  198.             Dim Carry1 As String = Carry.Substring(longest - 2, 1)
  199.             Dim Carry1Len As Integer
  200.             Do Until Carry1Len = (longest - 1)
  201.                 Carry1 = Carry1.Insert(0, addspace)
  202.                 Carry1Len = Carry1.Length
  203.             Loop
  204.  
  205.             sw.WriteLine()
  206.             sw.WriteLine(Carry1)
  207.             sw.WriteLine(line1)
  208.             sw.WriteLine(line2)
  209.             sw.WriteLine(line3)
  210.             sw.WriteLine(line4)
  211.             sw.WriteLine(line5)
  212.             sw.WriteLine(AnsLine)
  213.             sw.WriteLine()
  214.             sw.WriteLine("Next we'll place the second number under the row we added")
  215.             sw.WriteLine()
  216.             sw.WriteLine(Carry1)
  217.             sw.WriteLine(line1)
  218.             sw.WriteLine(line2)
  219.             sw.WriteLine(line3)
  220.             sw.WriteLine(line4)
  221.             sw.WriteLine(line5)
  222.             sw.WriteLine(AnsLine)
  223.             sw.WriteLine(carrydrop)
  224.             sw.WriteLine()
  225.             sw.WriteLine("Now we need to add up the next line dont forget to add")
  226.             sw.WriteLine("the number we carried over.")
  227.             sw.WriteLine()
  228.  
  229.             If line1.Length > 0 And (IsNumeric(line1)) Then
  230.                 Inta = line1.Substring(line1.Length - 2, 1)
  231.                 sw.WriteLine((Val(Inta)))
  232.             End If
  233.             If line2.Length > 0 And (IsNumeric(line2)) Then
  234.                 Intb = line2.Substring(line2.Length - 2, 1)
  235.                 sw.WriteLine((Val(Intb)))
  236.             End If
  237.             If line3.Length > 0 And (IsNumeric(line3)) Then
  238.                 Intc = line3.Substring(line3.Length - 2, 1)
  239.                 sw.WriteLine((Val(Intc)))
  240.             End If
  241.             If line4.Length > 0 And (IsNumeric(line4)) Then
  242.                 Intd = line4.Substring(line4.Length - 2, 1)
  243.                 sw.WriteLine((Val(Intd)))
  244.             End If
  245.             If line5.Length > 0 And (IsNumeric(line5)) Then
  246.                 Inte = line5.Substring(line5.Length - 2, 1)
  247.                 sw.WriteLine((Val(Inte)))
  248.             End If
  249.             sw.WriteLine()
  250.             sw.WriteLine("And our answer is")
  251.             sw.WriteLine()
  252.             intf = (Val(carry1)) + ((Val(Inta)) + (Val(Intb)) + (Val(Intc)) + (Val(Intd)) + (Val(Inte)))
  253.             sw.WriteLine(intf)
  254.  
  255.         Else
  256.  
  257.             Dim Carry As String = intf
  258.             Dim CarryLen As Integer = Carry.Length
  259.             Do Until CarryLen = longest
  260.                 Carry = Carry.Insert(0, addspace)
  261.                 CarryLen = Carry.Length
  262.             Loop
  263.  
  264.             Dim carrydrop As String = Carry.Substring(longest - 1, 1)
  265.             Dim droplen As Integer
  266.             Do Until droplen = longest
  267.                 carrydrop = carrydrop.Insert(0, addspace)
  268.                 droplen = carrydrop.Length
  269.             Loop
  270.  
  271.             Dim Carry1 As String = Carry.Substring(longest - 2, 1)
  272.             Dim Carry1Len As Integer
  273.             Do Until Carry1Len = (longest - 1)
  274.                 Carry1 = Carry1.Insert(0, addspace)
  275.                 Carry1Len = Carry1.Length
  276.             Loop
  277.             sw.WriteLine("Since our answer is less than 10 we would write the answer")
  278.             sw.WriteLine("below the numbers we just added")
  279.             sw.WriteLine()
  280.             sw.WriteLine(line1)
  281.             sw.WriteLine(line2)
  282.             sw.WriteLine(line3)
  283.             sw.WriteLine(line4)
  284.             sw.WriteLine(line5)
  285.             sw.WriteLine(AnsLine)
  286.             sw.WriteLine(carrydrop)
  287.             sw.WriteLine()
  288.         End If
  289.  
  290.         sw.Flush()
  291.         sw.Close()
  292.  
  293.  
  294.  
  295.         Diagnostics.Process.Start(path1)
  296.  
  297.  
  298.     End Sub
  299.  
Now is the way I am going the best way or could i possibly make it shorter and easier by doing some kind of imbedded for/next style loop? If so could someone give me an idea how, or possibley a snippit of some other code that does simular to what I am trying that I can use for reference? Or even a whole new direction that may be better suited.

Thanks in advance,
James
Mar 24 '08 #1
0 1272

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: gabriel | last post by:
Is there such a parser out there? I'm using XMLSPY.. can its native parser handle imbedded Schematron and validate an instace against it? /Gabriel
2
by: Bengt Richter | last post by:
Is this a well known bug that's been fixed? I couldn't find any discussion of it, but maybe my googling's off today ;-/ >>> def foo(): ... it = iter(range(10)) ... while True: ... ...
7
by: simonwittber | last post by:
>>> gen = iterator() >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next <method-wrapper object at 0x009D1BB0> >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next...
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
1
by: Tom | last post by:
I can't get the Microsoft Jet OLEDB 4.0 driver to read a tab delimited text file that has double quotes imbedded in the data. For example Field1 | Field2 | Field3 Bob | Pant Size 34" Waist | 22...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
0
ADezii
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.