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.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
60 Points
108 Posts
Dynamic Array Size
Jun 29, 2018 10:15 AM|KALYANA ALLAM|LINK
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, });
All-Star
53041 Points
23619 Posts
Re: Dynamic Array Size
Jun 29, 2018 10:31 AM|mgebhard|LINK
Use generics, List<int>, rather than an array.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/
Participant
1620 Points
927 Posts
Re: Dynamic Array Size
Jul 01, 2018 10:21 AM|PaulTheSmith|LINK
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)
Member
60 Points
108 Posts
Re: Dynamic Array Size
Jul 01, 2018 04:50 PM|KALYANA ALLAM|LINK
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.
Member
60 Points
108 Posts
Re: Dynamic Array Size
Jul 01, 2018 04:52 PM|KALYANA ALLAM|LINK
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
All-Star
54508 Points
14111 Posts
Re: Dynamic Array Size
Jul 01, 2018 08:44 PM|mudassarkhan|LINK
Make it a List, Add Items and then later convert it to Array.
Blog: ASPSnippets | Forum: ASPForums | Company: Excelasoft
Participant
1620 Points
927 Posts
Re: Dynamic Array Size
Jul 01, 2018 11:06 PM|PaulTheSmith|LINK
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())
Member
740 Points
321 Posts
Re: Dynamic Array Size
Jul 02, 2018 06:48 AM|Abraham Qian|LINK
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.
Result.
Feel free to let me know if you have any question.
Best Regards,
Abraham.
Member
60 Points
108 Posts
Re: Dynamic Array Size
Jul 02, 2018 12:54 PM|KALYANA ALLAM|LINK
Hi Khan,Thanks this idea helped using this I could keep the Array Dynamic
Member
60 Points
108 Posts
Re: Dynamic Array Size
Jul 02, 2018 12:56 PM|KALYANA ALLAM|LINK
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.
Member
60 Points
108 Posts
Re: Dynamic Array Size
Jul 02, 2018 12:57 PM|KALYANA ALLAM|LINK
Thanks Abraham