C
C#2y 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
SG972y ago
what is the error referenceclass1.IsWord() returns false? if so, provide the implementation of IsWord()
Dan
DanOP2y 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
SG972y ago
you need to provide all of the parameters or create a parameterless constructor and assign the fields individually
Dan
DanOP2y 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
SG972y 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
Soinagie2y ago
Can I ask question about this code?
SG97
SG972y ago
surely there's no problem so shoot
Soinagie
Soinagie2y ago
Why is there Class1 referenceclass1 = new Class1(word); if Word is assigned anything? Shouldn't then there be an error?
SG97
SG972y 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
Soinagie2y ago
Class1(word: "area")
SG97
SG972y ago
sure but would still not compile as there is not a constructor that has 1 parameter Class1(aWord: "area")
Soinagie
Soinagie2y ago
Would it be bad naming convention if I'd name them the same?
SG97
SG972y ago
the same? oh
Soinagie
Soinagie2y ago
This.word = word
SG97
SG972y ago
to me, yes
Soinagie
Soinagie2y ago
Yes, it's bad or yes, it's fine?
SG97
SG972y 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
Soinagie2y ago
Tbh I don't know the difference but thanks I'll be naming them differently
SG97
SG972y ago
difference of what
Soinagie
Soinagie2y ago
Parameters and properties
SG97
SG972y ago
you may want to read up on those individually, as they are basic language features
Soinagie
Soinagie2y ago
But there's no method here To assign parameters
SG97
SG972y ago
these are constructor parameters
Soinagie
Soinagie2y ago
Maybe someday I'll get it Probably not Thanks
cap5lut
cap5lut2y 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
Want results from more Discord servers?
Add your server