c# dynamically modifying list - c#

I don't understand how to do it with foreach...
The goal is to modify a list each time we change Num.
Is the way with a Canvas List and a working List is ok for nice coding?
class Program
{
static void Main(string[] args)
{
int i_Num = 0;
string Str_Num = "";
string[] linkToPLC = {"toto[{0}].test{1}", "tata[{0}].test{1}", "titi[{0}].test{1}"};
List<string> genlnkPLCCanvas = new List<string>(linkToPLC);
List<string> genlnkPLCworkingwith = new List<string>(linkToPLC);
Console.WriteLine("Insert Num: ");
Str_Num = Console.ReadLine();
i_Num = Convert.ToInt32(Str_Num);
for (int item = 0; item < genlnkPLCCanvas.Count; item++)
{
genlnkPLCworkingwith[item] = String.Format(genlnkPLCworkingwith[item], i_Num, 200);
Console.WriteLine("with List: result= " + genlnkPLCworkingwith[item]);
}
//foreach (string item in genlnkPLCCanvas) genlnkPLCworkingwith[item] = String.Format(item, i_Num, 200);
Console.ReadKey();
}
}

If you want to modify the existing list, you have to use for loop instead of foreach one:
foreach (var item in list) ...
should be changed into
for (int i = 0; i < list.Count; ++i) {
var item = list[i]; // not necessary, but often convenient
...
list[i] = ... // modification
...
}
For instance
for (int i = 0; i < genlnkPLCCanvas.Count; ++i) {
var item = genlnkPLCCanvas[i];
genlnkPLCCanvas[i] = string.Format(item, i_StationNum, 200);
}
When testing try creating reports (put all the logic into the single readable query) and then printing them out in one go:
...
var withListReport = genlnkPLC
.Select(item => "with List: result = " + string.Format(item, i_StationNum, 200));
var withoutListReport = genlnkPLC
.Select(item => "without List: result = " + string.Format(item, i_StationNum, 200));
// now you can do whatever you want with the reports:
// - print them to console
// Console.WriteLine(string.Join(Envrironment.NewLine, withListReport));
// - save to file:
// File.WriteAllLines(#"C:\MyFile.txt", withListReport);
// - print to, say, WinForm UI:
// MyTextBox.Text = string.Join(Envrironment.NewLine, withListReport);
Console.WriteLine(string.Join(Envrironment.NewLine, withListReport));
Console.WriteLine(string.Join(Envrironment.NewLine, withoutListReport));
Console.ReadKey();

String.Format() returns a string, it doesn't change whatever you're formatting. Therefore, your first foreach (var item in genlnkPLC) creates temporary strings that are immediately destroyed.
foreach (var item in genlnkPLC)
{
Console.WriteLine("with List = " + String.Format(item, i_StationNum, 200));
}

In the statement
foreach (var item in genlnkPLC)
Console.WriteLine("with List: result= "+item);
you are not using String.Format to insert arguments into the members of genlnkPLC, which are apparently intended as format strings. You can use
foreach (var item in genlnkPLC)
Console.WriteLine("without List result = " + String.Format(item, i_StationNum, 200));
instead.

The problem is that you can't change the reference of the elements enumerated in a foreach loop. string is an inmutable object, so changing it replaces the old reference with a new one. If you want to change the elements in the list, you'll need to do it in a for loop, like this:
for (int item = 0; item < genlnkPLC.Count; item++)
genlnkPLC[item]= String.Format(genlnkPLC[item], i_StationNum, 200);

No need to repeat foreach (the first one, did nothing to your item). Try this:
foreach (var item in genlnkPLC)
Console.WriteLine("with List: result= "+ String.Format(item, i_StationNum, 200));

As M.Bychenko says: "If you want to modify the existing list, you have to use for loop instead of foreach one:"
And thanks for the report tipp!
class Program
{
static void Main(string[] args)
{
int i_Num = 0;
string Str_Num = "";
string[] linkToPLC = {"toto[{0}].test{1}", "tata[{0}].test{1}", "titi[{0}].test{1}"};
List<string> genlnkPLCCanvas = new List<string>(linkToPLC);
List<string> genlnkPLCworkingwith = new List<string>(linkToPLC);
Console.WriteLine("Insert Num: ");
Str_Num = Console.ReadLine();
i_Num = Convert.ToInt32(Str_Num);
for (int item = 0; item < genlnkPLCCanvas.Count; item++)
{
genlnkPLCworkingwith[item] = String.Format(genlnkPLCCanvas[item], i_Num, 200);
}
var CanvasListReport = genlnkPLCCanvas.Select(item => "Canvas List = " + item);
var WorkingListReport = genlnkPLCworkingwith.Select(item => "Working list = " + item);//string.Format(item, i_Num, 200));
// now you can do whatever you want with the reports:
// - print them to console
// Console.WriteLine(string.Join(Envrironment.NewLine, withListReport));
// - save to file: File.WriteAllLines(#"C:\MyFile.txt", withListReport);
// - print to, say, WinForm UI:
// MyTextBox.Text = string.Join(Envrironment.NewLine, withListReport)
Console.WriteLine(string.Join(Environment.NewLine, CanvasListReport));
Console.WriteLine(string.Join(Environment.NewLine, WorkingListReport));
Console.ReadKey();
}
}

This is because
String.Format(item, i_StationNum, 200)
doesn't change the string in the list.
You have to assign the String.Format result to your item.

Related

Formatting List of String

I have an array of strings. I need to sort the list and save each letter's item in a single line. After this, I need to find the longest line of string.
I have done the first part in an inefficient way but I am trying to make it concise.
List<string> fruits = new List<string>
{
"Pomme",
"Apple",
"Apricots",
"Avocado",
"Banana",
"Blackberries",
"Blackcurrant",
"Blueberries",
"Cherries",
"Clementine",
"Cranberries",
"Custard-Apple",
"Durian",
"Elderberries",
"Feijoa",
"Figs",
"Gooseberries",
"Grapefruit",
"Grapes",
"Guava",
"Breadfruit",
"Cantaloupe",
"Carambola",
"Cherimoya",
};
fruits.Sort();
List<string> sortedString = new List<string> { };
foreach (var str in fruits)
{
sortedString.Add(str);
}
//string A, B, C, D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S;
var A = "";
var B = "";
var C = "";
var D = "";
var E = "";
var F = "";
var G = "";
foreach (var item in sortedString)
{
if (item.StartsWith('A'))
{
A += item;
}
else if (item.StartsWith('B'))
{
B += item;
}
else if (item.StartsWith('C'))
{
C += item;
}
else if (item.StartsWith('D'))
{
D += item;
}
else if (item.StartsWith('E'))
{
E += item;
}
else if (item.StartsWith('F'))
{
F += item;
}
}
The result will be like -
AppleApricotsAvocado
BananaBlackberriesBlackcurrantBlueberriesBreadfruit
CantaloupeCarambolaCherimoyaCherriesClementineCranberriesCustard-Apple
Durian
Elderberries
FeijoaFigs
GooseberriesGrapefruitGrapesGuava
After this, I need to find the longest line and put space between each item. Without effective looping, the code will be messy. Can you assist me to show the right way to solve the problem?
The Sort() method already sorts your list and you don't need to assign it to a new one.
My proposal to resolve your problem is
fruits.Sort();
var result = fruits.GroupBy(f => f[0]);
int[] lineslength = new int[result.Count()];
int index = 0;
foreach (var group in result)
{
foreach (var item in group)
{
lineslength[index] += item.Length;
Console.Write(item + " ");
}
Console.WriteLine();
index++;
}
int longestIndex = Array.FindIndex(lineslength, val => val.Equals(lineslength.Max()));
Console.WriteLine(longestIndex);
I used the GroupBy method to group strings by their first letter. Then when I was displaying strings I also counted their length. Using the static FindIndex method of the Array class, I found the index containing the maximum value of the array what corresponds to the line with the maximum length. So index zero is the first line, one is the second line etc.

Aligning text into three columns

I would like to show the LinkedList data into three separate columns with List1, List2, and List3 as the column heading. I am unable to align the LinkedList data under these headings.
void LoopThruLinkedList(LinkedList<string> strList)
{
foreach(string str in strList)
{
Console.WriteLine(string.Format($"{str, -20}"));
}
//Console.WriteLine();
}
// Creating a linkedlist
// Using LinkedList class
LinkedList<string> my_list = new LinkedList<string>();
// Adding elements in the LinkedList
// Using AddLast() method
my_list.AddLast("Zoya");
my_list.AddLast("Shilpa");
my_list.AddLast("Rohit");
my_list.AddLast("Rohan");
my_list.AddLast("Juhi");
my_list.AddLast("Zoya");
my_list.AddLast("Rohit");
string List1 = "List One Students: ";
string List2 = "List Two Students: ";
string List3 = "List Three Students: ";
Console.WriteLine($"{List1, -20}{List2, -20}{List3, -20}");
// Accessing the elements of LinkedList using the foreach loop
LoopThruLinkedList(my_list);
LoopThruLinkedList(my_list);
LoopThruLinkedList(my_list);
There are a lot of issues here
Why do you need linkedList? they have no indexers and makes any solution inefficent
Why are all the lists the same, however maybe this is just an example.
You need to flatten this somehow to write it to the console sequentially
However this might point you in the right direction
void LoopThruLinkedList(params LinkedList<string>[] strLists)
{
var max = strLists.Max(x => x.Count());
for (int i = 0; i < max; i++)
{
foreach (var item in strLists)
Console.Write($"{(item.Count > i ? item.ElementAt(i) : ""),-20}");
Console.WriteLine();
}
}
// Creating a linkedlist
// Using LinkedList class
LinkedList<string> my_list = new LinkedList<string>();
// Adding elements in the LinkedList
// Using AddLast() method
my_list.AddLast("Zoya");
my_list.AddLast("Shilpa");
my_list.AddLast("Rohit");
my_list.AddLast("Rohan");
my_list.AddLast("Juhi");
my_list.AddLast("Zoya");
my_list.AddLast("Rohit");
string List1 = "List One Students: ";
string List2 = "List Two Students: ";
string List3 = "List Three Students: ";
Console.WriteLine($"{List1,-20}{List2,-20}{List3,-20}");
// Accessing the elements of LinkedList using the foreach loop
LoopThruLinkedList(my_list, my_list, my_list);
Or another variation using enumeration in a pivot style operation with GroupBy
void LoopThruLinkedList(params LinkedList<string>[] strLists)
{
var results = strLists
.SelectMany(inner => inner.Select((item, index) => new { item, index }))
.GroupBy(i => i.index, i => i.item)
.Select(g => g.ToList());
foreach (var list in results)
{
foreach (var item in list)
Console.Write($"{item,-20}");
Console.WriteLine();
}
}
Here is a variation that uses only enumerators and is therefore more efficient than accessing elements by index:
void PrintLists(LinkedList<string>[] lists, string[] captions)
{
//Find the necessary column widths
var columnWidths = new int[lists.Length];
for(int i = 0; i < lists.Length; ++i)
{
columnWidths[i] = captions[i].Length;
foreach (var s in lists[i])
columnWidths[i] = Math.Max(columnWidths[i], s.Length);
columnWidths[i] += 2; //spacing
}
//Print the headings
for(int i = 0; i < lists.Length; ++i)
Console.Write(captions[i].PadRight(columnWidths[i]));
Console.WriteLine();
//Initialize iterators
var iterators = new LinkedList<string>.Enumerator[lists.Length];
var iteratorsValid = new bool[lists.Length];
for (int i = 0; i < lists.Length; ++i)
{
iterators[i] = lists[i].GetEnumerator();
iteratorsValid[i] = iterators[i].MoveNext();
}
//Print the rest of the table
while (iteratorsValid.Any(b => b))
{
for (int i = 0; i < lists.Length; ++i)
{
if (iteratorsValid[i])
{
var item = iterators[i].Current;
Console.Write(item.PadRight(columnWidths[i]));
iteratorsValid[i] = iterators[i].MoveNext();
}
else
Console.Write(new String(' ', columnWidths[i]));
}
Console.WriteLine();
}
}
Which you call like
PrintLists( new [] { my_list, my_list, my_list},
new [] { "List One Students: ", "List Two Students: ", "List Three Students: " });

Why is the .Clear() function clearing the wrong list?

public void ConvertMoves()
{
for (int i = 0; i < maxDirections; i++)
{
Debug.Log("gimme tsMoves "+tSpossibleMoves[i].Count + " from " + this);
possibleAttacks[i] = tSpossibleAttacks[i];
possibleAttacksInactive[i] = tSpossibleAttacksInactive[i];
possibleAttackIndicators[i] = tSpossibleAttackIndicators[i];
possibleMoves[i] = tSpossibleMoves[i];
Debug.Log("Gimme moves(1) " + possibleMoves[i].Count + " from " + this);
}
for (int i = 0; i < maxDirections; i++)
{
tSpossibleAttacks[i].Clear();
tSpossibleAttacksInactive[i].Clear();
tSpossibleAttackIndicators[i].Clear();
tSpossibleMoves[i].Clear();
Debug.Log("Gimme moves(2) " + possibleMoves[i].Count + " from " + this);
}
}
so the Debug Log reports the following:
gimme tsMoves 2 from JeanArc(Clone) (JeanArc)
Gimme moves(1) 2 from JeanArc(Clone) (JeanArc)
sofar everything is doing fine but then...
Gimme moves(2) 0 from JeanArc(Clone) (JeanArc)
why does it clear the moves of whole different List variable ?
This doesn't create a copy of the list item:
possibleAttacks[i] = tSpossibleAttacks[i]
It simply copies the reference to the same object into a second variable, so possibleAttacks[i] and tSpossibleAttacks[i] now both point to the same item in memory. Think of it like having two credit cards to access one bank account.
You can read more about reference types here in Microsoft's docs.
As Heinzi pointed out in the comment below, you can copy your item (as it's a list) by calling:
possibleAttacks[i] = tSpossibleAttacks[i].ToList();
By the way, if you just want to assign tSpossibleAttacks[i] and then reset it, you could also just do this:
possibleAttacks[i] = tSpossibleAttacks[i];
tSpossibleAttacks[i] = new List<your_type_name_here>(); // this will overwrite the reference held by `tSpossibleAttacks[i]`.
Note that if your list contains reference types, you have a similar problem within the list, for example:
public class Test
{
public string Name { get; set; }
}
List<Test> list1 = new List<Test>();
list1.Add(new Test() { Name = "John" });
List<Test> list2 = list1.ToList();
Console.WriteLine(list1[0].Name); // John
Console.WriteLine(list2[0].Name); // John
list2[0].Name = "Fred";
Console.WriteLine(list1[0].Name); // Fred
Console.WriteLine(list2[0].Name); // Fred
So I'd recommend reading up on value types vs reference types and how references work in C#.
What #John said. You need to copy the lists.
for (int i = 0; i < maxDirections; i++)
{
Debug.Log("gimme tsMoves "+tSpossibleMoves[i].Count + " from " + this);
possibleAttacks[i] = tSpossibleAttacks[i];
tSpossibleAttacks[i] = new List<T>;
possibleAttacksInactive[i] = tSpossibleAttacksInactive[i];
tSpossibleAttacksInactive[i] = new List<U>();
possibleAttackIndicators[i] = tSpossibleAttackIndicators[i];
tSpossibleAttackIndicators[i] = new List<V>();
possibleMoves[i] = tSpossibleMoves[i];
tSpossibleMoves[i] = new List<Z>();
Debug.Log($"Gimme moves(1), i={i}: {possibleMoves[i].Count} from {this}");
Debug.Log($"Gimme moves(2) i={i}: {tpossibleMoves[i].Count} from {this}");
}
Example:
var l1 = new List<string>();
List<string> l2;
l1.Add("One");
l1.Add("Two");
l2 = l1;
l1 = new List<string>();
l1.Add("Three");
Console.WriteLine("L1:");
foreach (var elem in l1)
{
Console.WriteLine(elem);
}
Console.WriteLine("L2:");
foreach (var elem in l2)
{
Console.WriteLine(elem);
}
This prints:
L1:
Three
L2:
One
Two

How do I make List of Lists? And then add to each List values?

class ExtractLinks
{
WebClient contents = new WebClient();
string cont;
List<string> links = new List<string>();
List<string> FilteredLinks = new List<string>();
List<string> Respones = new List<string>();
List<List<string>> Threads = new List<List<string>>();
public void Links(string FileName)
{
HtmlDocument doc = new HtmlDocument();
doc.Load(FileName);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[#href]"))
{
HtmlAttribute att = link.Attributes["href"];
if (att.Value.StartsWith("http://rotter.net/forum/scoops1"))
{
links.Add(att.Value);
}
}
for (int i = 0; i < links.Count; i++)
{
int f = links[i].IndexOf("#");
string test = links[i].Substring(0, f);
FilteredLinks.Add(test);
}
for (int i = 0; i < FilteredLinks.Count; i++)
{
contents.Encoding = System.Text.Encoding.GetEncoding(1255);
cont = contents.DownloadString(FilteredLinks[i]);
GetResponsers(cont);
}
}
private void GetResponsers(string contents)
{
int f = 0;
int startPos = 0;
while (true)
{
string firstTag = "<FONT CLASS='text16b'>";
string lastTag = "&n";
f = contents.IndexOf(firstTag, startPos);
if (f == -1)
{
break;
}
int g = contents.IndexOf(lastTag, f);
startPos = g + lastTag.Length;
string responser = contents.Substring(f + firstTag.Length, g - f - firstTag.Length);
foreach (List<string> subList in Threads)
{
}
}
}
}
I created this variable :
List<List<string>> Threads = new List<List<string>>();
The first thing I don't know yet how to do is how to create inside Threads number of Lists according to the FilteredLinks.Count inside the Links method.
Second thing is in the GetResponsers method I did:
foreach (List<string> subList in Threads)
{
}
But what I want is that first time it will add all the values from variable responser to the first List in Threads. Then when it's getting to the break; it stop then and then in the Links methods its calling GetResponsers(cont); again this time I want that all the values in responser to be added to the second List in Threads.
I know that each time it's getting to the break; it will get the next FilteredLink from FilteredLinks.
How do I create number of Lists in Threads according to the FilteredLinks.Count?
How do I make the code in GetResponsers to add the responser ?
You don't need to specify the count for the number of lists in Threads, since it is a list, you can simply keep adding lists to it. So the first part is correct where you are declaring it.
The second part --> Your calling method will change. Look below for the calling method.
The third part --> Change private void GetResponsers(string contents) to private void GetResponsers(List threadList, string contents). Look below for implementation change.
Also the loop will look like this then
//other code you have
List<List<string>> Threads = new List<List<string>>();
public void Links(string FileName)
{
// ...other code you have
for (int i = 0; i < FilteredLinks.Count; i++)
{
threads.Add(new List<string>);
contents.Encoding = System.Text.Encoding.GetEncoding(1255);
cont = contents.DownloadString(FilteredLinks[i]);
GetResponsers(threads[threads.Count - 1], cont);
}
}
private void GetResponsers(List<string> threadList, string contents)
{
int f = 0;
int startPos = 0;
while (true)
{
string firstTag = "<FONT CLASS='text16b'>";
string lastTag = "&n";
f = contents.IndexOf(firstTag, startPos);
if (f == -1)
{
break;
}
int g = contents.IndexOf(lastTag, f);
startPos = g + lastTag.Length;
string responser = contents.Substring(f + firstTag.Length, g - f - firstTag.Length);
threadList.Add(responser);
}
}
PS: Please excuse the formatting.
How do i make List of Lists ? And then add to each List values?
The following codesnippet demonstrates you, how to handle List<List<string>>.
List<List<string>> threads = new List<List<string>>();
List<string> list1 = new List<string>();
list1.Add("List1_1");
list1.Add("List1_2")
threads.Add(list1);
List<string> list2 = new List<string>();
list1.Add("List2_1");
list1.Add("List2_2")
list1.Add("List2_3")
threads.Add(list2);
How do i create number of Lists in Threads according to the
FilteredLinks.Count ?
for(int i = 0; i < FilteredLinks.Count; i++)
{
var newList = new List<string>();
newList.Add("item1"); //add whatever you wish, here.
newList.Add("item2");
Threads.Add(newList);
}
I'm afraid I can't help you with Question #2, since I don't understand what you try to achieve there exactly.

ObservableCollection adds an item and overrides the others

I have this function:
ObservableCollection<TankstellenItem> completeList = new ObservableCollection<TankstellenItem>();
for(var j = 0; j < listItem.Count; j++)
{
foreach (FuelItem fItem in listItem.ToList()[j].Fuels)
{
Debug.WriteLine("HERE AGAIN: " + fItem.Price);
TankstellenItem newItem = new TankstellenItem();
ObservableCollection<FuelItem> fuelList = new ObservableCollection<FuelItem>();
newItem = listItem.ToList()[j];
newItem.Fuels = null;
fuelList.Add(fItem);
newItem.Fuels = fuelList;
completeList.Add(newItem);
Debug.WriteLine("PRICES: " + completeList.ToList()[0].Fuels[0].Price);
}
}
Debug.WriteLine("COMPLETELIST LENGTH: " + completeList.ToList()[0].Fuels[0].Price + " + " + completeList.ToList()[1].Fuels[0].Price);
and the outcome is:
LISTITEM LENGTH: 1
HERE AGAIN: 1,699
PRICES: 1,699
HERE AGAIN: 1,529
PRICES: 1,529
COMPLETELIST LENGTH: 1,529 + 1,529
As you can seein the foreach the Prices are different. But after the for-method the output is only 1,529 in every item. I don't understand why this happens.
From what I can tell from your code, you want to retrieve all the Fuel objects from each TankstellenItem in your original list (listItem) and then put them into an ObservableCollection.
// Create Complete List
ObservableCollection<TankstellenItem> completeList = new ObservableCollection<TankstellenItem>();
// Loop through each TankstellenItem in listItem
foreach(TankstellenItem tItem in listItem) {
// Create an ObservableCollection to store FuelItem objects
ObservableCollection<FuelItem> fuelList = new ObservableCollection<FuelItem>();
// Loop through each FuelItem in the current TankstellenItem
foreach(FuelItem fItem in tItem) {
// Add the FuelItem from current TankstellenItem to the ObservableCollection
Debug.WriteLine("HERE AGAIN: " + fItem.Price);
completeList.Add(new TankstellenItem() {
// Add the FuelItem
Fuels = new ObservableCollection<FuelItem>() {
fItem,
};
}
}
}
Although, it might be easier, if you only care about the FuelItems, to have a collection of the FuelItems instead of wrapping them inside a TankstellenItem.
ObservableCollection<FuelItem> completeList = new ObservableCollection<FuelItem>();
foreach(TankstellenItem tItem in listItem) {
foreach(FuelItem fItem in tItem) {
completeList.Add(fItem);
}
}
Linq might make this easier: Do you actually want
var completelist=listItem.SelectMany(
i=>i.Fuels.Select(
f=>new TankstellenItem { Fuels=new ObservableCollection<FuelItem>(new[] { f})}
)
).ToList();

Categories