I Cannot Add in a List

can not add elements in list (in C#)

On every MouseDown you declare and initialize your list - so you need to move it outside of this method - so that it doesn't get destroyed after the scope of this function ends and it doesn't get overwritten by reinitialization.

When you declare a variable inside a scope - in this case a scope of pictureBox1_MouseDown method it gets destroyed once the function finishes.

Additionally, if you were to declare a variable outside a function but initialize it inside the function like so:

List<String> myPoints;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
myPoints = new List<String>();
textBox3.Text = e.X + " , " + e.Y;
myPoints.Add(textBox3.Text);
}

It would not be destroyed, but it's contents would be erased, so you'd end up with an empty list. So you need to do it like so:

List<String> myPoints = new List<String>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
textBox3.Text = e.X + " , " + e.Y;
myPoints.Add(textBox3.Text);
}

Why can't I add objects to my List<>?

You should initialize your list first:

if(p.Books == null)
p.Books = new List<Book>();

It is more appropriate to do it in your clsPerson class constructor.

How to add to an existing list - got Unsupported operation: Cannot add to an unmodifiable list

Note: if messages was final you could still add items to that list. The problem is that messages is being assigned to the default const[] in your constructor.

Instead of:

chat.messages.add(message);

Try this:

chat.messages = [...chat.messages, message];

... is the Spread Operator. This line will create a new list with message at the end.

You could also use List.from() combined with ..:

chat.messages = List.from(chat.messages)..add(message);

Here, .. is the Cascade Notation. It will add message to the new list but the list will be returned instead of the result of the add method (that would be void).

You could also initialize the messages with a non const []:

class Chat {
final String uuid;
final User receiverUser;
List<Message> messages;

Chat({this.uuid, this.receiverUser, List<Messages>? messages})
: messages = messages ?? [];
}

Cannot add tuple to list

Tuples should not be used in "production code" unless you deal with CLI or some other you get your input from some other language.

Because of Generics limitations in C# Tuples can have at most 8 generic values. And as per documentation: The last element of an eight element Tuple must be a Tuple.

To remove this error, convert this tuple to class, or convert your tuple so it uses a Tuple as a last element

I can't add an int to a list

Two things are wrong with your code:

  • You are doing a list == [] which returns a True or False since == is a comparison operator. In this case it returns False. You need to use = to initialize a variable.
  • list is the name of a built-in type in python, use something else as your variable name.

Fixing both of them :

alist = []

def MultiplesNumber(a):
for i in range(1, a+1):
if a % i == 0:
return i

alist.append(MultiplesNumber(100))

gives the correct output.



Related Topics



Leave a reply



Submit