[RESOLVED] function parameter predefined options
Hi,
I created a function as follow
vb Code:
Function FindCapital(ByVal CountryCode As Integer, ByVal CountryName As String) As String
End Function
And later on when i call the function I want the function to be able to show all the available CountryCode and CountryName values, so I can select the one I want. I'm not sure how I can do that or if it's possible.
ex :
Code:
FindCapital(US
Canada
Mexico
Thanks
Re: function parameter predefined options
Where are you storing your CountryCodes and CountryNames ?
Re: function parameter predefined options
For now let's say I only have 4 entries for each parameters , And I have the 4 entries CountryCodes and CountryNames on 8 different string values
dim a as string = "canada"
...
thanks
Re: function parameter predefined options
OK so you should probably be using a class, something like this - >
Code:
Public Class Countries
Private _name As String
Private _code As String
Private _capital As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Code() As String
Get
Return _code
End Get
Set(ByVal value As String)
_code = value
End Set
End Property
Public Property Capital() As String
Get
Return _capital
End Get
Set(ByVal value As String)
_capital = value
End Set
End Property
End Class
Then you can do this -
n.b. - personally i would be pulling my country data form a database but here i will just add a couple manually
Code:
Dim CountryList As New List(Of Countries)
' put this as a form / class level variable
'Load the country info
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim _Countries As New Countries
_Countries.Name = "USA"
_Countries.Code = "0001"
_Countries.Capital = "Washington DC"
CountryList.Add(_Countries)
_Countries = New Countries
_Countries.Name = "Canada"
_Countries.Code = "0002"
_Countries.Capital = "Ottawa"
CountryList.Add(_Countries)
End Sub
'Then the FindCapital function
Code:
Function FindCapital(ByVal CountryCode As Integer, ByVal CountryName As String) As List(Of Countries)
Dim selectedValues As List(Of Countries)
selectedValues = CountryList.FindAll(Function(p) p.Name = CountryName And p.Code = CountryCode)
Return selectedValues
End Function
and you can then use your list like this to return either a Capital OR a list of countries and country information -
Code:
Sub TestGetCapital()
Dim CountryCapitals As List(Of Countries)
Dim Capital As String
CountryCapitals = FindCapital("0001", "USA")
Capital = CountryCapitals(0).Capital
End Sub
Sub TestGetAllCountries()
Dim item As Countries
Dim name As String
Dim capital As String
Dim code As String
For Each item In CountryList
name = item.Name
capital = item.Capital
code = item.Code
'do stuff with values
Next
End Sub
Obviously these are just examples so i have used strings to hold the returned data, but you could put into a list view or grid or text-boxes or whatever you want really.
Re: function parameter predefined options
is your issue resolved now met0555 ?
Re: function parameter predefined options
@NeedSomeAnswers sorry for the delay got caught with some other project.
Thanks for your code, this was the exact functionality I was looking for. To be honest I was expecting to achieve this only 1 or 2 lines pf code lol... But as long as it works i'm fine with your code
thanks