FSharpList<FSharpList<int>> newImageList;
FSharpList<int> row;
for(int i = 0; i < CurrentImage.Header.Height)
{
row = PPMImageLibrary.GrayscaleImage(CurrentImage.ImageListData);
newImageList.Head = row;
}
Above I'm trying to take a int list list and set each index to row which is a int list. Obviously I can't do it with .Head, and if I could it would only change the first index. I'm wondering how I could possibly make this work, I'm having a hard time getting any index of newImageList in the first place.
FSharpList is an immutable list. Therefore you cannot assign its Head and Tail properties something. However, you can try adding your FSharp list into a generic C# List or any collection that inherits an IEnumerable. For example from your code:
List<int> newImageList = new List<int>();
for(int i = 0; i < CurrentImage.Header.Height)
{
newImageList.AddRange(PPMImageLibrary.GrayscaleImage(CurrentImage.ImageListData)); // I am assuming your GrayscaleImage method might return multiple records.
}
I hope this helps.
Related
I have 2 observablecollection type list in which one list already contains data and in second list I am adding data from first list. But when I shuffle data in second list it also shuffles the data in first list.
First I tried to add data manually by using foreach loop
foreach(var dat in list1){
list2.add(new model(){
name=dat.name
});
}
then used this method to copy data.
list2 = new ObservableCollection<Model>(list1);
but nothing works for me
Here is my code which I am currently using
list1 and 2 are property of
ObservableCollection<Model> type
list2 = new ObservableCollection<Model>(list1);
to shuffle data in list2
var templist = list2.Select(c => c).ToList();
templist.Shuffler();
int j = 0;
for (int i = 0; i < 5; i++)
{
list2[i].name = templist[j].name;
list2[i].Id = i + 1;
j++;
}
after this code the data in list1 is also shuffled.
What I want is whenever I shuffle data it should not affect data in my main list i.e. list1
Thanks to #jdweng for letting me know about Clone.
To make it work Correctly I need to implement ICloneable interface in my model.
Here is the good example of ICloneable implementation
https://www.c-sharpcorner.com/UploadFile/dacca2/important-interface-in-net-work-with-icloneable-interface/
Then I have used
list2 = new ObservableCollection<Model>(list1.Select(c=>(Model)c.Clone()));
and it worked without any problem
So I have a particular issue.
I have a standard 2D array (non-jagged) and a string array. They are linked lets just say via the 1st column of the 2D array.
So that means I would like to sort them together (its not a key based system so I can't use the columns as keys into the string array, just when ever a row in the 2D array moves so too shall the value in equivalent row of the string array move).
Not sure what is the best solution here. The dirty method that I have tried and works is standard nested loops to sort via the first column and move everything accordingly.
I just want to know if there is a better solution than this, perhaps using Linq and things like that??
TLDR:
private (int[,], string[]) SortByColumnNames(int[,] array, string[] columnNames)
{
var columnNameToIndex = columnNames.Select((c, index) => Tuple.Create(c, index));
var sortedColumns = columnNameToIndex.OrderBy(ci => ci.Item1, StringComparer.OrdinalIgnoreCase).ToList();
var sortedArray = new int[2, 3];
for (var newColumnIndex = 0; newColumnIndex < sortedColumns.Count; newColumnIndex++)
{
var oldColumnIndex = sortedColumns[newColumnIndex].Item2;
for (var rowIndex = 0; rowIndex < array.GetLength(1); rowIndex++)
{
sortedArray[newColumnIndex, rowIndex] = array[oldColumnIndex, rowIndex];
}
}
var resultColumnNames = sortedColumns.Select(ci => ci.Item1).ToArray();
return (sortedArray, resultColumnNames);
}
Idea:
Create list of structures: column name + column index
Order this array by column name
New list will have ordered column names and old column indexes
Create new structure and fill it with the following logic: row should be the same, however column should be new.
In the other words:
We remember column indexes
Sort columns (and don't forget indexes)
Create map: from old index to new
Create new dictionary where sortedArray[newColumnIndex, rowIndex] = array[oldColumnIndex, rowIndex]
I have a ListView named sandwichToppings which displays various sandwich toppings and allows the user to select multiple. In my controller code, I must capture the selected toppings by their index in the ListView, and send those indices back using an array.
The code which causes me to stumble is visible below. I have not been able to figure out the part which captures one or more toppings to the sandwich (the rest works fine).
void readSandwichSelection()
{
int[] toppings = null;
// One or many toppings were added to the sandwich.
if(toppingsAvailable.SelectedItems.Count > 0)
{
toppings = new int[toppingsAvailable.SelectedItems.Count];
int toppingIndex = 0;
for(int i = 0; i < toppingsAvailable.Items.Count; i++)
{
ListViewItem test = (ListViewItem)toppingsAvailable.Items.ElementAt(i);
if(test.IsSelected == true)
{
toppings[toppingIndex] = i;
toppingIndex++;
}
}
}
// No sandwich topping.
else
{
order.addSandwich(sandwichesAvailable.SelectedIndex + 1);
}
}
Along my journey, I have tried a couple solutions found on Telerik. The first:
foreach (ListViewDataItem item in radListView1.SelectedItems)
{
int example = radListView1.Items.IndexOf(item);
}
The above example fails to work because
ListViewDataItem does not exist.
When replaced by ListViewItem, a run-time error occurs: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'Windows.UI.Xaml.Controls.ListViewItem'.'
The second attempted solution:
for (int i = 0; i < radListView1.Items.Count; i++)
{
if (this.radListView1.Items[i].Selected)
{
int example = i;
}
}
This solution cannot work because the .Selected property simply does not exist.
In all my hour of attempted work, I come up with either of these two problems. Either some form of cast exception occurs, or it is impossible to reach the property.
In my example above, I did have success in reaching the isSelected property by making a copy of each list item, and testing whether it had been selected. However, although Visual Studio lets me make the assignment
ListViewItem test = (ListViewItem)toppingsAvailable.Items.ElementAt(i);
this statement cannot run at compile time, causing an error as written previously. Which datatype other than ListViewItem must be used to count over list view items?
toppingsAvailable.SelectedItems will return a List<> of selected items. However, how could I know from this list which list index selected items belong to?
You can try the following code to get the selected toppings's indexes and put the indexes in an array. The indexes will be saved in the ToppingArray.
void readSandwichSelection()
{
int[] ToppingArray = new int[toppingsAvailable.SelectedItems.Count];
for (int i = 0; i < toppingsAvailable.SelectedItems.Count; i++)
{
var selectedIndex = toppingsAvailable.Items.IndexOf(toppingsAvailable.SelectedItems[i]);
ToppingArray[i] = selectedIndex;
}
}
Please try the following. It is Selected rather than IsSelected.
for (int i = 0; i < toppingsAvailable.Items.Count; i++)
{
var test = toppingsAvailable.Items[i];
if (test.Selected)
{
toppings[toppingIndex] = i;
toppingIndex++;
}
}
how to delete from listbox ?
deleting many indexes at the same time
Note that im using list of car types with their details
the objects in the listBox have car type cost rate
Update: If you want to remove all selected items as commented:
foreach (int i in listBox1.SelectedIndices)
listBox1.Items.RemoveAt(i);
If you want to delete all items instead, use Clear:
listBox1.Items.Clear();
if you want to remove at a specific index, use RemoveAt:
listBox1.Items.RemoveAt(0);
or in a loop:
for(int i = 0; i < listBox1.Items.Count; i++)
listBox1.Items.RemoveAt();
if you want to remove a speicific item, use Remove:
Car car = (Car) listBox1.Items[0];
listBox1.Items.Remove(car);
You have to use loop.
Something like this:
List<int> indexesToDelete = new List<int>();
// add items you want to remove to List like this:
indexesToDelete.Add(1);
indexesToDelete.Add(2);
indexesToDelete.Add(4);
// loop will execute code inside inside for all items added to list
foreach (int indexToDelete in indexesToDelete)
{
listbox1.RemoveAt(indexToDelete);
}
Edit: itemsToDelete renamed to indexesToDelete in code.
I am iterating through all the list in mainlist and if a specific condition occur. I want to remove that specific list.
It is not working.
for (int i = 0; i < mainList.Count; i++)
{
if (mainList[i].Dead == true) //
{
mainList.removeAt(i);//what will be affect on mainList.Count;
}
}
mainList.RemoveAll(x => x.Dead);
The way you constructed your for loop, it will only work if mainList.Count stays constant, but you are removing elements during your loop. Do you understand why that is a problem? Whenever you remove an element you end up skipping the check of the element after it.
I think you could use foreach and mainList.remove but I'm not sure. Another solution is to just make your loop count downwards:
for (int i = mainList.Count-1; i >= 0; i--)
{
if (mainList[i].Dead == true)
{
mainList.removeAt(i);
}
}
You will not be able to do that since you are altering the length of the list when you are doing so. I would do it like this instead:
var newList = mainList.Where(y => !y.Dead).ToList();
How to modify or delete items from an enumerable collection while iterating through it in C#
http://bytes.com/topic/c-sharp/answers/238241-how-do-you-modify-array-while-iterating-through