C
C#3y ago
4

How do I use recursive methods and constructions together? [Answered]

i don't know what is wrong
51 Replies
TheRanger
TheRanger3y ago
what does the error message say?
Yawnder
Yawnder3y ago
Probably that there is no such method as Power accepting 2 parameters.
4
4OP3y ago
Yawnder
Yawnder3y ago
No, that can't be it.
TheRanger
TheRanger3y ago
do you have a method called Power that takes 2 ints somewhere?
Yawnder
Yawnder3y ago
You're talking about a runtime error while this can't compile.
4
4OP3y ago
I need to use fields inside power method
TheRanger
TheRanger3y ago
do that then
4
4OP3y ago
this ?
Yawnder
Yawnder3y ago
How can you reach there when you have a compile error?
TheRanger
TheRanger3y ago
not anymore i guess
Yawnder
Yawnder3y ago
Oh, there is that other method. Can you $codegif the whole class please?
4
4OP3y ago
can i post all the code here, it's short already
TheRanger
TheRanger3y ago
StackOverFlow exception means there is a method that keeps calling itself infinitely and Power keeps calling itself, infinitely
Yawnder
Yawnder3y ago
That's what I'm asking, but not as a screenshot, as $code
MODiX
MODiX3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
4
4OP3y ago
i use google translate sorry for my misakes
TheRanger
TheRanger3y ago
what is the value of powernum?
4
4OP3y ago
namespace something
{




class Calculate
{
int _baseNum;
int _powerNum;



public Calculate(int _baseNum, int _powerNum)
{
this._baseNum = _baseNum;
this._powerNum = _powerNum;
}


//int _baseNum, int _powerNum


public int Power(int _baseNum, int _powerNum)
{



if (this._powerNum == 0)

return 1;
else
return this._baseNum *= Power(this._baseNum, this._powerNum - 1);


}






static void Main()
{

Calculate number = new Calculate(2,3);
number.Power(4,5);


}

}

}
namespace something
{




class Calculate
{
int _baseNum;
int _powerNum;



public Calculate(int _baseNum, int _powerNum)
{
this._baseNum = _baseNum;
this._powerNum = _powerNum;
}


//int _baseNum, int _powerNum


public int Power(int _baseNum, int _powerNum)
{



if (this._powerNum == 0)

return 1;
else
return this._baseNum *= Power(this._baseNum, this._powerNum - 1);


}






static void Main()
{

Calculate number = new Calculate(2,3);
number.Power(4,5);


}

}

}
TheRanger
TheRanger3y ago
i see the issue
4
4OP3y ago
3 i guess
TheRanger
TheRanger3y ago
you never used your parameters
Yawnder
Yawnder3y ago
It's weird. You're using fields while you don't need fields.
4
4OP3y ago
this is my homework
TheRanger
TheRanger3y ago
your not using your parameters, ur using your fields
4
4OP3y ago
how i use its?
Yawnder
Yawnder3y ago
I don't think a homework would be as specific as saying "be recursive, but use fields not parameters".
TheRanger
TheRanger3y ago
remove this.
Yawnder
Yawnder3y ago
And I would say remove the fields all together. Even make the method static I think.
TheRanger
TheRanger3y ago
yeah
4
4OP3y ago
my teacher likes hard questions
Yawnder
Yawnder3y ago
It's not about it being hard. It's just a bad design imo. Do you have the verbatim question somewhere? What language is it in?
4
4OP3y ago
then how do i use the fields?
Yawnder
Yawnder3y ago
(Gotta make a work call, but I'll check back in after) By keeping this. ...
4
4OP3y ago
hmm
TheRanger
TheRanger3y ago
if you want to use fields your method doesnt need parameters
4
4OP3y ago
i'll wait Can't fields take parameters?
TheRanger
TheRanger3y ago
because ur basically passing arguments to the method, but you never used them if you mean assigning a value to fields, you can
4
4OP3y ago
I don't know how to use them
TheRanger
TheRanger3y ago
what is your goal? and why do you need fields?
4
4OP3y ago
i need power calculator with recrusive methods and constructions
TheRanger
TheRanger3y ago
yes but why do u need fields?
4
4OP3y ago
because we learned these topics in the lesson and I have to write this code using them
TheRanger
TheRanger3y ago
did they tell you in the lesson to use the method's parameters?
4
4OP3y ago
i think so
TheRanger
TheRanger3y ago
i can simulate how your code is working now
4
4OP3y ago
yes pls
TheRanger
TheRanger3y ago
Calculate number = new Calculate(2,3);
number.Power(4,5);

class Calculate
{
int _baseNum; // became 2 from the constructor
int _powerNum; // became 3 from the constructor



public Calculate(int _baseNum, int _powerNum)
{
this._baseNum = _baseNum; // _baseNum is 2
this._powerNum = _powerNum; // _powerNum is 3
}


//int _baseNum, int _powerNum


public int Power(int _baseNum, int _powerNum) //
{
// _baseNum passed from the method is 4, unrelated to _baseNum of field which is 2
// _powerNum passed from the method is 5, unrelated to _powerNum of field which is 3


if (_powerNum == 0) // this is _powerNum of the method, which is 5

return 1;
else
return this._baseNum *= Power(this._baseNum, this._powerNum - 1); //this._baseNum is 2, different from _baseNum which came from the method which is 4
//this._powerNum is 3, different from _powerNum which came from the method which is 5

// (_powerNumb==0)? return 1 : return _baseNum *= Power(_baseNum, _powerNumb - 1);

}
Calculate number = new Calculate(2,3);
number.Power(4,5);

class Calculate
{
int _baseNum; // became 2 from the constructor
int _powerNum; // became 3 from the constructor



public Calculate(int _baseNum, int _powerNum)
{
this._baseNum = _baseNum; // _baseNum is 2
this._powerNum = _powerNum; // _powerNum is 3
}


//int _baseNum, int _powerNum


public int Power(int _baseNum, int _powerNum) //
{
// _baseNum passed from the method is 4, unrelated to _baseNum of field which is 2
// _powerNum passed from the method is 5, unrelated to _powerNum of field which is 3


if (_powerNum == 0) // this is _powerNum of the method, which is 5

return 1;
else
return this._baseNum *= Power(this._baseNum, this._powerNum - 1); //this._baseNum is 2, different from _baseNum which came from the method which is 4
//this._powerNum is 3, different from _powerNum which came from the method which is 5

// (_powerNumb==0)? return 1 : return _baseNum *= Power(_baseNum, _powerNumb - 1);

}
4
4OP3y ago
omg im a dump then I have to delete the filedlari and use only recursive methods
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server