I currently have a string array that I need to perform operations on(the contains() method would be very helpful ) and I'd like to use the list structure. Is it possible to initilize a list in VS 2005 with string values in it? I can't get data in without using the add() method.
3.0 apparently would allow: - List<string> stuff = new List<string> { "Entry", "Something", "Another" };
but 2005 won't let me initalize like that. I have a lot of entries and it's silly to add() every one.
I've tried declaring: - List<string>stuff = new List<string>();
-
stuff = {"this", "that", "other"};
which doesn't work either.
Thanks for any help!
10 31985
I currently have a string array that I need to perform operations on(the contains() method would be very helpful ) and I'd like to use the list structure. Is it possible to initilize a list in VS 2005 with string values in it? I can't get data in without using the add() method.
3.0 apparently would allow: - List<string> stuff = new List<string> { "Entry", "Something", "Another" };
but 2005 won't let me initalize like that. I have a lot of entries and it's silly to add() every one.
I've tried declaring: - List<string>stuff = new List<string>();
-
stuff = {"this", "that", "other"};
which doesn't work either.
Thanks for any help!
VB - Dim sList() As String = {"a", "b", "c"}
-
Dim oList As List(Of String)
-
oList.AddRange(sList)
C# - string[] sList = {"a", "b", "c"};
-
List<string> oList = new List<string>();
-
List.AddRange(sList);
Hope that helps
Thanks for the help. I managed to get it to work also as: - string[] myString = {"a", "b", "c"};
-
List<string> myList = new List<string> (myString);
I'm still interested to hear if theres a way to bypass having to declare a string array and put the data directly into the list if anyone has any input.
Thanks for the quick reply.
Thanks for the help. I managed to get it to work also as: - string[] myString = {"a", "b", "c"};
-
List<string> myList = new List<string> (myString);
I'm still interested to hear if theres a way to bypass having to declare a string array and put the data directly into the list if anyone has any input.
Thanks for the quick reply.
I figured it out:
VB - Dim myList As New List(Of String)(New String() {"a","b","c"})
C# - List<string> myList = new List<string>(new string[]{"a","b","c"});
It's not quite as tidy as one might hope - but this is how to do it in one line. Sadly it still requires a string array as the new List<t> doesn't have any overloads that allow for a parameter array which means you have to create an IEnumerable (array in this instance) to pass. But at least the array is an anonymous type now which cuts down on code.
The List<t> class only can only be instanciated with overloads of the following signatures
new List<t>();
new List<t>(int Capacity);
new List<t>(System.Collections.Generic.IEnumerable(t)) ;
What about : -
List<string> stuff = new List<string>(new string[] { "thing1", "thing2", "cat in the hat" });
-
EDIT: oops, that will teach me to only read the first post before responding.
What about: -
List<string> stuff = new List<string>(new string[] { "thing1", "thing2", "cat in the hat" });
-
EDIT: oops, that will teach me to only read the first post before responding.
Better late than never Plater ;) A Dr. Seuss fan I see, hehe.
Thanks for the help. I managed to get it to work also as: - string[] myString = {"a", "b", "c"};
-
List<string> myList = new List<string> (myString);
I'm still interested to hear if theres a way to bypass having to declare a string array and put the data directly into the list if anyone has any input.
Thanks for the quick reply.
It wouldn't be possible to have such a constructor for List.
What would it look like? Remember it has to be generic as well.
It wouldn't be possible to have such a constructor for List.
What would it look like? Remember it has to be generic as well.
There is, why they didn't include some form of this wrapper I don't know.
Although I suppose making it:
public List<object> MakeList(params object[] items)
defeats the purpose of having a List<T> -
public void testing()
-
{
-
List<string> = MakeList("green", "eggs", "and ham");
-
}
-
public List<string> MakeList(params string[] items)
-
{
-
return new List<string>(items);
-
}
-
There is, why they didn't include some form of this wrapper I don't know.
Although I suppose making it:
public List<object> MakeList(params object[] items)
defeats the purpose of having a List<T> -
public void testing()
-
{
-
List<string> = MakeList("green", "eggs", "and ham");
-
}
-
public List<string> MakeList(params string[] items)
-
{
-
return new List<string>(items);
-
}
-
you mean MakeList(T[] items), to make it generic? That is covered with the constructor that takes an enumerable. Making it take an array rather limits it a bit since they would need separate ones for other enumerables. It's that case of coding to the Interface rather than to the implementation again.
But I have a heavy dislike of non-indexed enumerables :-P
But I have a heavy dislike of non-indexed enumerables :-P
Don't get me started on that.
Anyway, I could create my own class and implement the IEnumerable interface and it would work nicely with the .NET list. That's the benefit of coding to the interface.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: anongroupaccount |
last post by:
class CustomType
{
public:
CustomType(){_i = 0;}
CustomType(int i) : _i(i) {}
private:
int _i;
};
class MyClass
|
by: JAL |
last post by:
Here is my first attempt at a deterministic collection using Generics,
apologies for C#. I will try to convert to C++/cli.
using System;
using System.Collections.Generic;
using System.Text;
...
|
by: Anders Borum |
last post by:
Hello!
Whilst refactoring an application, I was looking at optimizing a
ModelFactory with generics. Unfortunately, the business objects created by
the ModelFactory doesn't provide public...
|
by: toton |
last post by:
Hi,
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
Test x = {Test(3),Test(6)}; using array initialization list. (Note
Test do NOT have a...
|
by: semedao |
last post by:
Hi,
I want to implement list of key-values that can be sort by 2 ways.
let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
|
by: rkausch |
last post by:
Hello everyone,
I'm writing because I'm frustrated with the implementation of C#'s
generics, and need a workaround. I come from a Java background, and
am currently writing a portion of an...
|
by: Jon Slaughter |
last post by:
I created a generic class and I want to pass it a enum,
class GObject<SomeType>
{
...
public SomeType q = SomeType.Default;
...
}
|
by: =?Utf-8?B?Y2hyaXNiZW4=?= |
last post by:
Here are the codes
Queue<string> q = new Queue<string>;
Console.WriteLine(q.Count + "");
The program will crash since q is null.
So my question is that what is the best way for new to...
|
by: SimonDotException |
last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |