Hi,
In the code below, an array is filled with variables. However, I would like to reverse the order in which the variables are entered and stored in the array. This code doesn't work. Any suggestions would be greatly appreciated.
diel_pos[0] = 0F;
fillDielectricPosMicrostrip(layersArray, ref diel_pos);
Array.Reverse(diel_pos); //April 23
diel_const[0] = 1;
fillDielectricConst(layersArray, 1, ref diel_const);
Array.Reverse(diel_const); //April 23
Barry OlneyPosted Apr 27, 2023, 1:00 AM
Thanks Naimish, that helps. I did want to keep the first element of the array unchanged.
Naimish MakwanaPosted Apr 26, 2023, 5:22 AM
The code you provided seems to be incomplete, as the implementation of the
fillDielectricPosMicrostripandfillDielectricConstmethods are not shown. However, if you want to reverse the order of the elements in thediel_posanddiel_constarrays, theArray.Reversemethod you're already using is the correct way to do it.Based on your code, it seems like you're already calling
Array.Reverseon both arrays. However, keep in mind that this will reverse the order of all elements in the array, including the first element, which you've already assigned a specific value to (diel_pos[0] = 0F; diel_const[0] = 1;).If you want to reverse the order of the elements in the array while keeping the first element unchanged, you can modify your code as follows:
Here, we're passing additional parameters to the
Array.Reversemethod to indicate that we only want to reverse the elements starting from the second element (1) and up to the end of the array (diel_pos.Length - 1). This way, the first element of the array will remain unchanged.Note that if the length of the array is odd, the middle element will be swapped with itself when reversing the array.