C
C#2y ago
Oli

✅ Need some help with classes

Not too sure how to correctly use attributes from a class in a class function
22 Replies
Anton
Anton2y ago
There are no attributes on the picture. Do you mean fields? Also, a class function is usually called a method
Oli
OliOP2y ago
yeah thats what i meant
Anton
Anton2y ago
well methods are in effect just functions that take an extra hidden parameter the this parameter it's a reference to an object with your fields effectively you could say this.F_name in your function for clarity, if you were wondering how that works
Oli
OliOP2y ago
alright
Anton
Anton2y ago
so what's your problem anyway?
Oli
OliOP2y ago
it just keeps looping ill enter an upper case first name and last name but it keeps asking for it or a string where the first character is uppercase
Anton
Anton2y ago
what's 1 in Char.IsUpper?
Oli
OliOP2y ago
the first character of the string oh wait
Anton
Anton2y ago
indices are usually treated as offsets rather than positions
Oli
OliOP2y ago
i see what ive done
Anton
Anton2y ago
so the first character is at offset 0
Oli
OliOP2y ago
it should be 0 to check the first character yeah cheers
Anton
Anton2y ago
np
Oli
OliOP2y ago
how do i get rid of the green underlines it keeps saying that it could be null
Anton
Anton2y ago
check for null before assigning it or make the fields nullable
Oli
OliOP2y ago
thats just putting ? infront of them right
Anton
Anton2y ago
yes, after the type string?
Oli
OliOP2y ago
Anton
Anton2y ago
readline may return null an empty string too
Oli
OliOP2y ago
it works now so does it really matter?
Anton
Anton2y ago
it would throw an exception if you enter an empty string or pipe from a file
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Closed!
Want results from more Discord servers?
Add your server