C
C#15mo ago
Dan

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
SG97
SG9715mo ago
what is the error referenceclass1.IsWord() returns false? if so, provide the implementation of IsWord()
Dan
Dan15mo ago
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; } }
SG97
SG9715mo ago
you need to provide all of the parameters or create a parameterless constructor and assign the fields individually
Dan
Dan15mo ago
👍 It would help if you example rewrite the constructor for referenceclass1 in a way that provides all the parameters slightly unclear what you mean
SG97
SG9715mo ago
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()
Soinagie
Soinagie15mo ago
Can I ask question about this code?
SG97
SG9715mo ago
surely there's no problem so shoot
Soinagie
Soinagie15mo ago
Why is there Class1 referenceclass1 = new Class1(word); if Word is assigned anything? Shouldn't then there be an error?
SG97
SG9715mo ago
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)'
Soinagie
Soinagie15mo ago
Class1(word: "area")
SG97
SG9715mo ago
sure but would still not compile as there is not a constructor that has 1 parameter Class1(aWord: "area")
Soinagie
Soinagie15mo ago
Would it be bad naming convention if I'd name them the same?
SG97
SG9715mo ago
the same? oh
Soinagie
Soinagie15mo ago
This.word = word
SG97
SG9715mo ago
to me, yes
Soinagie
Soinagie15mo ago
Yes, it's bad or yes, it's fine?
SG97
SG9715mo ago
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
Soinagie
Soinagie15mo ago
Tbh I don't know the difference but thanks I'll be naming them differently
SG97
SG9715mo ago
difference of what
Soinagie
Soinagie15mo ago
Parameters and properties
SG97
SG9715mo ago
you may want to read up on those individually, as they are basic language features
Soinagie
Soinagie15mo ago
But there's no method here To assign parameters
SG97
SG9715mo ago
these are constructor parameters
Soinagie
Soinagie15mo ago
Maybe someday I'll get it Probably not Thanks
cap5lut
cap5lut15mo ago
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