Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Function Accepting Parameters of Any Type

Function Accepting Parameters of Any Type

Scheduled Pinned Locked Moved C#
helpdata-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    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; i

    public 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.

    P L 2 Replies Last reply
    0
    • A ASPnoob

      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; i

      public 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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • A ASPnoob

        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; i

        public 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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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[^]

        1 Reply Last reply
        0
        • P Pete OHanlon

          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

          D Offline
          D Offline
          Dave Doknjas
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups