Retrieving list of objects from listbox [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a listbox with listboxitems.
In the listboxitems, I have domain objects.
How kan I converse the listbox's items to a list of domain objects?
I now have something like this:
List<DomainObject> list = theListBox.Items.Cast<ListViewItem>()
.Select(item => item.Content)
.ToList();
This isn't working...
Thanks in advance!
EDIT:
I'm going to give an example of my domainobject class because I wasn't very clear:
public class Person
{
public int personId{get;set;}
public string name{get; set;}
public double price{ get; set; }
public override string ToString()
{
return name;
}
These objects are stored in the listboxitems via the .content property.
Now I just want a list of those PersonObject that are in the listboxitems of the listbox.

You have to convert the output to the DomainObject in the Select() method, for sample:
List<DomainObject> list = theListBox.Items.Cast<ListViewItem>()
.Select(item => new DomainObject() { Content = item.Content})
.ToList();
I am not sure about what property the DomainObject type has, but you could fill these properties in the object initialization.

Related

Nested list getter and setter c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this nested list :
public List<List<string>> pcList = new List<List<string>>();
How can I create getters and setters for it?
I have tried everything out there on the internet and nothing seems to work
Thx!
EDIT:
So, In class Pc I have this code:
class PC
{
public List<List<string>> pcList = new List<List<string>>();
public List<string> subList = new List<string>();
}
so the pcList is the "parent" list and the sublist is actually the place where I add each pc with its info.
I have a method where I populate the lists. Then I want to use an object of this class in another class called X, let's say.
I have tried to simply access the lists by using object.ListName but it doesn't work.
You can use auto property - just add {get;set;} (note that usually properties are upper case)
public List<List<string>> PcList {get;set;}
public List<List<string>> pcList { get; set; } = new List<List<string>>();
This is a variable.
If you want a property declare it like:
public List<List<string>> pcList {get;set;}

Ignore mapping for all members with different types and same name, using newest version of Automapper(6.1.1) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I ignore mapping if some properties have different type with same name? By default it's throwing error.
I found some answers for an older versions of automapper but they are not working with the newest.
For example one property is string the other is bool but they both have the same name. The behaviour i want is to ignore them(not try to map them).
Here is a small example based on this link
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Class1, Class2>();
cfg.ForAllMaps((typeMap, mappingExpr) =>
{
var ignoredPropMaps = typeMap.GetPropertyMaps();
foreach (var map in ignoredPropMaps)
{
var sourcePropInfo = map.SourceMember as PropertyInfo;
if (sourcePropInfo == null) continue;
if (sourcePropInfo.PropertyType != map.DestinationPropertyType)
map.Ignored = true;
}
});
});
Class1 src = new Class1()
{
TestProperty = "A"
};
Class2 dest = Mapper.Map<Class1, Class2>(src);
Test classes:
public class Class1
{
public string TestProperty { get; set; }
}
public class Class2
{
public bool TestProperty { get; set; }
}

Class with same code and send data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have this C# class to send the data from DriveInfo Class to my window form:
using System;
public class FileSystemInfo
{
public string CheckTotalFreeSpace()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.VolumeLabel.ToString();
}
}
I want to send huge data from one class (see my example) into my form class (maybe labels or ListBox Control), by using a a good way to solve this issue. Also I don't want to put this line of code into separate c# class method:
System.IO.DriveInfo DInfo
The basic issue for me is: deal and show many info about my computer so I need to put all this info into one structure or something else.
You may use Lazy class:
using System.IO;
public class FileSystemInfo
{
private readonly Lazy<DriveInfo> dInfo =
new Lazy<DriveInfo>(() => new DriveInfo(#"C:\"));
public string CheckTotalFreeSpace()
{
return dInfo.Value.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
return dInfo.Value.VolumeLabel.ToString();
}
}

How to use IEnumerable public property? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public class Kuku
{
private LinkedList<MyClass> m_list;
public IEnumerable<MyClass> Locations { get { return m_list; } }
}
I need to implement in public class FindAllMyClass
the method which iterates all MyClass objects in Kuku - I don't know how do I use Locations property from Kuku.
Should I define IEnumerable Locations or just make LinkedList m_list to be public property?
Should I define IEnumerable<MyClass> Locations or just make LinkedList<MyClass> m_list to be a public property?
public class FindAllMyClass
{
public void itMethod(Kuku input)
{
//This is not correct
foreach (MyClass c in input.Locations)
{
}
}
}
Why to define IEnumerable Locations and not just make LinkedList m_list to be public property?
Several possible reasons:
What if later you decide you don't want to implement it as a LinkedList<> but as a regular List? Or a lazy-loaded collection type? or an array?
Exposing the collection as a generic IEnumerable<T> allows you to change out the internal implementation later without changing the public contract.
If you make the collection a property (with get; set; accessors) you are allowing clients to add to, remove from, even replace the entire list. Exposing it as IEnumerable indicates that the list is intended to be read-only (unless you expose Add methods somewhere else).
I don't know how do I use Locations property from Kuku.
Sure you do - you're already doing it in your sample:
foreach (MyClass c in input.Locations)
{
}
Or you can use Linq to search for or aggregate data from the collection (foreach is still appropriate for updating the instances.
You could use LINQ to object:
input.Locations.Where(x=> x.Property1 == value1 && x.Property2 > value2);
IEnumerable gives you a way to expose a list from your class which has the following properties:
It is implementation independent - you can change what sort of list you use internally without needing to change everything which uses your class
It integrates well with LINQ, foreach and various other language constructs
It provides a read only view on the list, so no chance of others updating the list (inserting/deleting items)

Copy a static field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a class in which I operate some methods.
public class MyClass
{
public static List<ObjectA> MyField;
public static Object MyMethod()
{
List<ObjectA> anotherObjectA = new List<ObjectA>();
// I do something with anotherObjectA...
// after processing something now I want to keep the current status of anotherObjectA to MyField:
MyField = anotherObjectA;
// and now I want to work just with anotherObjectA. The problem is that whatever I work with anotherObjectA it changes also MyField
}
}
How can i achieve what I am trying to do
You can do
MyField = new List<ObjectA>(anotherObjectA);
This will create a copy of the list. However, any changes to the objects in the list will be visible in both. You'll have to decide for yourself how deep your copy has to be. If you really want a deep copy, you'll need to provide a mechanism for ObjectA to make a copy of itself, iterate over the original list, and add a copy of each object to the target list.
MyField and anotherObjectA reference same object. So if you change MyField it also changes anotherObjectA.
So First you need to create two List objects:
MyField = new List<ObjectA>(anotherObjectA);
This will create two list objects but the ObjectA objects inside the list are still referencing to the same.
MyField.First() == anotherObjectA.First() // returns true;
If you want to make a complete copy you also need to create a copy of objects inside anotherObjectA
public class ObjectA
{
public ObjectA() { } // Normal constructor
public ObjectA(ObjectA objToCopy) { /* copy fields into new object */ }
}
MyField = anotherObjectA.Select(obja => new ObjectA(obja)).ToList();
with this solution, changing objects inside MyField will not affect objects inside anotherObjectA unless ObjectA also contains reference types.

Categories

Resources