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 returnsFalse
. 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
Remove Duplicates from a List of Objects Based on Property in Java 8
Save Child Objects Automatically Using JPA Hibernate
How to Identify File Type by Base64 Encoded String of a Image
Better Way to Detect If a String Contains Multiple Words
How to Have Each Record of Json on a Separate Line
How to Convert Date Which I Got from Firebase Server as Am Getting Error Date
Java 8: How to Write Lambda Stream to Work With Jsonarray
The Group Coordinator Is Not Available-Kafka
Generate 6 Digit Random Number
In Java, Can You Modify a List While Iterating Through It
Java How to Retrieve More Than 1 Million Rows in a Resultset
How to Post a Json Payload to a @Requestparam in Spring MVC
How to Make the Icon Background of an Android App Transparent
How to Return a HTML Page from a Restful Controller in Spring Boot
Flush/Clear System.In (Stdin) Before Reading
Transactionrequiredexception Executing an Update/Delete Query