Function Accepting Parameters of Any Type
-
Hi all, I would like to write a function of type Bool that checks an array for a certain value or a function of type Void to simply sort an array. If I write a function for only a particular array type, the function is pretty straight forward but inflexible. However, if I want the function to work on any kind of array regardless of it's type, it is pretty tricky. I have tried to do this by using a For Loop in the function. Then I tried to pass in the name of the array and it's length as parameters but got stuck. The problem is if I pass in the name of an array as a parameter, that parameter must have a type. Then I tried to use the ForEach Loop where I can enter the array name but it seems it also needs to know what array type it is dealing with. The following are what I have tried:
public bool myFunction(ARRAYTYPE myArrayName, int myArrayLength)
{
for(int i = 0; ipublic void myFunction(ARRAYTYPE myArrayName )
{
foreach (ARRAYTYPE value in myArrayName)
{
//Sort
}
}I'm beginning to think this can't be done but I'm really hoping someone has a solution. Thanks in advance for your reply.
-
Hi all, I would like to write a function of type Bool that checks an array for a certain value or a function of type Void to simply sort an array. If I write a function for only a particular array type, the function is pretty straight forward but inflexible. However, if I want the function to work on any kind of array regardless of it's type, it is pretty tricky. I have tried to do this by using a For Loop in the function. Then I tried to pass in the name of the array and it's length as parameters but got stuck. The problem is if I pass in the name of an array as a parameter, that parameter must have a type. Then I tried to use the ForEach Loop where I can enter the array name but it seems it also needs to know what array type it is dealing with. The following are what I have tried:
public bool myFunction(ARRAYTYPE myArrayName, int myArrayLength)
{
for(int i = 0; ipublic void myFunction(ARRAYTYPE myArrayName )
{
foreach (ARRAYTYPE value in myArrayName)
{
//Sort
}
}I'm beginning to think this can't be done but I'm really hoping someone has a solution. Thanks in advance for your reply.
The examples you post aren't really suitable for handling in a single method because the internal implementations are too different. In other words, they don't have common behaviour, so you shouldn't try to treat them as though they do. On a general note, you can use generics to pass in a type to a method when you want to be able to use different types, decided at compile time. To do this, you would declare it like this (changing the array to a generic list instead):
public void MyFunction<T>(List<T> myCollection){
}*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Hi all, I would like to write a function of type Bool that checks an array for a certain value or a function of type Void to simply sort an array. If I write a function for only a particular array type, the function is pretty straight forward but inflexible. However, if I want the function to work on any kind of array regardless of it's type, it is pretty tricky. I have tried to do this by using a For Loop in the function. Then I tried to pass in the name of the array and it's length as parameters but got stuck. The problem is if I pass in the name of an array as a parameter, that parameter must have a type. Then I tried to use the ForEach Loop where I can enter the array name but it seems it also needs to know what array type it is dealing with. The following are what I have tried:
public bool myFunction(ARRAYTYPE myArrayName, int myArrayLength)
{
for(int i = 0; ipublic void myFunction(ARRAYTYPE myArrayName )
{
foreach (ARRAYTYPE value in myArrayName)
{
//Sort
}
}I'm beginning to think this can't be done but I'm really hoping someone has a solution. Thanks in advance for your reply.
ASPnoob wrote:
I would like to write a function of type Bool that checks an array for a certain value or a function of type Void to simply sort an array
Try the recommended approach; IComparable[^].
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]
-
The examples you post aren't really suitable for handling in a single method because the internal implementations are too different. In other words, they don't have common behaviour, so you shouldn't try to treat them as though they do. On a general note, you can use generics to pass in a type to a method when you want to be able to use different types, decided at compile time. To do this, you would declare it like this (changing the array to a generic list instead):
public void MyFunction<T>(List<T> myCollection){
}*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
Note that you can also do this with arrays:
public void MyFunction(T[] myArray){
}Dave Doknjas Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com Instant C# - VB to C# Converter Instant VB - C# to VB Converter