Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a Method that creates a new List and takes additional parameters
    primarykey
    data
    text
    <p>Is there a way to create a <code>Method</code> that creates a new instance of a <code>List</code> as a <code>Method Parameter</code>and does something with the new <code>List</code>?</p> <pre><code> private void ApplyMaxValue(List&lt;double&gt; list, double myDouble, Border border) { list = new List&lt;double&gt;(); list.Add(myDouble * 8); border.Width = list.Max(); } </code></pre> <p>I would like to be able to pass values like this where <code>someListToCreate</code> is created in the <code>Method</code> rather than using an existing list. </p> <pre><code> ApplyMaxValue(someListToCreate, aDouble, myBorder); </code></pre> <p>I am still a little new to C# and not sure if <code>Methods</code> are capable of this or if I would need to create a <code>Class</code>.</p> <p><strong>EDIT</strong></p> <p>Code with demonstration of Method</p> <pre><code>&lt;Window x:Class="NewListSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="500" Width="525"&gt; &lt;Grid&gt; &lt;StackPanel Margin="20"&gt; &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Bottom"&gt; &lt;Border x:Name="borderForArray1" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FFF57F7F" VerticalAlignment="Bottom"/&gt; &lt;Border x:Name="borderForArray2" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FF6FA8D6" VerticalAlignment="Bottom"/&gt; &lt;Border x:Name="borderForArray3" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FFEEB382" VerticalAlignment="Bottom"/&gt; &lt;Border x:Name="borderForArray4" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FFB171E6" VerticalAlignment="Bottom"/&gt; &lt;/StackPanel&gt; &lt;Button Content="Generate" HorizontalAlignment="Left" Padding="8,3" Margin="0,5,0,0" Click="Button_Click"/&gt; &lt;Button Content="Method" HorizontalAlignment="Left" Padding="8,3" Margin="0,5,0,0" Click="Button_Click_1" /&gt; &lt;Button Content="Clear" HorizontalAlignment="Left" Padding="8,3" Margin="0,5,0,0" Click="Button_Click_2"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>CS</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace NewListSample { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } string[] array1 = { "This is a word.", "This is a sentence.", "This is short.", "This is." }; string[] array2 = { "This is a word.", "This is a very very very very long sentence.", "This is the longest sentence in this array along with all other arrays in this sample.", "This is a string." }; string[] array3 = { "This.", "That.", "Those.", "These.", "Slightly longer string." }; string[] array4 = { "a.", "b.", "c.", "defg." }; //Prevent from writing this code List&lt;double&gt; list1 = new List&lt;double&gt;(); List&lt;double&gt; list2 = new List&lt;double&gt;(); List&lt;double&gt; list3 = new List&lt;double&gt;(); List&lt;double&gt; list4 = new List&lt;double&gt;(); //Prevent code end //Method to prevent writing longer code private void ApplyMaxValue(string[] array, Border border) { List&lt;double&gt; someList = new List&lt;double&gt;(); foreach (string s in array) { someList.Add(s.Length * 3); } border.Height = someList.Max(); } private void Button_Click(object sender, RoutedEventArgs e) { foreach (string s in array1) { list1.Add(s.Length * 3); } foreach (string s in array2) { list2.Add(s.Length * 3); } foreach (string s in array3) { list3.Add(s.Length * 3); } foreach (string s in array4) { list4.Add(s.Length * 3); } borderForArray1.Height = list1.Max(); borderForArray2.Height = list2.Max(); borderForArray3.Height = list3.Max(); borderForArray4.Height = list4.Max(); } private void Button_Click_1(object sender, RoutedEventArgs e) { ApplyMaxValue(array1, borderForArray1); ApplyMaxValue(array2, borderForArray2); ApplyMaxValue(array3, borderForArray3); ApplyMaxValue(array4, borderForArray4); } private void Button_Click_2(object sender, RoutedEventArgs e) { borderForArray1.Height = borderForArray2.Height = borderForArray3.Height = borderForArray4.Height = 1; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload