Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
FunctionThatReturnsAList(cmd)[0]
It is short-hand for:
List<Whatever> list = FunctionThatReturnsAList(cmd);
Whatever whatever = list[0];
The return type of FunctionThatReturnsAList is an object, like a List or an array that can be accessed via an indexer. The code is calling the function, which is then returning the List or array and then using the indexer to reference the first element in the collection.
An example would be:
var cmd = "123";
var returnedObj = FunctionThatReturnsAList(cmd)[0];
private List<string> FunctionThatReturnsAList(cmd)
{
return new List<string> {cmd};
}
The function returns a list, and you just access element 0 in the returned list.
Seems like cmd is an SQL command which returns may be array of some kind like DataTable[] and this function gets only first element(DataTable) from the array.
This statement can be used for all methods whose return type has a numeric indexer (e.g. lists or arrays).
Related
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 7 years ago.
Improve this question
I am making a game where there are squares (sectors) generated to make a path for a ball to go. There are two types of sectors: Sector and Presector. They are all put into a list of type Sector. How would I check to see if a specific sector in that list was actually a Presector before it was put in?
BTW: Presector is a child class of Sector.
I looked all over the place and couldn't find anything. The as keyword isn't working for me, and Type.IsAssignableFrom isn't either. EDIT: is will not work either, since that just checks if an object is that type.
SAMPLE CODE TIME!
List<Sector> sectors = new List<Sector>();
sectors.Add(new Sector());
sectors.Add(new Presector());
Now, we have a list full of two sectors. The second one was casted. How do I find that out using code?
if (objectFromList is Presector)
// Code here..
List<Sector> sectors = new List<Sector>();
sectors.Add(new Sector());
sectors.Add(new Presector());
sectors.Add(new Sector());
Presector ps = new Presector();
sectors.Add(ps);
// this returns an array with one element
var x = sectors.OfType<Presector>().ToArray();
// this returns true (the second element IS a Presector)
var hasPresector = sectors.Any(s => s is Presector);
// this returns true (the presector is present in the list)
var containsPs = sectors.Contains(ps);
What's the problem with the 'is' keyword?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a program where you insert some numbers in a listbox and then a button click should get the values from the listbox and check whether they are positive or negative and display each other's count in a textbox.
I tried getting the value by: string x = listBox1.Items[index].Value; but it doesn't seem to work.
If you add the items to the list box as such:
listBox1.Items.Add(textBox1.Text);
Then you can retrieve the item from given index as follows:
string x = listBox1.Items[index];
The indexer is returning the value, which in that case is a string. Probably you might need to cast it to string, because the indexer actually returns object - see here: ListBox.ObjectCollection.Item Property :
string x = (string)listBox1.Items[index];
You can also try
string s = (string)listbox1.Items.GetItemAt(index);
You have to cast it to string because it's returning an object
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I get a count of all items in a Mongo collection, using the 2.x C# driver?
I'm trying to use CountAsync, which I need to pass in a filter. I don't want to filter - I want everything returned.
You can always pass the empty document in the method like this
db.collection.CountDocumentsAsync(new BsonDocument());
db.collection.count()
In C# you can use Count() on the cursor of your collection.
Try as below:
var mongo = new Mongo();
mongo.Connect();
var db = mongo.GetDatabase("DatabaseName");
var collection = db.GetCollection<Product>();
var totalCount= collection.Count();
you can check in the following URL
this url
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've been going through this for hours now and just can't find a solution. I need to make a class that takes it's input like this:
Collection collection = new Collection(3);
collection("RandomNumber") = 123;
collection("DecimalNumber") = 456.78;
collection("Text") = "Hello world!";
The problem is whatever I try I won't get it working so that it would set and return all the values as supposed. Mostly I end up getting some errors that tell me that something cannot be converted to a string (while trying value.ToString() for an example).
All in all what I've done isn't that important because it didn't work, so can someone help me?
Perhaps you want to use a Dictionary<string, object>:
Dictionary<string, object> collection = new Dictionary<string, object>(3);
collection["RandomNumber"] = 123;
collection["DecimalNumber"] = 456.78;
collection["Text"] = "Hello world!";
Console.WriteLine(collection["RandomNumber"]); // 123
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can not make this work. What should i do to make it work ? Thank you
public static List<int> lstAllMonsterIds = new List<int>();
using (DataTable dtTemp = DbConnection.db_Select_DataTable("select MyId,Name from myTable"))
{
lstAllMonsterIds = dtTemp.AsEnumerable().ToList(dtr => Convert.ToInt32(dtr.Field<Int16>("PokemonId").ToString()));
}
You need List<int> back, then select the field using int, and don't call ToString on it, and in the end call ToList
lstAllMonsterIds = dtTemp.AsEnumerable()
.Select(dtr => dtr.Field<int>("MyId"))
.ToList();
If your field is of type Int16, then it can be implicitly casted to int or Int32, you don't have to call ToString on it and then Convert it to Int32