C
C#2y ago
Sheraf

❔ Hello I am new to C#, I dont understand this error:

I have a insert method that adds to queue:
_insertQueue.Enqueue(new object[] { database, collection, document });
_insertQueue.Enqueue(new object[] { database, collection, document });
and then in a thread loop:
while(_insertQueue.Count > 0)
{
var items = _insertQueue.Dequeue();
var database = items[0];
var collection = items[1];
var document = items[2];
var db = _mongoClient.GetDatabase(database);
var col = db.GetCollection<BsonDocument>(collection);
col.InsertOne(document);
}
while(_insertQueue.Count > 0)
{
var items = _insertQueue.Dequeue();
var database = items[0];
var collection = items[1];
var document = items[2];
var db = _mongoClient.GetDatabase(database);
var col = db.GetCollection<BsonDocument>(collection);
col.InsertOne(document);
}
but I'm getting this error:
error CS0021: Cannot apply indexing with [] to an expression of type 'object'
error CS0021: Cannot apply indexing with [] to an expression of type 'object'
35 Replies
Sheraf
SherafOP2y ago
So im trying to cast items as a list but I can't figure it out Ok this seem to work:
while(_insertQueue.Count > 0)
{
var items = (ArrayList)_insertQueue.Dequeue();
string database = (string)items[0];
string collection = (string)items[1];
BsonDocument document = (BsonDocument)items[2];
var db = _mongoClient.GetDatabase(database);
var col = db.GetCollection<BsonDocument>(collection);
col.InsertOne(document);
}
while(_insertQueue.Count > 0)
{
var items = (ArrayList)_insertQueue.Dequeue();
string database = (string)items[0];
string collection = (string)items[1];
BsonDocument document = (BsonDocument)items[2];
var db = _mongoClient.GetDatabase(database);
var col = db.GetCollection<BsonDocument>(collection);
col.InsertOne(document);
}
is that the right way?
Pobiega
Pobiega2y ago
What is your _insertQueue declared as? Never use ArrayList
Sheraf
SherafOP2y ago
private Queue _insertQueue = new Queue(); Error: Specified cast is not valid.
Pobiega
Pobiega2y ago
yeah its an untyped queue
circles.png
circles.png2y ago
dont cast to arraylist
Pobiega
Pobiega2y ago
you need to use the generic version Queue<object[]>
Sheraf
SherafOP2y ago
Sheraf
SherafOP2y ago
Not letting me ok
Pobiega
Pobiega2y ago
you need to update your imports ^
Sheraf
SherafOP2y ago
private Queue<object[]> _insertQueue = new Queue<string, string, BsonDocument>(); I added the import
circles.png
circles.png2y ago
wrong type argument should be object[] right
Pobiega
Pobiega2y ago
you can't have type argument mismatches also, I dont think queue takes 3 type arguments which is why Diddy suggested a tuple (T1, T2, T3) or a custom class
Sheraf
SherafOP2y ago
private Queue<(string, string, BsonDocument)> _insertQueue = new Queue<(string, string, BsonDocument)>(); this makes sense?
Pobiega
Pobiega2y ago
sure I'd probably use a record myself, but this is fine
Sheraf
SherafOP2y ago
and then :
public void Insert(string database, string collection, BsonDocument document)
{
_insertQueue.Enqueue((database, collection, document));
}
public void Insert(string database, string collection, BsonDocument document)
{
_insertQueue.Enqueue((database, collection, document));
}
Pobiega
Pobiega2y ago
yeah, seems okay at a glance
Sheraf
SherafOP2y ago
this
Sheraf
SherafOP2y ago
:/
Pobiega
Pobiega2y ago
you can't access it with an index accesser
circles.png
circles.png2y ago
unpack the tuple
Sheraf
SherafOP2y ago
var (database, collection, document) = _insertQueue.Dequeue(); ?
circles.png
circles.png2y ago
var database, collection, document = items or inline that
Sheraf
SherafOP2y ago
Not used to all these types, I come from python :x
circles.png
circles.png2y ago
same, but a long time ago
Pobiega
Pobiega2y ago
time to unlearn your bad habits 🙂
Sheraf
SherafOP2y ago
I kinda like it tho, i feel like it makes things more clear or forces you to write clean code
circles.png
circles.png2y ago
it is definitely safer (harder to write bugs)
Pobiega
Pobiega2y ago
the good thing with types is that after some time, you revisit this code, you don't need to go "uhh, what the hell do I actually write to that queue.. let me look" you can just look at the type declaration oh, its a (string database, string collection, BsonDocument document)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Sheraf
SherafOP2y ago
No clue i just started using mongo and csharp
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Sheraf
SherafOP2y ago
Yeah i should grab the database and the collection before the loop obviously This is pure testing at this point
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Sheraf
SherafOP2y ago
Thanks for the help guys!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server