I am trying to do
var mahByteArray = new ArrayList<byte>();
And it does not work.
It says this:
The non-generic type 'System.Collectios.ArrayList' cannot be used with
type arguments
What is the proper way to do declare a byte ArrayList?
You are confusing Java ArrayList collections with C# List generic collections. Both are used to declare collections, but the first one is used in Java as being a type defined in the generic class List for Collections framework and in the last one is used in C# language as an implicit generic type.
So, you must declare as being a List type. See details on List.
var mahByteArray = new List<byte>();
or
List<byte> mahByteArray = new List<byte>() { 2, 3, 4 };
sure you can use a ArrayList
var mahByteArray = new ArrayList();
mahByteArray.Add((byte) 230);
ArrayList<> isn't generic. You can use generic List<> instead
var mahByteArray = new List<byte>();
ArrayList is not generic. Use System.Collections.Generic.List<T> instead. The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.
var mahByteArray = new List<byte>();
Also take a look at this: Difference between ArrayList and Generic List.
Related
I have defined a new class type called Hotspot. I need 2 dynamic array of Hotspot (I used List) and a third one that allow me to "switch" between them. Here my code:
List<Hotspot> items = new List<Hotspot>();
List<Hotspot> locations = new List<Hotspot>();
Hotspot[][] arrays = new Hotspot[][]{items, locations};
but arrays doesn't work. I just need it so I can easily access to items/locations array.
In F# I did it in this way:
let mutable items = new ResizeArray<Hotspot>()
let mutable locations = new ResizeArray<Hotspot>()
let arrays = [|items; locations|]
but I can't do the same thing in C#. Some help?
List<Hotspot>[] arrays = new List<Hotspot>[]{items, locations};
items and locations are declared (and instantiated) as lists. Lists are not arrays and you're trying to assign them as arrays. Convert them to arrays or don't use arrays at all but a list instead.
Hotspot[][] arrays = new Hotspot[][]{ items.ToArray(), locations.ToArray() };
//or
List<Hotspot>[] lists = new[] { items, locations };
p.s., The F# ResizeArray<T> is essentially an alias to the .NET List<T>. So in effect, the arrays variable in your F# example is equivalent to lists in my example above, you created an array of lists.
I have to declare a list and use it in my code.How ever the number of elements that i will add to list will vary during each time I run my code.So how can I create a list and add elements to it dynamically with out specifying its size during declaration?
var myList = new List<string>();
myList.Add("foo");
myList.Add("blah");
// and on and on ...
List's in .Net will automatically resize themselves as you add to them.
You don't have to specify the bounds of a list (as you do with arrays). You can keep on calling Add() method to add elements in the list. You can create either a generic list which takes only specified types of objects and a non-generic list that only takes objects:
Generic:
List<int> intList = new List<int>();
intList.Add(10);
intList.Add(20);
Non-Generic:
ArrayList objList = new ArrayList();
objList.Add(New Employee());
objList.Add(20);
objList.Add("string");
The later can take any type of object but is not type-safe.
The System.Collection namespace is full of collection classes that can dynamically contract and expand its size, see the Generic namespace for the most used classes: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx
I recommend sticking with a List if you doubt what you are doing:
var list = new List<string>();
list.Add("test1");
list.Add("test2");
list.Remove("test1");
Can I define an array such that the first element is String, the Second is an int and the third is a textbox?
It's like when we create a List we choose type of element List<string >
Update from Comment:
Sorry I couldnt explain.I need to like
this List<string,int,object> Firstly i
will set type and when i call the list
i will not need to cast
thanks
create list of objects. in C# everything is derived from object
List<object> list = new List<object> {"first", 10, new TextBox()};
EDIT(To comment):
Then you should create seperate class to hold those three items , or use Tuple
List<Tuple<string,int,TextBox>> list;
You can declare an array of object and do that. You're talking about a mixed type array, right?
var arr = new object[] { "Hi", 42, 3.7, 'A' }
If you need an array that has elements without a common base-class other than object, then you're going to need an array of objects!
object[] myArray = new object[] { "Hi", 23, new TextBox() };
Note that this is not really something you should doing. If you need to associate disparate types like this, a class makes much more sense.
You want a Tuple<string,int,TextBox>, not an array.
IMHO the best way to do this is through a List<> of objects:
String s = "hey!";
int i = 156;
TextBox t = new TextBox();
List<object> list = new List<object>(3);
list.Add(s);
list.Add(i);
list.Add(t);
The reason this works is because (almost?) everything in C# derives from the base-class object
Arrays are typically homogeneous collections, which means that every object contains in the array is of the same type (or at least shares a common parent type). An array of [string, int, textbox] could be defined as an object[] but that's really misuse of arrays.
Just create a proper class which contains the 3 fields.
class MyType {
public string myString;
public int myInt;
public Listbox myListbox;
}
If you're looking make a list of string, int, textbox, you can either create a class which has those members or look at the Tuple class in .net 4.0
List<Tuple<string,int,TextBox>
Define a class that contains the 3 types then define an array that contains the new type.
Object[] myObjects = new Object(){"myString", 42, textbox1};
System.Collections.Generic.Dictionary<string, object> source = new System.Collections.Generic.Dictionary<string, object>();
source.Add("A", "Hi");
source.Add("B", 10);
source.Add("C", new TextBox());
While accessing
string str = Convert.ToString(source["A"]);
int id = Convert.ToInt16(source["B"]);
TextBox t = (TextBox)source["C"];
I will suggest that you create a Type such as
enum ItemType { Int, String, Textbox }
class MyType {
public object objValue;
public ItemType itemType;
}
List<MyType> list = new List<MyType>();
.......
You can iterate through the list or extract the list by type such as below.
var intList = list.Where(e=>e.itemType == ItemType.Int);
Of course you can achieve the above with the enum and using the reflected Type info directly from the object, but I just think it is clearer this way also more explicitly list out the type your list can hold rather than just all type in the CLR
it is possible to store data in of two arraylist into <list>?
here's my code with two arrays that will merge:
ArrayList arrPrices = new ArrayList();
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
ArrayList arrDetails = new ArrayList();
List<StockInfoDetails> lstStockInfoDetails = new List<StockInfoDetails>();
Util oUtils = new Util();
arrPrices = oUtils.GetDetails(SymbolIndex);
You can do it with linq simply:
lstStockInfoPrice.AddRange(arr1.Cast<StockInfoPrice>());
lstStockInfoPrice.AddRange(arr2.Cast<StockInfoPrice>());
See Cast in IEnumerable.
It is possible.
You could try the following if oUtils.GetPrices(SymbolIndex) returns StockInfoPrice;
lstStockInfoPrice.AddRange(oUtils.GetPrices(SymbolIndex));
I this Util class isn't your own, then you're stuck with Marius' answer. However, if you control that Util class then you could make the GetPrices and GetDetails methods return someting with type IEnumerable and IEnumerable respectively.
Then, you can add the whole lot to another list with List.AddRange() method.
As an aside, your allocation in the declaration of arrPrices is a waste of time - the allocated object is never used and will then be subject to garbage collection.
Your GetPrices() method returns an ArrayList - ie, a new arrayList, and
arrPrices = oUtils.GetPrices(SymbolIndex);
simply makes arrPrices refer to the new list. There are then no references to the one you allocated when you declared arrPrices, so it's thrown away.
Do it like this:-
ArrayList arrPrices;
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
If you want to move the value from arrPrices to lstStockInfoPrice and lstStockInfoDetails, you could iterate over the array list and put the elements in the list. Something like this:
foreach(var o in arrPrices)
{
lstStockInfoPrice.Add(o); // or Add((StockInfoPrice)o)
}
I'm looking for something similar to List<T>, that would allow me to have multiple T. For example: List<TabItem, DataGrid, int, string, ...> = new List<TabItem, DataGrid, int, string, ...>().
If you are using .NET 4, you could have a List<Tuple<T1, T2, ...>>
Otherwise, your choice is to implement your own type.
Create a class that defines your data structure, and then do
var list = new List<MyClass>();
Normally you'd just have List<MyClass> where MyClass had all those other ones as members.
If it can have any old type, then you need to use an ArrayList.
If you know ahead of time what you'll have in there, then you should either create your own structure, or use a Tuple.
Looks like you're after List<object>?
Tuples are best if you are using .net 4.0. But if you are working 3.5 or below, multidimensional object array is good. Here is the code. I have added 3 different types in a object array and I pushed the same to list. May not be the best solution for your question, can be achieved with object array and list. Take a look at the code.
class Program
{
static void Main(string[] args)
{
object[,] OneObject = new object[1,3]{ {"C Sharp",4,3.5 }};
List<object> MyList = new List<object>();
MyList.Add(OneObject);
object[,] addObject = new object[1,3]{{"Java",1,1.1}};
MyList.Add(addObject);
foreach(object SingleObject in MyList)
{
object[,] MyObject = (object[,])SingleObject;
Console.WriteLine("{0},{1},{2}", MyObject[0, 0], MyObject[0, 1], MyObject[0, 2]);
}
Console.Read();
}
}
Instead of trying in C# 4, you can give the old version features a chance here.
It seems you don't need a strongly typed collection here, in that case ArrayList is the best option.