Using LinkedList<> in C# - c#

I am looking to use the C# Linked list class instead of creating my own, but I'm not sure how to stick multiple items in the LinkedList<>.
I would like to do something like LinkedList<string, List>, to allow me to have:
entry1->string
And also have a List:
entry2->string, and list
All I see online from tutorials it that it will allow LinkedList only,
Any ideas on how to get more than 1 value in my linked list? Thanks.

I'm going to guess what you mean...
You need to create a custom object for your list..
public class MyListItem
{
public String s;
public List<Something> list;
}
Then you can do
LinkedList<MyListItem> myLinkedList = new LinkedList<MyListItem>();
Now each item in your LinkedList has a String and a List.
You can add an item with something like
MyListItem myListItem = new MyListItem();
myListItem.s = "a string";
myListItem.list = someList;
myLinkedList.AddLast(myListItem);

You could also try this:
Dictionary<String,List<Something>>

Related

Another class property call from base class list

I have a two class properdata and pprosecnddata both classes having property
I want to access product property from properdata class list object. How is it possible,below is my sample code
pupilc class ProperData
{
public string code{get;set;}
public List<ProSecndData>Secnd{get;set;}
}
public class ProSecndData
{
public string product{get;set;}
}
I am trying to call property like that
class Program
{
static void Main(string[] args)
{
ProperData.Secnd.Product = "Hello";
}
}
you cannot directly access property of Secnd as it is a list
you need to iterate or select the index of the List<Secnd>
you must initialize Secnd first and Secnd should have items in the list
properData.Secnd = new List<ProSecndData>();
so it can be access via
foreach(var second in properData.Secnd)
{
second.product = "hello";
}
//or
for(var i = 0; i < proderData.Secnd.Count(); i++)
{
properData.Secnd[i].product = "hello";
}
//or
var index = //0-length of list;
properData.Secnd[index].product = "hello";
if you want to have items first then add first on your Secnd List
properData.Secnd = new List<ProSecndData>();
properData.Secnd.Add(new ProSecndData{ product = "hello"});
then you now can iterate the list by using methods above
You are trying to access list as a single object, which is not possible.
you need to create single instance of your list class and then you can add string in that single instance.
properData.Secnd = new List<ProSecndData>();
ProSecndData proSecndData = new ProSecndData();
proSecndData.product = "Hello";
properData.Secnd.Add(proSecndData);
Actually I know the answer already, you have not created a constructor to initialise your List.
I'm guessing you get a object null ref error?
Create the constructor to initialise your list and it should be fine.
But in future, please post the error message (not the whole stack, just the actual error) as well as all the code required to repeat the issue. Otherwise you run the risk of getting your question deleted
(It should be deleted anyway because it could be considered a "what is a null ref err?" question).
Also you are accessing an item in a list like the list is that item (should be more like: ProperData.Secnd.elementAt(0).product, please also note the capitalisation of 'product' in the model vs your code.

Removing special type from list that contains multiple types C#

The problem I have is that I want to remove a certain item from my list that contains 2 different type. Whatever I try I just get errors and that I can use types, if I create a new instance of that class it won't delete the old. I'm really stuck here, I have no idea on what I should do.
private List<Toys> toys = new List<Toys>();
toys.Add(new Ball("red"));
toys.Add(new MouseToy("gray"));
public abstract class Toys {}
public class Ball : Toys {}
public class MouseToy : Toys {}
//how do i remove only one of the types?
//beacuse this does not work witch is one of many things i tried
foreach(var toy in toys){
if (toy.GetType == Ball){
toys.Remove(toy)
}
}
That's pretty much the code I use for the list.
You could do just:
toys = toys.Where(toy=>!(toy is Ball)).ToList();
As mentioned #Sefe in comments (thanks): you could use do it even simple and better with
List<MouseToy> mouseToys = toys.OfType<MouseToy>().ToList();
If it's ok for you that your new collection will be List<MouseToy> not List<Toys> . And that won't work if you will get a third class CatToy and you will still want to get all items, but not of Ball class
You have two issues with your solution:
You are modifying the list you are iterating with foreach. That will cause an InvalidOperationException.
.GetType == Ball does not work like this. Use is instead.
Try this:
foreach(var toy in toys.ToArray()){
if (toy is Ball){
toys.Remove(toy);
}
}
You can also use LINQ:
foreach(var toy in toys.OfType<Ball>().ToArray()){
toys.Remove(toy);
}
You cannot remove items from a list while iterating over that same list. You can either use a reverse for loop (i.e. loop descending):
for (int i = toys.Length - 1; i >= 0; i--)
{
if (toys[i] is Ball)
{
toys.RemoveAt(i);
}
}
...or you can use one of the methods defined in the System.Linq namespace. Maksim Simkin shows how to use the Where method; you could also use the OfType and Except methods:
toys = toys.Except(toys.OfType<Ball>())
.ToList();

C# Parameter of IList<IList<(ClassType)>>

I can't seem to figure out why the following doesn't work.
From reading around it seems it must be somehow related to something like this.
public class Test<T>
{
public void SomeFunction(IList<IList<T>> ListOfLists)
{
}
}
....
List<List<SimpleClass>> lists = new List<List<SimpleClass>>();
new Test<SimpleClass>().SomeFunction( (IList<IList<SimpleClass>>)lists );
How can I require a list to actually be a list of lists? I can't seem to grasp what I am doing wrong.
Thanks,
A List<List<SimpleClass>> isn't an IList<IList<SimpleClass>>.
For example, for the latter type, you could call lists.Add(new SimpleClass[5]) as SimpleClass[] implements IList<SimpleClass>. But you can't do that with a List<List<SimpleClass>>, because each element has to be a List<SimpleClass>.
This would be okay though:
List<IList<SimpleClass>> lists = new List<IList<SimpleClass>>();

Multidimensional Lists in C#

At the moment I am using one list to store one part of my data, and it's working perfectly in this format:
Item
----------------
Joe Bloggs
George Forman
Peter Pan
Now, I would like to add another line to this list, for it to work like so:
NAME EMAIL
------------------------------------------------------
Joe Bloggs joe#bloggs.com
George Forman george#formangrills.co
Peter Pan me#neverland.com
I've tried using this code to create a list within a list, and this code is used in another method in a foreach loop:
// Where List is instantiated
List<List<string>> list2d = new List<List<string>>
...
// Where DataGrid instance is given the list
dg.DataSource = list2d;
dg.DataBind();
...
// In another method, where all people add their names and emails, then are added
// to the two-dimensional list
foreach (People p in ppl.results) {
list.Add(results.name);
list.Add(results.email);
list2d.Add(list);
}
When I run this, I get this result:
Capacity Count
----------------
16 16
16 16
16 16
... ...
Where am I going wrong here. How can I get the output I desire with the code I am using right now?
Why don't you use a List<People> instead of a List<List<string>> ?
Highly recommend something more like this:
public class Person {
public string Name {get; set;}
public string Email {get; set;}
}
var people = new List<Person>();
Easier to read, easy to code.
If for some reason you don't want to define a Person class and use List<Person> as advised, you can use a tuple, such as (C# 7):
var people = new List<(string Name, string Email)>
{
("Joe Bloggs", "joe#bloggs.com"),
("George Forman", "george#formangrills.co"),
("Peter Pan", "me#neverland.com")
};
var georgeEmail = people[1].Email;
The Name and Email member names are optional, you can omit them and access them using Item1 and Item2 respectively.
There are defined tuples for up to 8 members.
For earlier versions of C#, you can still use a List<Tuple<string, string>> (or preferably ValueTuple using this NuGet package), but you won't benefit from customized member names.
Where does the variable results come from?
This block:
foreach (People p in ppl.results) {
list.Add(results.name);
list.Add(results.email);
list2d.Add(list);
}
Should probably read more like:
foreach (People p in ppl.results) {
var list = new List<string>();
list.Add(p.name);
list.Add(p.email);
list2d.Add(list);
}
It's old but thought I'd add my two cents...
Not sure if it will work but try using a KeyValuePair:
List<KeyValuePair<?, ?>> LinkList = new List<KeyValuePair<?, ?>>();
LinkList.Add(new KeyValuePair<?, ?>(Object, Object));
You'll end up with something like this:
LinkList[0] = <Object, Object>
LinkList[1] = <Object, Object>
LinkList[2] = <Object, Object>
and so on...
You should use List<Person> or a HashSet<Person>.
Please show more of your code.
If that last piece of code declares and initializes the list variable outside the loop you're basically reusing the same list object, thus adding everything into one list.
Also show where .Capacity and .Count comes into play, how did you get those values?

Is a Collection of Collections possible and/or the best way? C# .Net 3.5

I have created a class for a dashboard item which will hold information such as placement on the dashboard, description, etc. I am currently using a pair of Collections to hold those dashboard items contained in the "library" and those items showing on the dashboard itself. I have been asked to make this dashboard multi-tab, and my first inclination was to make a new Collection for each tab. For this I would want some type of array or collection which could have many of these dashboard item collections added to it as more tabs are added to the dashboard.
Is this possible, and if so, could I get a little code snip for the declaration of such a collection? I have considered using a single collection with a variable to show which tab the item will be shown in... However, the display and routines to manage dashboard item movement between screen and library currently need those individual collections.
Edit: Thank you for your answers. While I do find them all interesting I believe I am going to go with James solution and will be marking it as the accepted answer.
List< List<Placement>> ListofListOfPlacements = new List< List<Placement>> ();
List<Placement> dashboard1 = new List<Placement>();
List<Placement> dashboard2 = new List<Placement>();
List<Placement> dashboard3 = new List<Placement>();
List<Placement> dashboard4 = new List<Placement>();
ListofListOfPlacements.Add(dashboard1);
ListofListOfPlacements.Add(dashboard2);
ListofListOfPlacements.Add(dashboard3);
ListofListOfPlacements.Add(dashboard4);
Since you are talking about tabs, it sounds to me like you want something closer to a dictionary keyed on the tab name, with a set of items per tab. .NET 3.5 added the ILookup<,> interface:
ILookup<string, Foo> items = null; //TODO
foreach (Foo foo in items["SomeTab"])
{
Console.WriteLine(foo.Bar);
}
Note that the MS implementation is immutable - you can't edit it after creation; however, I wrote an EditableLookup<,> in MiscUtil that allows you to work more effectively (just like a regular .NET collection):
var items = new EditableLookup<string, Foo>();
items.Add("SomeTab", new Foo { Bar = "abc" });
items.Add("AnotherTab", new Foo { Bar = "def" });
items.Add("SomeTab", new Foo { Bar = "ghi" });
foreach (Foo foo in items["SomeTab"])
{ // prints "abc" and "ghi"
Console.WriteLine(foo.Bar);
}
Without EditableLookup<,>, you need to build the lookup via the Enumerable.ToLookup extension method.
If any part of this sounds like an option, I can add more detail...
Why not create a class that contains your second collection and any of the previous information, and just have a collection of these items?
why not put your collection of dash board items in a tabpage class?
like so:
public class DashboardTabPage : TabPage
{
public List<DashboardItem> DashboardItems { get; set; }
public DashboardTabPage() : this (new List<DashboardItem>())
{
}
public DashboardTabPage(List<DashboardItem> items) :base ("Dashboard Thing")
{
DashboardItems = items;
}
}
public class DashboardItem { }
then you can do this:
c.TabPages.Add(new DashboardTabPage());
I think you should go with composition, as below
Per user Dashboard-home(having multiple tabs) object containing list of dashboard objects containing list of dashboard item objects having various operations on them defined. Again the dashboard item can be a usercontrol having all possible events defined which are handled either there in dashboard item object and/or left to do for dashboard UI object that'll work with individual dashboard objects.
I guess this would be better manageable, if you are going to follow SOA and reusable component based architecture.
You can also use a tuple like this: List<(string Father, IEnumerable<string> Children)>.
Example:
var list = new List<(string Parent, IEnumerable<string> Children)>
{
("Bob", new[] { "Charlie", "Marie" }),
("Robert", new[] { "John", "Geoff", "Oliver" }),
};

Categories

Resources