how to improve efficiency

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

51 Replies - 1948 Views - Last Post: 27 May 2011 - 09:51 PM Rate Topic: -----

#1 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

how to improve efficiency

Posted 27 May 2011 - 04:05 AM

is there a more efficient means of programming this

Dim lesson As String = cmbSession.SelectedIndex
        Dim changed As String = lstNames.SelectedItem
        Dim chosen As String = cmbChange.SelectedIndex

        If lstupdate.Items.Count = 10 Then
            MsgBox("Classes can have a maximum of 10 students")
            Return
        End If

        lstNames.Items.Remove(changed)
        lstupdate.Items.Add(changed)

        If lesson = 0 Then
            Dim copyback1 As StreamWriter = File.CreateText("SaturdayCloud.txt")
            For Each entry1 As Object In lstNames.Items
                copyback1.WriteLine(entry1)
            Next
            copyback1.Close()

        ElseIf lesson = 1 Then
            Dim copyback2 As StreamWriter = File.CreateText("wednesdaycloud.txt")
            For Each entry2 As Object In lstNames.Items
                copyback2.WriteLine(entry2)
            Next
            copyback2.Close()

        ElseIf lesson = 2 Then
            Dim copyback3 As StreamWriter = File.CreateText("wednesdaymotoko.txt")
            For Each entry3 As Object In lstNames.Items
                copyback3.WriteLine(entry3)
            Next
            copyback3.Close()

        ElseIf lesson = 3 Then
            Dim copyback4 As StreamWriter = File.CreateText("tuesdayMotoko.txt")
            For Each entry4 As Object In lstNames.Items
                copyback4.WriteLine(entry4)
            Next
            copyback4.Close()
        End If


        If chosen = 0 Then
            Dim saveback1 As StreamWriter = File.CreateText("SaturdayCloud.txt")
            For Each entry1 As Object In lstupdate.Items
                saveback1.WriteLine(entry1)
            Next
            saveback1.Close()

        ElseIf chosen = 1 Then
            Dim saveback2 As StreamWriter = File.CreateText("wednesdaycloud.txt")
            For Each entry2 As Object In lstupdate.Items
                saveback2.WriteLine(entry2)
            Next
            saveback2.Close()

        ElseIf chosen = 2 Then
            Dim saveback3 As StreamWriter = File.CreateText("wednesdaymotoko.txt")
            For Each entry3 As Object In lstupdate.Items
                saveback3.WriteLine(entry3)
            Next
            saveback3.Close()

        ElseIf chosen = 3 Then
            Dim saveback4 As StreamWriter = File.CreateText("tuesdayMotoko.txt")
            For Each entry4 As Object In lstupdate.Items
                saveback4.WriteLine(entry4)
            Next
            saveback4.Close()
        End If


Is This A Good Question/Topic? 0
  • +

Replies To: how to improve efficiency

#2 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: how to improve efficiency

Posted 27 May 2011 - 04:07 AM

Switch case ?:)
Was This Post Helpful? 0
  • +
  • -

#3 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 04:29 AM

Well, a select case block would look nicer and flow nicer, gimme a sec to type up an example and I'll edit it in.

Also, a note, the lesson variable in your program should probably be a numeric rather than a string, considering how you use it.

Edit: Here's a really basic example of the Select Case block:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lesson As Integer
        lesson = Integer.Parse(TextBox1.Text)

        Select Case lesson
            Case 1
                MsgBox("1")
            Case 2
                MsgBox("2")
            Case 3
                MsgBox("3")
            Case Is > 3
                MsgBox("> 3")
            Case Else
                MsgBox("Else")
        End Select

    End Sub


EditEdit: Also another way to improve efficiency is to not repeat code, make a function/sub to handle making that streamwriter, with the select case block serving to pass the string you need to the function/sub rather than simply using the same code over and over again with different arguments.

EditEditEdit: I extended my previous example to show what I mean by making a separate sub:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lesson As Integer
        lesson = Integer.Parse(TextBox1.Text)

        Select Case lesson
            Case 1
                writeStream("wednesdaycloud.txt")
        End Select
    End Sub
    Private Sub writeStream(ByVal textFile As String)
        Dim IOOut As StreamWriter = File.CreateText(textFile)
        For Each entry As Object In ListBox1.Items
            IOOut.WriteLine(entry)
        Next
        IOOut.Close()
    End Sub

This post has been edited by Tarkenfire: 27 May 2011 - 04:52 AM

Was This Post Helpful? 2
  • +
  • -

#4 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 06:49 AM

writestream does not exist as an operation

Parse(TextBox1.Text)
i have a combo box and if i change it to combobox1.text
it causes a runtime error
Was This Post Helpful? 0
  • +
  • -

#5 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: how to improve efficiency

Posted 27 May 2011 - 06:50 AM

Witch Runtime error ?
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,177
  • Joined: 02-July 08

Re: how to improve efficiency

Posted 27 May 2011 - 06:56 AM

Did you see this Sub he made?
Private Sub writeStream(ByVal textFile As String)
  Dim IOOut As StreamWriter = File.CreateText(textFile)
  For Each entry As Object In ListBox1.Items
    IOOut.WriteLine(entry)
  Next
  IOOut.Close()
End Sub

Was This Post Helpful? 0
  • +
  • -

#7 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 06:57 AM

formate exception
Input string was not in a correct format.
Was This Post Helpful? 0
  • +
  • -

#8 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: how to improve efficiency

Posted 27 May 2011 - 06:58 AM

View Posttheazza, on 27 May 2011 - 06:57 AM, said:

formate exception
Input string was not in a correct format.

The it probably was no string that you give.


What did you give as input ? :)
Was This Post Helpful? 0
  • +
  • -

#9 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 06:59 AM

View Posthawkvalley1, on 27 May 2011 - 06:56 AM, said:

Did you see this Sub he made?
Private Sub writeStream(ByVal textFile As String)
  Dim IOOut As StreamWriter = File.CreateText(textFile)
  For Each entry As Object In ListBox1.Items
    IOOut.WriteLine(entry)
  Next
  IOOut.Close()
End Sub


writestream does not exist as an event
Was This Post Helpful? 0
  • +
  • -

#10 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: how to improve efficiency

Posted 27 May 2011 - 07:00 AM

View Posttheazza, on 27 May 2011 - 06:59 AM, said:

View Posthawkvalley1, on 27 May 2011 - 06:56 AM, said:

Did you see this Sub he made?
Private Sub writeStream(ByVal textFile As String)
  Dim IOOut As StreamWriter = File.CreateText(textFile)
  For Each entry As Object In ListBox1.Items
    IOOut.WriteLine(entry)
  Next
  IOOut.Close()
End Sub


writestream does not exist as an event

Can we see the full code ?
Was This Post Helpful? 0
  • +
  • -

#11 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 07:02 AM

thats what i did

 Dim lesson As Integer
        lesson = Integer.Parse(cmbSession.Text)

        Select Case lesson
            Case 1
                Writestream("wednesdaycloud.txt")

        End Select

    End Sub

    Private Sub write(ByVal textfile As String)

        Dim IOOut As StreamWriter = File.CreateText(textfile)
        For Each entry As Object In lstNames.Items
            IOOut.WriteLine(entry)
        Next
        IOOut.Close()

    End Sub


now i only have the input error thing everything else is fine
Was This Post Helpful? 0
  • +
  • -

#12 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: how to improve efficiency

Posted 27 May 2011 - 07:03 AM

View Posttheazza, on 27 May 2011 - 07:01 AM, said:

thats what i did

 Dim lesson As Integer
        lesson = Integer.Parse(cmbSession.Text)
]
        Select Case lesson
            Case 1
               
Writestream("wednesdaycloud.txt") //This is your error the name must be write. the same as your private sub :)/>

        End Select

    End Sub

    Private Sub write(ByVal textfile As String)

        Dim IOOut As StreamWriter = File.CreateText(textfile)
        For Each entry As Object In lstNames.Items
            IOOut.WriteLine(entry)
        Next
        IOOut.Close()

    End Sub

Writestream is not the same as your sub name type write and it will work :)

This post has been edited by cornetto456: 27 May 2011 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#13 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 07:06 AM

  Dim lesson As Integer
        lesson = Integer.Parse(cmbSession.Text)


this part of the code has something wrong in it
Was This Post Helpful? 0
  • +
  • -

#14 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: how to improve efficiency

Posted 27 May 2011 - 07:08 AM

View Posttheazza, on 27 May 2011 - 07:06 AM, said:

  Dim lesson As Integer
        lesson = Integer.Parse(cmbSession.Text)


this part of the code has something wrong in it

The best method is tho put it in a Try catch. because if you put a string in the cmbSession then you will get an error and you should look at the name of the sub and rename it that's also an error ;)


EDIT the code must look like this i think :

Dim lesson As Integer
       lesson = Integer.Parse(cmbSession.Text)

       Select Case lesson
           Case 1
               write("wednesdaycloud.txt")

       End Select

   End Sub

   Private Sub write(ByVal textfile As String)

       Dim IOOut As StreamWriter = File.CreateText(textfile)
       For Each entry As Object In lstNames.Items
           IOOut.WriteLine(entry)
       Next
       IOOut.Close()

   End Sub



Try this and add at the [il]lesson = Integer.Parse(cmbSession.Text)[/i]

A try/Catch

Good luck


Arno,Cornetto456

This post has been edited by cornetto456: 27 May 2011 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

#15 theazza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-May 11

Re: how to improve efficiency

Posted 27 May 2011 - 07:11 AM

i fixed the name of the sub but how would i go about using a try catch
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »