In A Funtion

MRRX8

Newcomer
Joined
Jan 29, 2006
Messages
7
Hi Everybody

Im fairly new to .net i am after some help with a function. I have put this code into a click procedure however what i want it to do is put it in a function or class so i can call it whenever i like. This is the code

Code:
    Dim Litem As New ListViewItem
        Dim totea As Integer
        Dim ctrl As Control
        Dim iCtrl, iChk, iTotalEA As Integer
        Dim bb() As BoxedBird

        For iCtrl = 0 To Panel1.Controls.Count - 1
            ctrl = Panel1.Controls(iCtrl)
            If ctrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                ReDim Preserve bb(iCtrl)
                bb(iCtrl).SIZE = ((CType(ctrl, TextBox).Tag))
                iChk = modFunctions.CheckTextBoxForString((CType(ctrl, TextBox).Text))
                bb(iCtrl).Boxed = iChk
                iTotalEA = modFunctions.CalculateTotalBirdEach(iChk, (CType(ctrl, TextBox).Tag))
                bb(iCtrl).bTotal = iTotalEA
                totea = totea + iTotalEA
            End If
        Next
        'Populate whats in the array iterate 
        Dim i As Integer
        Dim LItem1 As ListViewItem

        For i = 0 To bb.GetUpperBound(0)
            LItem1 = lvBoxed.Items.Add(bb(i).SIZE)
            LItem1.SubItems.Add(bb(i).Boxed)
            LItem1.SubItems.Add(bb(i).bTotal)
        Next


        Litem.ForeColor = (Color.Green)
        Litem.Text = UCase(dsf.sSize)
        Litem.SubItems.Add(dsf.dWeight)
        Litem.SubItems.Add(dsf.iCount)
        Litem.SubItems.Add(dsf.dAvSizeWeight)
        Litem.SubItems.Add(dsf.dBoxedWeight)
        Litem.SubItems.Add(dsf.dCutUpSizeTotalKg)
        Litem.SubItems.Add(dsf.dTotalSizeMaryland)
        Litem.SubItems.Add(dsf.dTotalSizeWholeBreast)
        Litem.SubItems.Add(dsf.dTotalSizeWings)
        Litem.SubItems.Add(dsf.dTotalSizeBRFillet)

        Litem.ImageIndex = 0
        lvStockDetails.Items.Add(Litem)

Only to put this code in a function i have no idea how to do it ive tried several ways unsuccessfully, not sure if i can have an array in a fucntion or not. I would like it if somebody could please help me writing this as a function

thank you

MRRX8
 
You should be able to put the code in a sub in the same file as the click event.

Visual Basic:
private sub MeaningfulNameForWhatYourCodeDoes()
  ''Put your code here
End Sub
'
'
'You click event, in the same file, in the same class
Private Sub Click_method(e as event args, sender as Object)
   MeaningfulNameForWhatYourCodeDoes()
End Sub

Unless there is a catch that I missed, that should work just fine. Let me know if that works or not and what errors you get if it doesn't. As far as moving your code to a new class, that doesn't really make sense becuase you are accessing controls and you'll need to access them from within the class.
 
Thank you for your reply

What i dont really understand is the arguments that you are passing in the function. I thought that i would need to pass down all the controls and the array to the function this is what is confusining me. Hopefully that makes sense. I look forward to a response
 
If the method you create is in the same class as the control you will not need to pass them as arguments to the method.

The controls are declared globally within the scope of the form class. That means any method in that class will have access to them. If you are using VS2003 the controls will be declared in the region that says windows form generated code. If you are using VS2005 they are in a seperate file and uses a new feature called partial classes to spread a class through multiple files. The concept is the same though.

The arguments in the function (such as the button click function) are scoped only to that function. That means that you can only see them while you are within that function.

This article discusses variable scope in Visual Basic. That should help clarify things for you I think.
 
Code:
Public Class Form1

   Friend TextBox1 as New Textbox

   Private Sub Button1_Click.....
          ShowToHer("I love you")
   End Sub

   Private Function ShowToHer(inMessage as String) as Boolean
      Textbox1.Text = "Honey, " & inMessage
   End Function

End Class

is this able to gain ur understanding? hope it helps.
 
Back
Top