locked
Dynamic Array Size RRS feed

  • Question

  • User1203305613 posted

    I have the below code where I pass an array.

    Filters.Add("[dbo].[Employees].[EmployeeID]", new int[5] { 1, 2, 3, 4, 5 });

    How do I make it dynamic ,say onetime it should be 

    Filters.Add("[dbo].[Employees].[EmployeeID]", new int[6] { 1, 2, 3, 4, 5,6 });

    Next time it could be 

    Filters.Add("[dbo].[Employees].[EmployeeID]", new int[4] { 1, 2, 3, 4, });

    Friday, June 29, 2018 10:15 AM

Answers

  • User475983607 posted

    Use generics, List<int>, rather than an array.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 29, 2018 10:31 AM
  • User303363814 posted

    You have a problem with the code you are calling but you don't show the code with the problem!  What does the called method look like?

    void Add(string x, int[] numbers)

    (Mind you, I agree with the previous answer.  Don't use arrays - they are very old fashioned)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 1, 2018 10:21 AM
  • User-1171043462 posted

    Make it a List, Add Items and then later convert it to Array.

    List<int> lst = new List<int>();
    lst.Add(1);
    lst.Add(2);
    lst.Add(3);
    int[] arr = lst.ToArray();

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 1, 2018 8:44 PM
  • User-330142929 posted

    Hi KALYANA ALLAM,

    According to your description, I suggest you could refer the following code snippets. Please pay attention to my comments in the code.

        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(34);
            list.Add(66);
            list.Add(343);
    //Dynamic Array? is this the effect you want.
            Console.WriteLine(Sum(list.ToArray()));
            Console.ReadKey();
        }
    // Functions we can't change
        protected static int Sum(int[] array)
        {
            return array.Sum(x => x);
    }

    Result.

    Feel free to let me know if you have any question.

    Best Regards,

    Abraham.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 2, 2018 6:48 AM

All replies

  • User475983607 posted

    Use generics, List<int>, rather than an array.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 29, 2018 10:31 AM
  • User303363814 posted

    You have a problem with the code you are calling but you don't show the code with the problem!  What does the called method look like?

    void Add(string x, int[] numbers)

    (Mind you, I agree with the previous answer.  Don't use arrays - they are very old fashioned)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 1, 2018 10:21 AM
  • User1203305613 posted

    Thanks for your suggestion 

    The code Is from Product I need to pass an array ,I cannot alter their product code   . But I want to make the size of array of dynamic.

    Filters.Add("[dbo].[Employees].[EmployeeID]", new int[5] { 1, 2, 3, 4, 5 });.

    Its like Filter.Add("Column Name",Array);

    I want to make the Array dynamic. 

     

    Sunday, July 1, 2018 4:50 PM
  • User1203305613 posted

    The method is from a Product,its a call to API which I don't have control

    The code Is from Product I need to pass an array ,I cannot alter their product code   . But I want to make the size of array of dynamic.

    Filters.Add("[dbo].[Employees].[EmployeeID]", new int[5] { 1, 2, 3, 4, 5 });.

    Its like Filter.Add("Column Name",Array);

    I want to make the Array dynamic. 

    Thanks for your  suggestion,will keep in mind

     

    Sunday, July 1, 2018 4:52 PM
  • User-1171043462 posted

    Make it a List, Add Items and then later convert it to Array.

    List<int> lst = new List<int>();
    lst.Add(1);
    lst.Add(2);
    lst.Add(3);
    int[] arr = lst.ToArray();

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 1, 2018 8:44 PM
  • User303363814 posted

    Are you saying that you cannot change the Filters.Add() method?

    If that method is expecting a fixed sized array then it will not work unless you pass a fixed sized array.

    You still haven't shown us the signature of the method you are trying to call (filters.Add())

    Sunday, July 1, 2018 11:06 PM
  • User-330142929 posted

    Hi KALYANA ALLAM,

    According to your description, I suggest you could refer the following code snippets. Please pay attention to my comments in the code.

        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(34);
            list.Add(66);
            list.Add(343);
    //Dynamic Array? is this the effect you want.
            Console.WriteLine(Sum(list.ToArray()));
            Console.ReadKey();
        }
    // Functions we can't change
        protected static int Sum(int[] array)
        {
            return array.Sum(x => x);
    }

    Result.

    Feel free to let me know if you have any question.

    Best Regards,

    Abraham.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 2, 2018 6:48 AM
  • User1203305613 posted

    Hi Khan,Thanks this idea helped using this I could keep the Array Dynamic 

    Monday, July 2, 2018 12:54 PM
  • User1203305613 posted

    Yes Paul ,I don't see the code inside method , but team confirmed the array to be dynamic. 

    It got resolved now thanks for your time on this. 

    Monday, July 2, 2018 12:56 PM
  • User1203305613 posted

    Thanks Abraham

    Monday, July 2, 2018 12:57 PM