I have an arraylist which has data structures in it.
I am having problems trying to figure out how to get those values back and show them in a table..
Thanks
this is my structure..
public BackupSpecEntry(string Path, string InclExcl, byte InclExclFlags, bool IndexContents,
int ServerBackupSpecId, int Freq, int Retention)
{
path = Path;
inclExcl = InclExcl;
inclExclFlags = InclExclFlags;
indexContents = IndexContents;
serverBackupSpecId = ServerBackupSpecId;
freq = Freq;
retention = Retention;
}
With an ArrayList you need to cast them,
ArrayList list = new ArrayLIst();
: your code
BackupSpecEntry entry = (BackupSpecEntry)list[0];
However, with generics with C# you can create a template list:
List<BackupSpecEntry> list = new List<BackupSpecEntry>();
: your fill list code
BackupSpecEntry entry = list[0];
An ArrayList isn't strongly typed so whenever you pull out an item, you have to cast it back to your custom object type. Then you should be able to access it's properties.
I assume by Table and by the Asp.net attribute that you mean a DataGrid, GridView, or DetailsView. Is this correct. Assuming that it is: read up on Data binding and custom objects. There is a lot of information about this subject on the internet if you look for it.
Related
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.
public class LIST
{
public double num;
public double longi;
public double ux;
public double vy;
}
public static List<LIST> LIST1= new List<LIST>();
LIST L1 = new LIST();
L1.ux= // I take l1.ux from stream reader by reading a file and made this
for
L1.vy=.. the other parameters
L1.longi=..
L1.num=....
LIST1.Add(L1)
Here my problem is ı made a list that contains 4 parameters. But ı want to find just one parameter value for instance L1.num how can I take this value from a list?
var indexOfLISTObject = 1 //you need to know which object from LIST1 you want to use
var numParamOfLISTObject = LIST1[indexOfLISTObject].num;
Please Don't name your object LIST. It will create readability issues down the line for you. Name the object what a particular item in your list will represent.
To access a particular property from an item in list you can do following
List l1 = new list();
//Add items ...
//Print property from particular index
Console.WriteLine(l1[index].propertyname);
Put the paremeters inside a list one by one.
in your example you want the parameter num.
List parameterNum = new List();
parameterNum = l1.Select(x => x.Num).ToList();
parameterNum has now the list of Num from l1 list.
If you mean to find/search for a particular value, you could also use System.Linq. For example if you would like to find a member where num is set to 2, you could do this:
LIST1.Where(item=>item.num==2).FirstOrDefault()
According my Understanding If you want to Get objects that contain with specific parameter
then you can use this code
public static List<LIST> LIST1= new List<LIST>();
LIST L1 = new LIST();
var SearchedValue= List1.where(x=>x.num==L1.num).tolist();
if you want just L1.num value then you can use this line (if searched record will 1 then you should use this)
var SearchedValue= List1.where(x=>x.num==L1.num).FirstOrDefault().num;
you can use
LIST1[index].propertyname //index is index of list element and propertyname is the name of property you want to access
I'm building a console application on visual studio that involves some SQL. Basically, I have a database table with a large amount of columns, and I need set them as variables and have a way of addressing them efficiently. Right now I'm thinking an array which I can then loop through. Here's sort of what my code looks like right now:
SqlCommand getColumns = new SqlCommand("SELECT * FROM tableName",
connection1);
myReader = getColumns.ExecuteReader();
while (myReader.Read())
{
string[] array = new string { myReader["ColumnName"].ToString(),
myReader["ColumnName2"].ToString, etc...};
for (int i = 0; i <= array.length; i++) {
some action...array[i];
}
}
I admit I'm somewhat new to c# and this kind of development, so let me know if there's a more efficient way to do this or if I need to post more code. Thanks!
You do not need custom type for this. .NET already gives you DataSet and DataTable. You need DataTable since it's just one result set, and results look just as same as you see in SSMS results window.
var dt = new DataTable();
dt.Load(myReader);
If you have a lot of columns, accessing them by index instead of name would be more efficient:
string[] array = new string[myReader.FieldCount];
for(int i=0; i<myReader.FieldCount; i++)
{
array[i] = myReader[i].ToString();
}
Also, if you know the number of columns in advance (which is usually the case), you could move array declaration out of while(myReader.Read()), resulting in less memory allocation due to reusing same array.
Another suggestion is that you might not want to convert everything to string... Declare array of objects and handle each data type as you wish. Integer probably should be assined to some entity's integer property instead of saving in string array.
Personally I would declare some entity and declare a List of those. Then populate the list from database:
public class MyEntity
{
public string Name { get; set; }
public int Age { get; set; }
//Other properties
}
//...
SqlCommand getColumns = new SqlCommand("SELECT * FROM tableName",
connection1);
var myDataFromTable = new List<MyEntity>();
myReader = getColumns.ExecuteReader();
while (myReader.Read())
{
myDataFromTable.Add(new MyEntity {
Name = myReader[0] as string,
Age = (int)myReader[1]
//...
});
}
//Process your list of entities here
Another approach may be not to save everything into memory (as you do now), but process data on the fly - everything depends on your requirements and the size of data set. But anyway I recommend parsing data to some entity and work with that.
As per my understanding - On way of doing it.
We can create a class containing properties similar to that of columns in table. In the while loop you can create object of that class, then set the properties with the respected columns and add that object to Arraylist (Arraylist containing the class objects ).
Reference Link : https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx
Hope its helps.
I'm trying to use a property of individual object instances stored within a List<T> object, but I can't seem to access the properties directly.
I have an object (sportsCarVehicle) which stores a user-defined name (strVehicleName) (amongst other properties, but that's not important) within itself. The object is then stored within a List<sportsCarVehicle> object called sportsCarVehicleStorage.
I need to access every instance of sportsCarVehicle in List<sportsCarVehicle> and pass the value of strVehicleName to a combo box on a form.
I assume I'll need some kind to loop to cycle through each instance and pass the name to the combo box, but my main issue is not being able to access the property I need. The sportsCarVehicle instances have no reference-able name.
One more thing I should note: the constructor for sportsCarVehicle is called within the sportsCarVehicleStorage.Add() method.
Any suggestions on how I could do this?
Cant you do this
List<string> lst = new List<string>{"Hello", "World"};
int len = lst[0].Length;
Here .Length is a property of string. As long as that property is public we can access it.
In your case
List<sportsCarVehicle> sportsCarVehicleStorage = new List<sportsCarVehicle>();
// Some code to populate list.
mycombobox.Items = sportsCarVehicleStorage
.Select(x => x.strVehicleName).ToArray();
Make Sure property strVehicleName is public in that class.
You can use foreach to loop through the list, assigning each member of the list to a named variable, like:
foreach (sportsCarVehicle scv in sportsCarVehicleStorage)
{
//scv is the name of the currently looping sportsCarVehicle object
//use scv.strVehicleName to access the property.
myComboBox.Items.Add(scv.strVehicleName);
}
foreach (SportsCarVehicle car in myListName)
{
//do stuff here
}
That's the most basic example, you can use PLINQ etc. to do it in a more streamlined way.
An alternative could be to bind the list of sportsCarVehicle directly to the comboBox, for example:
List<sportCarVehicle> sportsCarVehicleStorage= new List<sportsCarVehicle>;
// Set up list content here
// ...
myComboBox.DataSource = sportsCarVehicleStorage;
myComboBox.DisplayMember = "strVehicleName";
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>>