C
C#5mo ago
Eve.

✅ null error, not sure how to fix

No description
60 Replies
Eve.
Eve.5mo ago
@Pobiega
Pobiega
Pobiega5mo ago
Yeah, you can't do that. Strings in C# are immutable Meaning you need to produce a new string
Eve.
Eve.5mo ago
oh in the RemoveExclaimation or in the main
Pobiega
Pobiega5mo ago
Former
Eve.
Eve.5mo ago
where is that i thought turning it into an array would help
thegu5
thegu55mo ago
make the userinput a char array, then give that char array to the function
Eve.
Eve.5mo ago
i thought i did that
Pobiega
Pobiega5mo ago
thats also a bad idea. your input is a string. The "law of least surprise" says that your output should be a string without exclamations
Eve.
Eve.5mo ago
i produced a new string
No description
Eve.
Eve.5mo ago
however i think i've done it wrong
Pobiega
Pobiega5mo ago
correct also, setting it to a space isnt the same as removing it do you just want to replace the ! with or do you want a string without ! in it?
Eve.
Eve.5mo ago
i want to remove it
Pobiega
Pobiega5mo ago
okay
Eve.
Eve.5mo ago
but i thought i had to replace it to do that
Pobiega
Pobiega5mo ago
then you need to build a new string you needed to do that before too, to be clear since strings are immutable
Eve.
Eve.5mo ago
that means it can't change doesnt it
Pobiega
Pobiega5mo ago
correct
Eve.
Eve.5mo ago
i've done this before and it worked
Eve.
Eve.5mo ago
No description
Pobiega
Pobiega5mo ago
ToCharArray() it converts the string to a char[] but an array is fixed-size! that means you cant remove an item from it, just clear it so !!hello!! would become hello not hello
Eve.
Eve.5mo ago
right okay so have i done this completely wrong
Pobiega
Pobiega5mo ago
Well, depends on what your goal was Most people would expect you to take a string in and return a new string
Eve.
Eve.5mo ago
my goal is to remove the !
Pobiega
Pobiega5mo ago
Fully remove, right?
Eve.
Eve.5mo ago
yes
Pobiega
Pobiega5mo ago
Not just replace Ok
Eve.
Eve.5mo ago
no
Pobiega
Pobiega5mo ago
There is a class called StringBuilder It's very commonly used when you need to "build" a string
Eve.
Eve.5mo ago
okay ill make it now
Eve.
Eve.5mo ago
No description
Eve.
Eve.5mo ago
you mean like this
Pobiega
Pobiega5mo ago
Haha, no there already is one in .NET You don't need to make your ow
Eve.
Eve.5mo ago
oh okay
Eve.
Eve.5mo ago
No description
Pobiega
Pobiega5mo ago
Yup Now change the code to use the string builder
Eve.
Eve.5mo ago
im not sure what string builder does exactly
Pobiega
Pobiega5mo ago
on a new row, type sb. and see what pops up
Eve.
Eve.5mo ago
No description
Pobiega
Pobiega5mo ago
see that list of things? scroll through it so far, Append is a very good method suggestion
Eve.
Eve.5mo ago
there is remove
Pobiega
Pobiega5mo ago
the idea is that you progressively build a copy of the original string, but when you encounter a !, you DONT add it or that, copy the entire string and remove ! but I prefer the first approach
Eve.
Eve.5mo ago
okay ive used append what does it do?
Pobiega
Pobiega5mo ago
what do you think? with that name
Eve.
Eve.5mo ago
fix?
Pobiega
Pobiega5mo ago
we have a class called StringBuilder. it builds strings, right?
Eve.
Eve.5mo ago
oh build
Pobiega
Pobiega5mo ago
what does append mean, in english? it means "to add at the end"
Eve.
Eve.5mo ago
i did not know that
Pobiega
Pobiega5mo ago
now you do 🙂
Eve.
Eve.5mo ago
you're teaching me coding and english
Pobiega
Pobiega5mo ago
When do I get my teaching license? :d
Eve.
Eve.5mo ago
soon right so ive put
Eve.
Eve.5mo ago
No description
Pobiega
Pobiega5mo ago
static string RemoveExclamations(string input)
{
var sb = new StringBuilder();

foreach (char c in input)
{
// Eve - your code here.
}

return sb.ToString();
}
static string RemoveExclamations(string input)
{
var sb = new StringBuilder();

foreach (char c in input)
{
// Eve - your code here.
}

return sb.ToString();
}
maybe start over from here you have a bunch of old code that doesnt belong and is just confusing you
Eve.
Eve.5mo ago
okay ive changed it
Pobiega
Pobiega5mo ago
also, if you prefer, we could go to a voice channel and screen share
Eve.
Eve.5mo ago
okay lets do that
Pobiega
Pobiega5mo ago
#vc-1
Eve.
Eve.5mo ago
i've got 2 exams on monday, so im going to revise the othe for the rest of the night, but will be back to computer science tomorrow @Pobiega
Pobiega
Pobiega5mo ago
bah stop being sensible 😄 good luck!
Want results from more Discord servers?
Add your server