locked
Optional Object parameter passing in a function RRS feed

  • Question

  • Hi,

    I have this code. I want to use the same procedure with the controls on a form instead of just controls on a groupbox. The catch is i do not want to repeat the procedure. I tried to declare grpbox as optional but it will ask for the default value and I don't know what i can give as default.
    Any other recommendations to make this function precise would also be welcomed.

    Thanks

    Public
    Sub checkCriteria(ByVal GrpBox As GroupBox)

    Dim grp As GroupBox = CType(GrpBox, GroupBox)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In grp.Controls

    If TypeOf ctrl Is TextBox Then

    Dim sctrl As TextBox = CType(ctrl, TextBox)

    Try

    If sctrl.Text <= txtasUMin Or sctrl.Text >= txtasUMax Then

    sctrl.BackColor = Color.OrangeRed

    sctrl.ReadOnly = False

    flag = True

    End If

    Catch e As InvalidCastException

    sctrl.BackColor = Color.OrangeRed

    sctrl.ReadOnly = False

    flag = True

    End Try

    End If

    Next

    If flag = True Then

    MsgBox(txtMsg, MsgBoxStyle.Critical, "Message centre")

    Else

    settxtasUMin()

    MsgBox("Data validation completed")

    End If

    End Sub

    Monday, July 17, 2006 3:23 PM

Answers

  • If IsNothing(grpbox) then

    ...DoSomething

    End If

     

    Examples of Overloading:

    Public Sub checkCriteria(ByVal GrpBox As GroupBox)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In GrpBox.Controls

    ********

    Public Sub checkCriteria(ByVal frm As Form)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In frm.Controls

    *******

    Public Sub checkCriteria(ByVal pnl As Panel)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In pnl.Controls

     

    *******

    Public Sub checkCriteria(ByVal Obj As Object)

    If TypeOf Obj Is GroupBox Then

    Dim grpbox As GroupBox = CType(Obj, GroupBox)

    End If

    If TypeOf Obj Is Panel Then

    Dim pnl As Panel = CType(Obj, Panel)

    End If

    ...

    End Sub

     

    Monday, July 17, 2006 3:59 PM
    Moderator

All replies

  • Optional GrpBox As GroupBox = Nothing
     
    Or you can also do an overload where the procedure accepts any container object...or an overload for each type of container you would like to work with
     
    HTH
    Monday, July 17, 2006 3:46 PM
    Moderator
  • Hi,

    The Nothing value is declaring but how should i check the value nothing in the If statement?
    i don't really understand how can i do overload. Can you elaborate on this point plz.

    Thanks

    Monday, July 17, 2006 3:49 PM
  • If IsNothing(grpbox) then

    ...DoSomething

    End If

     

    Examples of Overloading:

    Public Sub checkCriteria(ByVal GrpBox As GroupBox)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In GrpBox.Controls

    ********

    Public Sub checkCriteria(ByVal frm As Form)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In frm.Controls

    *******

    Public Sub checkCriteria(ByVal pnl As Panel)

    Dim ctrl As Control

    Dim flag As Boolean

    flag = False

    For Each ctrl In pnl.Controls

     

    *******

    Public Sub checkCriteria(ByVal Obj As Object)

    If TypeOf Obj Is GroupBox Then

    Dim grpbox As GroupBox = CType(Obj, GroupBox)

    End If

    If TypeOf Obj Is Panel Then

    Dim pnl As Panel = CType(Obj, Panel)

    End If

    ...

    End Sub

     

    Monday, July 17, 2006 3:59 PM
    Moderator
  • Thanks Dman1, really appreciate it.

     

    Tuesday, July 18, 2006 8:06 AM