C
C#7mo ago
Eve.

✅ null error, not sure how to fix

No description
60 Replies
Eve.
Eve.OP7mo ago
@Pobiega
Pobiega
Pobiega7mo ago
Yeah, you can't do that. Strings in C# are immutable Meaning you need to produce a new string
Eve.
Eve.OP7mo ago
oh in the RemoveExclaimation or in the main
Pobiega
Pobiega7mo ago
Former
Eve.
Eve.OP7mo ago
where is that i thought turning it into an array would help
thegu5
thegu57mo ago
make the userinput a char array, then give that char array to the function
Eve.
Eve.OP7mo ago
i thought i did that
Pobiega
Pobiega7mo 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.OP7mo ago
i produced a new string
No description
Eve.
Eve.OP7mo ago
however i think i've done it wrong
Pobiega
Pobiega7mo 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.OP7mo ago
i want to remove it
Pobiega
Pobiega7mo ago
okay
Eve.
Eve.OP7mo ago
but i thought i had to replace it to do that
Pobiega
Pobiega7mo 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.OP7mo ago
that means it can't change doesnt it
Pobiega
Pobiega7mo ago
correct
Eve.
Eve.OP7mo ago
i've done this before and it worked
Eve.
Eve.OP7mo ago
No description
Pobiega
Pobiega7mo 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.OP7mo ago
right okay so have i done this completely wrong
Pobiega
Pobiega7mo 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.OP7mo ago
my goal is to remove the !
Pobiega
Pobiega7mo ago
Fully remove, right?
Eve.
Eve.OP7mo ago
yes
Pobiega
Pobiega7mo ago
Not just replace Ok
Eve.
Eve.OP7mo ago
no
Pobiega
Pobiega7mo ago
There is a class called StringBuilder It's very commonly used when you need to "build" a string
Eve.
Eve.OP7mo ago
okay ill make it now
Eve.
Eve.OP7mo ago
No description
Eve.
Eve.OP7mo ago
you mean like this
Pobiega
Pobiega7mo ago
Haha, no there already is one in .NET You don't need to make your ow
Eve.
Eve.OP7mo ago
oh okay
Eve.
Eve.OP7mo ago
No description
Pobiega
Pobiega7mo ago
Yup Now change the code to use the string builder
Eve.
Eve.OP7mo ago
im not sure what string builder does exactly
Pobiega
Pobiega7mo ago
on a new row, type sb. and see what pops up
Eve.
Eve.OP7mo ago
No description
Pobiega
Pobiega7mo ago
see that list of things? scroll through it so far, Append is a very good method suggestion
Eve.
Eve.OP7mo ago
there is remove
Pobiega
Pobiega7mo 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.OP7mo ago
okay ive used append what does it do?
Pobiega
Pobiega7mo ago
what do you think? with that name
Eve.
Eve.OP7mo ago
fix?
Pobiega
Pobiega7mo ago
we have a class called StringBuilder. it builds strings, right?
Eve.
Eve.OP7mo ago
oh build
Pobiega
Pobiega7mo ago
what does append mean, in english? it means "to add at the end"
Eve.
Eve.OP7mo ago
i did not know that
Pobiega
Pobiega7mo ago
now you do 🙂
Eve.
Eve.OP7mo ago
you're teaching me coding and english
Pobiega
Pobiega7mo ago
When do I get my teaching license? :d
Eve.
Eve.OP7mo ago
soon right so ive put
Eve.
Eve.OP7mo ago
No description
Pobiega
Pobiega7mo 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.OP7mo ago
okay ive changed it
Pobiega
Pobiega7mo ago
also, if you prefer, we could go to a voice channel and screen share
Eve.
Eve.OP7mo ago
okay lets do that
Pobiega
Pobiega7mo ago
#vc-1
Eve.
Eve.OP7mo 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
Pobiega7mo ago
bah stop being sensible 😄 good luck!
Want results from more Discord servers?
Add your server