Object Method error
True is only outputted with referenceclass2 not with referenceclass1. I can probably avoid with exception handling but this is for practice. Any ideas why the error
Class1 referenceclass1 = new Class1(word); referenceclass1.word = "area"; referenceclass1.sentence = "area is a word"; referenceclass1.letters = 12; Class1 referenceclass2 = new Class1("apple", "apple is a fruit", 13); Console.WriteLine(referenceclass1.IsWord()); Console.WriteLine(referenceclass2.IsWord());
25 Replies
what is the error
referenceclass1.IsWord()
returns false?
if so, provide the implementation of IsWord()
CS7036 There is no argument given that corresponds to the required formal parameter 'aSentence' of 'Class1.Class1(string, string, double)'
internal class Class1 { public string word; public string sentence; public double letters; public Class1(string aWord, string aSentence, double aLetters) { word=aWord; sentence=aSentence; letters=aLetters; } public bool IsWord() { if(letters>=1) { return true; } return false; } }
you need to provide all of the parameters
or create a parameterless constructor and assign the fields individually
👍
It would help if you example rewrite the constructor for referenceclass1 in a way that provides all the parameters slightly unclear what you mean
Class1 referenceclass1 = new Class1(word);
you're only providing one of the parameters
if you want to go the parameterless constructor route, it's just public Class1()
Can I ask question about this code?
surely there's no problem so shoot
Why is there
Class1 referenceclass1 = new Class1(word);
if Word
is assigned anything?
Shouldn't then there be an error?they probably left that out
judging by the error: There is no argument given that corresponds to the required formal parameter 'aSentence' of 'Class1.Class1(string, string, double)'
Class1(word: "area")
sure but would still not compile
as there is not a constructor that has 1 parameter
Class1(aWord: "area")
Would it be bad naming convention if I'd name them the same?
the same?
oh
This.word = word
to me, yes
Yes, it's bad or yes, it's fine?
yes, it's bad
the params would be
string word, string sentence, double letterCount
and corresponding properties would be string Word, string Sentence, double LetterCount
Tbh I don't know the difference but thanks
I'll be naming them differently
difference of what
Parameters and properties
you may want to read up on those individually, as they are basic language features
But there's no method here
To assign parameters
these are constructor parameters
Maybe someday I'll get it
Probably not
Thanks
maybe its more like a XY problem and actually there should be only one parameter with the input, that may or may not is a sentence.
the rest u can calculate on the fly (eg. by defining that a word consists only of letters)
my assumption is based on that
letters
is a double