I have a class called rateTime
class rateTime
{
private List<string> t = new List<string>();
private List<string> s = new List<string>();
public rateTime(string[] time, string[] sender)
{
for (int i = 0; i < time.Length; i++)
{
t.Add(time[i]);
s.Add(sender[i]);
}
}
~rateTime() { }
public List<string> Time
{
get { return t;}
set { t = value; }
}
public List<string> Sender
{
get { return s; }
set { s = value; }
}
}
The DataSource of my combobox is set as follows:
rateTime rt = new rateTime(time, rateSender);
cb_rateTime.DataSource = rt.Time;
cb_rateTime.DisplayMember = "time";
In both lists I have 28 strings. I set items from List t as combobox items. And if I chose an item from the combobox with index, for example 10, I want know how can I get the string from list s with index 10.
try elementAt(index) - http://msdn.microsoft.com/en-us/library/bb299233.aspx
or indexer - yourList[index]
I don't know if I understood well, but:
var index = cb_rateTime.SelectedIndex;
var itemS = rt.Sender.elementAt(index);
or
var selected = cb_rateTime.SelectedText;
var itemS = rt.Sender[selected];
That should resolve.
Access it by index as in
MyRateTime.Sender[10]
Related
I am coding an application for my study, but i'm stuck at this point. I made a class called 'Neighborhood'. This class has a string and an int. In my main code I give the class a value and put it in a list. I now want to loop through my list and get the int out of it (put it in a listbox, or do a calculation). How do I get the int out of the list?
class Wijk
{
private string wijken;
private int tijd;
public string Wijken
{
get { return wijken; }
set { wijken = value; }
}
public int Tijd
{
get { return tijd; }
set { tijd = value; }
}
}
Created the list and the instance of the class.
List<object> Uden = new List<object>();
Wijk Wijkeninput = new Wijk();
Now I value the string and int with a combobox and textbox.
private void wijkAanmaken()
{
Wijkeninput.Wijken = Convert.ToString(cbWijken);
Wijkeninput.Tijd = Convert.ToInt16(tbSnelheid.Text);
Uden.Add(Wijkeninput);
}
For this, instead of having an object list, you can have list containing class objects like
List<Wjik> Uden = new List<Wjik>();
then you can access int as follows:
foreach (Wjik obj in listProgram)
{
int tij = Convert.ToInt32(obj.tijd);
}
First the List can be declared like this:
List<Wijk> Uden = new List<Wijk>();
To iterate over it:
foreach(var item in Uden)
{
var myInt = item.Tijd;
var myString = item.Wijken;
//here do whatever you want with the values
}
I have a class baseClass, and a list of objects of the baseClass. What i want to achieve is that i have to dynamically assign the instance number to each object in the list. for that what am doing is that use a constructor to do this.
Following is the class definition:
public class baseClass
{
private int _InstanceNumber;
private int _MyIntVal;
private string _MyString;
public string MyString
{
get { return _MyString; }
set { _MyString = value; }
}
public int MyIntVal
{
get { return _MyIntVal; }
set { _MyIntVal = value; }
}
public int MyProperty
{
get { return _InstanceNumber; }
}
public baseClass(int instance)
{
_InstanceNumber = instance;
}
}
The creation of the List of objects is as follows:
int instanceNumber = 0;
List<baseClass> classList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
I know it is not the actual way for creating this. it does not give the index number actually. how can i calculate the instance number?
Consider the following scenario, that am creating another list of objects then it hard to maintain the instance number. or if i create another object(this also be an instance) external to the list.
int instanceNumber = 0;
List<baseClass> anotherClassList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
Updates:
This is my temporary solution for this. i need proper way/ method to maintain instance number
If you want to find the index of item in the list, you should ask it from the list, not the item like:
var index = list.IndexOf(item);
But it seems that you expect the item to be aware of its position in the list. In order to do this, you should pass the list to the item so it can use it to find its own place in it:
public class Item
{
private List<Item> _containerList;
public Item(List<Item> containerList)
{
_containerList = containerList;
}
public int InstanceNumber
{
get { return _containerList.IndexOf(this); }
}
}
and change your code to:
List<Item> classList = new List<Item>();
classList.Add(new Item(classList ) { ... });
classList.Add(new Item(classList ) { ... });
I would like to create an array of objects. Each object has it's own int array.
For each object I assign values to it's array ONLY with keys given by myself (example: li[i].V[10] = 1; li[i].V[50] = 10; )
Can someone tell me how to do that? Can I do that without using Lists?
The second case is analogical to first. I would like to know how to assign values of object's List
using setter.
I tried to do that by myself. Unfortunately My code crashed cuz I don't know how to set the dimension of V and Word:
class CFiles
{
//private int[] v=new int[5];//dont want to specify the dimention of array here
private int[] v;//vector of file
private List<string> words;
public CFiles()
{
words = Words;
v = new int[50];
v = V;
}
public int[] V { get; set; }
public List<string> Words { get; set; }
}
class Program
{
static void Main(string[] args)
{
CFiles[] li = new CFiles[2];
for(int i=0;i<li.Length;i++)
{
li[i]=new CFiles();
li[i].V[10] = 1;
li[i].V[50] = 10;
li[i].V[50] = 15;
li[i].Words.Add("a");
li[i].Words.Add("ab");
li[i].Words.Add("abc");
}
for (int i = 0; i < li.Length; i++)
{
for(int j=0;j<li[i].V.Length;j++)
{
Console.WriteLine(li[i].V[j]);
}
}
Console.WriteLine();
}
}
Your constructor isn't right and your properties aren't quite right. You might want something more like this:
class CFiles
{
//private int[] v=new int[5];//dont want to specify the dimention of array here
private int[] v;
public int[] V { get { return v; } set { v = value; } }
private List<string> words;
public List<string> Words { get { return words; } set { words = value; } }
public CFiles()
{
words = new List<string>();
v = new int[51]; //needs to be 51 if you are going to assign to index 50 below
}
}
Other than those issues, your code seems to do what you want. You have an array of objects where each object has its own int array (in addition to a string of strings).
Is that not what you want?
I'm trying to populate a DataTreeListView from ObjectListView library using a list. Unfortunately I am unable to achieve it, there is nothing displayed even though there is a count of item inside the List itself.
Class.cs
public class Class
{
protected string xName;
protected int xId;
protected int xParentId;
protected int happinessStatus;
protected int salaryStatus;
public Class()
{
this.xName = "";
this.xId = 0;
this.xParentId = 0;
this.happinessStatus = 0;
this.salaryStatus = 0;
}
public String Name
{
get { return this.xName; }
set { this.xName = value; }
}
public int Id
{
get { return this.xId; }
set { this.xId = value; }
}
public int ParentId
{
get { return this.xParentId; }
set { this.xParentId = value; }
}
public int HappinessStatus
{
get {return this.happinessStatus; }
set { this.happinessStatus = value; }
}
public int SalaryStatus
{
get { return this.salaryStatus; }
set { this.salaryStatus = value; }
}
public static List<Class> GetList()
{
List<Class> oList = new List<Class>();
Class oClass = new Class();
oClass.Name = "Person A";
oClass.Id = 1;
oClass.ParentId = 0;
oClass.HappinessStatus = 1;
oClass.SalaryStatus = 1000;
oList.Add(oClass);
oClass.Name = "Person B";
oClass.Id = 2;
oClass.ParentId = 1;
oClass.HappinessStatus = 1;
oClass.SalaryStatus = 2000;
oList.Add(oClass);
oClass.Name = "Person C";
oClass.Id = 3;
oClass.ParentId = 1;
oClass.HappinessStatus = 1;
oClass.SalaryStatus = 1000;
oList.Add(oClass);
return oList;
}
On the MainForm's Load Event,
I did the following:
List<Class> list = new List<Class>();
list = Class.GetList();
dataTreeListView1.DataSource = list;
On the designer view, I've also created columns which has got aspect name set to each of the property of the class file except the Id and ParentId.
KeyAspectName : Id
ParentKeyAspectName: ParentId
I did a small messagebox to show the count of the item in the list, its correct but nothing displayed out on the dataTreeListView control.
May I know what is wrong with my coding?
Did you set KeyAspectName, ParentKeyAspectName and RootKeyValue accordingly?
If you did it using the designer, RootKeyValue may be your problem:
Due to the limitations of the Designer in the IDE, RootKeyValue can only be given a string value through the IDE. If your ParentKey is not of type string, you will have to set its value through code.
Since you parent key is of type int use
dataTreeListView1.RootKeyValue = 0;
Note that in contrast to the basic OLV, you don't need to add columns manually. If you want to hide the key columns set ShowKeyColumns = false.
EDIT:
There is another mistake in you code. You add the same instance of the object oClass 3 times. Use oClass = new Class(); before initializing a new person.
I want to retrieve object data from an ArrayList;
public class Form1
{
ArrayList list = new ArrayList();
private void OnSockMessage(object sender, SockEventArgs e)
{
Regex MyRegex = new Regex("^[<][A-Za-z]");
if (e.SockMsg != null)
{
string y = e.SockMsg.ToString();
if (MyRegex.IsMatch(y) == true)
{
rrr = y;
string ipdd = SocClient[e.SocketRef].Soc.RemoteEndPoint.ToString();
//serverkey seckey;
list.Add(new serverkey(ipdd,rrr));
}
else
{
string curipadd = SocClient[e.SocketRef].Soc.RemoteEndPoint.ToString();
for (int i = 0; i < list.Count-1; i++)
{
//serverkey pk = list[i] as serverkey;
//string jj = list[i].ToString();
// serverkey pk = new serverkey(list[i].ToString());
/*********************************************
here i want to retrieve data from array list
*********************************************/
string ipadd;
if (curipadd == ipadd )
{
y = DecryptString(e.SockMsg, rrr);
listBox1.Items.Add(txtIP.Text + " <<" + y);
}
}
}
}
public class serverkey : Form1
{
string ipaddress;
string secertkey;
public serverkey(string IPAdd, string Seckey)
{
ipaddress = IPAdd;
secertkey = Seckey;
}
public string ip
{
get { return ipaddress; }
}
public string key
{
get { return secertkey; }
}
You'd be better off using a strongly typed generic List<serverkey> and a foreach loop rather than a for loop. It'll be something like
List<serverkey> list = new List<serverkey>();
//add your items as you already are
foreach(var item in list)
{
item.ip ...// use item as a serverkey
}
Having said that, if you can you use a generic for some reason, use an 'as'
ArrayList list = new ArrayList();
//add your items as you already are
foreach(var item in list)
{
var sk = item as serverkey;
sk.ip ...// use item as a serverkey
}