Passing multipule variable types to a function

Fion

Member
Joined
Mar 16, 2006
Messages
9
Programming Experience
Beginner
Hi all,
I have a quick question, that is pretty simple, and I'm hopeing that someone else knows how to go about doing this.
First off I am using Visual Basic in Visual Studio 2005.
Now I have a function which I pass a combobox to, and it then fills the combobox with the correct values. This all works correctly. NOW I have a toolstripcombo box also, that I would like to do the exact same thing for, except the function will not accept a toolstripcombobox instead of a normal combobox being passed to it. Does anyone know how I can get that to work? Ideally, something similar in function to:
Private Function Fillfield(ByVal B As ComboBox or ToolStripComboBox)
(I know the above does not work, I'm just attempting to show HOW I would like it to work..)
Any help would be greatly appreciated!
 
What you need is an overload.... a function that accepts multiple parameter types....
VB.NET:
PrivateFunction Fillfield(ByVal B As ComboBox )
'Do your filling
End Function

PrivateFunction Fillfield(ByVal B As ToolStripComboBox)
'Do your filling
End Function

A couple of things though.... unless it actualy returns a value, make them subs. Also, it's pointless to pass the controls byval since they will be passed byref (which is what you really want anyways.)

-tg
 
So what you are saying is to make two seperate functions (or subs)? One for the combobox and one for the toolstripcombobox? Or did I read that wrong? I have to confess, I don't actually know what an overload is or how it works.
 
No, you got that right....
In a nutshell an over loaded function/sub is any time you have multiple copies of the same function (in name only) that has a different signature - sometimes it is the number of parameters, or different parameters types. It allows you to perform different actions (if you want) based on the types of the parameters passed in.

-tg
 
There are two other ways that can be used, without you needing to code the same method twice to have the same thing done for these two objects.

The first one is intendedly wrong, I suppose, that is, the documentation says the ObjectCollection class is not intended to be used, but it works nevertheless since Items property of both these combos is of this same type:
VB.NET:
Sub PassObjectCollection()
  fillcombo(ComboBox1.Items)
  fillcombo(ToolStripComboBox1.Items)
End Sub
 
Sub fillcombo(ByRef items As System.Windows.Forms.ComboBox.ObjectCollection)
  Dim ary(2) As String
  ary(0) = "A"
  ary(1) = "B"
  ary(2) = "C"
  items.AddRange(ary)
End Sub
The other is to make a function that returns an array of the objects you want to fill into these combos.
VB.NET:
Sub FillFromArray()
  ComboBox1.Items.AddRange(objectarray)
  ToolStripComboBox1.Items.AddRange(objectarray)
End Sub
 
Function objectarray() As Object()
  Dim ary(2) As String
  ary(0) = "A"
  ary(1) = "B"
  ary(2) = "C"
  Return ary
End Function
 
Last edited:
Back
Top