C
C#16mo ago
Schenk

✅ Convert List to IList

How do I convert a List to an IList?
18 Replies
jcotton42
jcotton4216mo ago
List<T> implements IList<T> you can just cast it, or pass it to a method that expects IList<T>, no conversion needed
Schenk
SchenkOP16mo ago
but i get an error when i try to assign my a List<T> to an IList<T>, how do i get around this issue
jcotton42
jcotton4216mo ago
show the $code and the error
MODiX
MODiX16mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Schenk
SchenkOP16mo ago
here is the codesnippit
contextMessages.Add(newMessage);;

var openAiResponse = await openAiClient.GetChatCompletionsAsync(openAIModel, new ChatCompletionsOptions
{
Messages = contextMessages // DOES NOT WORK WITH contextMessages FIX!!!!!!!!!!
});
contextMessages.Add(newMessage);;

var openAiResponse = await openAiClient.GetChatCompletionsAsync(openAIModel, new ChatCompletionsOptions
{
Messages = contextMessages // DOES NOT WORK WITH contextMessages FIX!!!!!!!!!!
});
the error is: Error CS0200 Property or indexer 'ChatCompletionsOptions.Messages' cannot be assigned to -- it is read only
jcotton42
jcotton4216mo ago
that's not a List vs IList issue that's the property not having a setter presumably you're meant to call Add or AddRange on Messages instead of reassigning it
Schenk
SchenkOP16mo ago
I can't call AddRange as it does not exits, and i cant ude Add as i need to a whole lot of old things to it
Denis
Denis16mo ago
Don't hesitate to read the error message. It literally says, that what you are trying to set is readonly - you cannot assign to it Addrange is basically calling add in a loop
Schenk
SchenkOP16mo ago
Yes, but this works
var openAiResponse = await openAiClient.GetChatCompletionsAsync(openAIModel, new ChatCompletionsOptions
{
Messages = { new ChatMessage(role, prompt) }
});
var openAiResponse = await openAiClient.GetChatCompletionsAsync(openAIModel, new ChatCompletionsOptions
{
Messages = { new ChatMessage(role, prompt) }
});
Okay, then I will make my own, but why can i assign a new list and not a variable
Denis
Denis16mo ago
Are you sure you need to assign to the messages property Click on the messages property and press F12, show a screenshot of what appears
Schenk
SchenkOP16mo ago
this?
Denis
Denis16mo ago
Yes. This clearly shows that you do not set messages via the property, as it only has a get This explains the read-only error You supply the messages via the constructor Or, I would assume you store your ChatCompletionOptions in a separate variable and access its Messages property to directly add your messages there
var options = new ChatCompletionOptions();

options.Add(......);
var options = new ChatCompletionOptions();

options.Add(......);
Schenk
SchenkOP16mo ago
ahh, thats clever, no idea why i didn't think of that
Denis
Denis16mo ago
And then provide the options to your GetChatCompletionOptions method
Schenk
SchenkOP16mo ago
yes, that makes so much more sense, thank you so much
Denis
Denis16mo ago
Welcome, try it out It might happen that the mesaages are null But hopefully that is not the case If it is null, then you can provide the messages via the constructor As seen in the screenshot you've shared This Oh no, it is internal Then maybe there is a different constructor available
Schenk
SchenkOP16mo ago
okay, great, thank you so much again, it seems that it works to just save the GetChatCompletionOptions
Denis
Denis16mo ago
Consider closing this thread using /close if you have no further questions
Want results from more Discord servers?
Add your server