✅ Member '<member name>' cannot be accessed with an instance reference

namespace palidromeDepth
{
internal class Program
{
static void Main(string[] args)
{

}
void int palidromCheck(int depth, int number)
{
Program program = new Program(); // Defines program
string reversed = program.Reverse(Convert.ToString(number); // Supposed to get a reverse of the number
if (Convert.ToString(number) == reversed)
{
return depth; // returns all and sends depth to the user
}
else
{
return palidromCheck(depth+1, number+Convert.ToInt16(reversed)); // Runs the module again
}

}
static string Reverse(string word)
{
// Converts to Array, reverses array, then converts back to string
char[] charArray = word.ToCharArray();
Array.Reverse(charArray);
return new string(charArray); // SHOULD return reversed string
}
}

}
namespace palidromeDepth
{
internal class Program
{
static void Main(string[] args)
{

}
void int palidromCheck(int depth, int number)
{
Program program = new Program(); // Defines program
string reversed = program.Reverse(Convert.ToString(number); // Supposed to get a reverse of the number
if (Convert.ToString(number) == reversed)
{
return depth; // returns all and sends depth to the user
}
else
{
return palidromCheck(depth+1, number+Convert.ToInt16(reversed)); // Runs the module again
}

}
static string Reverse(string word)
{
// Converts to Array, reverses array, then converts back to string
char[] charArray = word.ToCharArray();
Array.Reverse(charArray);
return new string(charArray); // SHOULD return reversed string
}
}

}
When I call it the way I am doing here, it tells me I need to do it with an instance reference, how would I do that? How would I call my string reverse code? Do I need to change on how I name it? Sorry I am pretty new to C# and only started learning it today
4 Replies
Saltys_Salty
Saltys_SaltyOP2y ago
Please ping in response! Thank you in advance
daysleeper
daysleeper2y ago
i dont really get what your palindromeCheck() is supposed to do but to resolve your issues: first off you cant declare "void int" there can only be one return type - either void or int. your Main() method is the part that executes in C#, so you need to invoke your method inside it. then you mustnt create an instance of Program. you need to make methods static (you only did it to Reverse method) and you will be able to to do what you want. there are probably some more issues which i cant think of right now but you can say which errors you get afterwards
Saltys_Salty
Saltys_SaltyOP2y ago
Yes! Everything is now working, thank you so much
Saltys_Salty
Saltys_SaltyOP2y ago
Want results from more Discord servers?
Add your server