C
C#6mo ago
MorganIGuess

MVVM How to make observablecollection<string> always contain a string

I am trying to figure out how do I make an observablecollection of type string always contain a string without manually listing strings. I want the user to be able to click on a button and type in their own string, but allow the observablecollection to automatically detect a string inside the collection of buttons
124 Replies
Jimmacle
Jimmacle6mo ago
there's no shortcut, if you want to make sure there's always at least 1 string then you need to add logic to enforce that like in the operation that removes items from the collection, check if it's empty and if so add your default one back
MorganIGuess
MorganIGuessOP6mo ago
I did that, I type in a name whenever I add a new item into the collection, and for some reason it never has a string in it. if (!Items.Contains(string) then add something, and it always adds something because of it
Jimmacle
Jimmacle6mo ago
would have to see code
MorganIGuess
MorganIGuessOP6mo ago
Okay I'll post it here
MorganIGuess
MorganIGuessOP6mo ago
GitHub
Notepad-Bugged-with-breakpoint-/ViewModel/MainViewModel.cs at maste...
A randomly generated code keeps popping up and will not let me use the app anymore - Morganiscooler/Notepad-Bugged-with-breakpoint-
MorganIGuess
MorganIGuessOP6mo ago
this is the viewmodel im using that's connected to the page I want to delete items from
Jimmacle
Jimmacle6mo ago
i don't see any logic here that will ensure that there's always 1 item in the collection and there isn't 1 item in the collection to begin with
MorganIGuess
MorganIGuessOP6mo ago
How so?
Jimmacle
Jimmacle6mo ago
there just... isn't? in your delete command i would expect some logic to check if there are no items left and add one back, or prevent removing a specific one
MorganIGuess
MorganIGuessOP6mo ago
I don't want to add a new item when I delete something. How can I make sure the item I'm adding in the first place counts as an item in the collection?
Jimmacle
Jimmacle6mo ago
i'm getting really confused about what you're trying to do if you add a string to the collection, it's added there's nothing else to it
MorganIGuess
MorganIGuessOP6mo ago
im confused on why the items im creating don't count as items in the collection and im trying to make them items in the collection
Jimmacle
Jimmacle6mo ago
when you use the debugger do you see that the collection contains the items you're adding? Items.Add(Text); adds an item, that's all you have to do to make it "count"
MorganIGuess
MorganIGuessOP6mo ago
No it doesn't contain a string it says Unless I make string = Text, which isn't what I want to do because the Entry resets
Jimmacle
Jimmacle6mo ago
Unless I make string = Text i don't know what this means or what an entry is
MorganIGuess
MorganIGuessOP6mo ago
Text is binded to an entry box every single time I go to create a new note, the entry box is blank or changed to something else, since this is the name of the note and not all notes are the same name which makes deleting previous notes impossible I am trying to work around this, I want the collection to recognize any string as an item abc could be an item, hello123 can be another item etc
Jimmacle
Jimmacle6mo ago
I want the collection to recognize any string as an item i'm not playing dumb, but what does this mean the collection doesn't "recognize" strings it just has strings in it, if you want to remove one then pass the same text to Remove and it will be gone
MorganIGuess
MorganIGuessOP6mo ago
How do I get it to recognize the same text every time?
Jimmacle
Jimmacle6mo ago
what does recognize mean in this context?
MorganIGuess
MorganIGuessOP6mo ago
like know the text is in there so it can be removed
Jimmacle
Jimmacle6mo ago
by doing nothing, because it already does that?
MODiX
MODiX6mo ago
Jimmacle
REPL Result: Failure
var collection = new ObservableCollection<string>();
collection.Add("My Item");
collection.Remove("My Item");
Console.WriteLine(collection.Count);
var collection = new ObservableCollection<string>();
collection.Add("My Item");
collection.Remove("My Item");
Console.WriteLine(collection.Count);
Exception: CompilationErrorException
- The type or namespace name 'ObservableCollection<>' could not be found (are you missing a using directive or an assembly reference?)
- Argument 1: cannot convert from 'method group' to 'System.ReadOnlySpan<char>'
- The type or namespace name 'ObservableCollection<>' could not be found (are you missing a using directive or an assembly reference?)
- Argument 1: cannot convert from 'method group' to 'System.ReadOnlySpan<char>'
Compile: 447.256ms | Execution: 0.000ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle6mo ago
sad modix
MorganIGuess
MorganIGuessOP6mo ago
The item fluxtuates though like one name could be blah another one could be hi Is there a way to get it to be the same string regardless?
Jimmacle
Jimmacle6mo ago
...huh? i'm sorry but i'm totally lost on what you want can you explain it in plain english, no code? what do you want to happen from the user's perspective
MorganIGuess
MorganIGuessOP6mo ago
I'll try User perspective: As an user, I want to be able to create a note with any given name, but I want to be able to delete any of the notes without forcing them to be the same name every single time
Jimmacle
Jimmacle6mo ago
so you have some kind of textbox that holds the name they want to delete yeah?
MorganIGuess
MorganIGuessOP6mo ago
Oh yeah
Jimmacle
Jimmacle6mo ago
so just take that text and remove it from the collection i don't understand where "forcing them to be the same name" is coming from, because if that's happening that's an issue in your own code
MorganIGuess
MorganIGuessOP6mo ago
How do i contain the same text they input
Jimmacle
Jimmacle6mo ago
...it would already be there you're trying to remove an existing item from the collection correct?
MorganIGuess
MorganIGuessOP6mo ago
yes
Jimmacle
Jimmacle6mo ago
yeah so take the text from the user's input, and call collection.Remove(theUsersText)
MorganIGuess
MorganIGuessOP6mo ago
Hmm ok So in this case should I like, not make it binded to the same spot because the user will change the Text when they make their second note I need the text to remain the same so it's deletable
Jimmacle
Jimmacle6mo ago
that doesn't sound related to this what? i'm going to do something else for 5 minutes and give you some time to thorougly explain your project and what needs to happen here
MorganIGuess
MorganIGuessOP6mo ago
The user creates a new note, then creates another note there should be 2 items in the collection at this point we should be able to delete any of the 2 items in the collection But the problem is, I can't delete of any of the 2 items. I can only delete items that contain the same string, like "hi" and "hi" because it is the same as the string Text that the user types in when creating a note But as soon as they change the Text to something else, "hi" can no longer be deleted I don't know how to get the collection to contain all the names of the items the user creates, so that any of the items can be deleted
Jimmacle
Jimmacle6mo ago
does the collection not just contain names right now?
MorganIGuess
MorganIGuessOP6mo ago
I'll get a recording of this problem if this doesn't make sense give me a few minutes
Jimmacle
Jimmacle6mo ago
or are you trying to put the title and contents of a note in the same string
MorganIGuess
MorganIGuessOP6mo ago
Yeah it only contains one name at a time
Jimmacle
Jimmacle6mo ago
that's not what i asked
MorganIGuess
MorganIGuessOP6mo ago
It does contain the name you give the note, yes I need to brb I'll upload a video of my problem I'm doing this actually the more I think about it It contains the name on the cover, with a title and is supposed to contain data within the note when navigating to it
Jimmacle
Jimmacle6mo ago
then it sounds like you need a more complex type to represent your note like a Note class with Title and Body strings
MorganIGuess
MorganIGuessOP6mo ago
MorganIGuess
MorganIGuessOP6mo ago
This is what happens when I use delete the notes based off the Text
MorganIGuess
MorganIGuessOP6mo ago
here's what happens when I click on it (it navigates to a different page)
No description
MorganIGuess
MorganIGuessOP6mo ago
So I can't just remove a note if it contains any of the names in the collection?
sibber
sibber6mo ago
what does the code for the delete button look like what does that mean its better to give each note an id and delete by that id so you can have multiple notes with the same text
MorganIGuess
MorganIGuessOP6mo ago
I've been thinking about an Id system But I have no idea how I would be able to implement it
sibber
sibber6mo ago
is my discord bugged or is there a giant blank space oh its gone anyway lol think about it thats what programming is :) thinking about how to implement stuff
MorganIGuess
MorganIGuessOP6mo ago
The person said that I need to make a more complex class in order to specifically delete notes However, I thought the solution would just be deleting an item if it contained a name, like a string "abc"
sibber
sibber6mo ago
making a note object is a good idea yes because a note isnt just the text in the note
MorganIGuess
MorganIGuessOP6mo ago
I was thinking "why not just store all the strings the user types, and if any of the items contain any of those strings previously written, then make those items deletable"
sibber
sibber6mo ago
contained? not equals?
MorganIGuess
MorganIGuessOP6mo ago
What's the difference? The tutorial I watched used contain, but I don't get why equals would be used
sibber
sibber6mo ago
you want to delete all notes that contain the note the user wants to delete?
MorganIGuess
MorganIGuessOP6mo ago
Are there any examples of this being used for multiple buttons? I haven't seen any just yet
sibber
sibber6mo ago
forget about the ui for now how would you identify a note to delete
MorganIGuess
MorganIGuessOP6mo ago
Not buttons sorry I mean items Yes Or better: make anything in the collection that contains any form of string be deletable So it's up to the user
sibber
sibber6mo ago
but when i select a note and click delete i expect only the note that i selected to be deleted
MorganIGuess
MorganIGuessOP6mo ago
Yeah
sibber
sibber6mo ago
so if i have 2 notes hello hello world and i click delete on hello, both of those would be deleted?
MorganIGuess
MorganIGuessOP6mo ago
Oh wait,so you're saying this wouldn't be specific enough Nope, just the first
sibber
sibber6mo ago
because hello world contains hello
MorganIGuess
MorganIGuessOP6mo ago
Ahh true So I can't really use contains then
sibber
sibber6mo ago
yeah because thats not what you want
MorganIGuess
MorganIGuessOP6mo ago
Can you tell me more about the equals idea? It's more specific right
sibber
sibber6mo ago
and if i have 2 identical notes and i click delete on the second one i dont want the first one to be deleted right?
MorganIGuess
MorganIGuessOP6mo ago
Yes
sibber
sibber6mo ago
so how would you differentiate between these 2 identical notes?
MorganIGuess
MorganIGuessOP6mo ago
The id system
sibber
sibber6mo ago
how would you identify which is which? yes you give them ids
MorganIGuess
MorganIGuessOP6mo ago
Assign a number to each note
sibber
sibber6mo ago
yup
MorganIGuess
MorganIGuessOP6mo ago
Contains doesn't accept ints thoufh
sibber
sibber6mo ago
you dont want contains write in english what you want the delete button to do the logic of the delete button shouldnt be that long of a sentence
MorganIGuess
MorganIGuessOP6mo ago
When the delete button is clicked, I want the item the user selects and deletes to be deleted from the observable collection, and thus hidden on the UI
sibber
sibber6mo ago
right but whats the logic
MorganIGuess
MorganIGuessOP6mo ago
Code now right
sibber
sibber6mo ago
an example of a logic: "find the string in the collection that matches the text of the note, and delete it" but this logic cant differentiate between identical notes so whats the logic with ids
MorganIGuess
MorganIGuessOP6mo ago
Okay Find the number in the collection that matches the number of the note, then delete it
sibber
sibber6mo ago
and where would you get the number of the note?
MorganIGuess
MorganIGuessOP6mo ago
Hmm Right now I'm thinking of a count system Id = 0 While collection > 0, id++
sibber
sibber6mo ago
while?
MorganIGuess
MorganIGuessOP6mo ago
While it grows right?
sibber
sibber6mo ago
wdym
MorganIGuess
MorganIGuessOP6mo ago
Or maybe a for each item in collection Increment and assign it to each item
sibber
sibber6mo ago
why not just assign the id when you create the note
MorganIGuess
MorganIGuessOP6mo ago
Yeah okay that works So when you add a new note, the number comes along with the note when it's added right?
sibber
sibber6mo ago
a common simple way is to start at 0 and just increment the id whenever you add an object ok let me ask something else how would you store the notes?
MorganIGuess
MorganIGuessOP6mo ago
The notes are stored on the homepage in a growing collectionview
sibber
sibber6mo ago
yes but how
MorganIGuess
MorganIGuessOP6mo ago
You may click on each note and it will eventually navigate to it's own page
sibber
sibber6mo ago
are they stored as a strings?
MorganIGuess
MorganIGuessOP6mo ago
Oh yes
sibber
sibber6mo ago
so as far as your app knows, a note is a string
MorganIGuess
MorganIGuessOP6mo ago
Yeah they are strings, they contain text Yep
sibber
sibber6mo ago
but you dont just want them to be strings right? you want a note to have an id and a color and maybe a title so string alone isnt enough right?
MorganIGuess
MorganIGuessOP6mo ago
Yes I already gave it a title color etc through the UI
sibber
sibber6mo ago
and where do you store the title and the color?
MorganIGuess
MorganIGuessOP6mo ago
This is .NET Maui by the way fyi I binded them on the UI
sibber
sibber6mo ago
to what?
MorganIGuess
MorganIGuessOP6mo ago
They are apart of the properties The title will just be the string that the user gives the note The title property And the background color property
sibber
sibber6mo ago
and where are those properties? you said earlier theyre stored as strings
MorganIGuess
MorganIGuessOP6mo ago
dotnet
YouTube
.NET MAUI Data Binding with MVVM & XAML [5 of 8] | .NET MAUI for Be...
Learn more ➡️ https://learn.microsoft.com/training/paths/build-apps-with-dotnet-maui/ View full playlist: https://aka.ms/dotnet/beginnervideos/youtube/maui Get Started with .NET in Visual Studio: https://aka.ms/dotnet/get-started/vs Welcome to the .NET MAUI for Beginners Series where you will learn the basics of building multi-platform apps wit...
MorganIGuess
MorganIGuessOP6mo ago
I followed this tutorial for the storing title and color onto the items added in the collectionview The data type is a string as shown in the video, and the collection is known as an observable collection of type string
sibber
sibber6mo ago
ah so what theyre doing
MorganIGuess
MorganIGuessOP6mo ago
By the way I want the delete to work the way it's working for him But mine isn't a mobile app so I can't use swipe view's delete, and his code isn't working because of the contains as mentioned
sibber
sibber6mo ago
is checking if the list contains the item theyre not deleting any string that contains the string theyre checking if the string is in the list before deleting i think you misunderstood that
MorganIGuess
MorganIGuessOP6mo ago
Ah wait Let me try to understand
sibber
sibber6mo ago
also they didnt implement colors or titles
MorganIGuess
MorganIGuessOP6mo ago
Yeah I did that for myself I made my own color functions Theirs is a task app but similar to mine I guess
sibber
sibber6mo ago
right so we go back to my original question then where are you storing the colors
MorganIGuess
MorganIGuessOP6mo ago
The colors are stored in the command When a user clicks on a color box it changes their note background color This background color is then stored in the mainpage's UI in each item of the collection view
sibber
sibber6mo ago
so its only in the ui what does stored in the commad mean? you said earlier you were binding them to a property
MorganIGuess
MorganIGuessOP6mo ago
But the problem is, the notes I make aren't in the list before deleting I need to make them be in the list The command changes the observable property of the color to the chosen color, which then I can use that observable color to bind to the background color property So if the Id helps make the note actually exist in the list so I can delete it then I will implement this
sibber
sibber6mo ago
so you have a viewmodel for the notes
MorganIGuess
MorganIGuessOP6mo ago
Yes The mainviewmodel is where I store the observable collection, which are the notes
sibber
sibber6mo ago
so its an observable collection of the note viewmodel?
MorganIGuess
MorganIGuessOP6mo ago
It contains all the visible items which are the notes if that's what you're asking
sibber
sibber6mo ago
what are the notes observable collection of what that what ive been asking lol
MorganIGuess
MorganIGuessOP6mo ago
Strings Sorry Observable collection<string> I was confused about the view model part until I saw this I think you're asking the type the notes are and in that case it would be observablecollection<string>
sibber
sibber6mo ago
alright i gotta go but what i was getting at is to make a observable collection of note viewmodels
MorganIGuess
MorganIGuessOP6mo ago
Note viewmodels? When you come back could you explain that a bit I think I'm going to try to do this, because even if I do use contains only one specific note is going to have for example the number "1" And by this logic I think it should get me to delete specific notes even when another note has the same string in its name
Want results from more Discord servers?
Add your server