I am having trouble with this piece of code and I can’t figure out how to get it to work. I can’t figure out what the problem is as to me it looks like it should work. The string array called m_nameList on both places are marked as ‘An object reference required for the non static feild, method, or property ‘Solutionname.classname.m_nameList’
the code:
public static bool CheckVacantSeats(int seatNumber)
{
if (m_nameList[seatNumber] == null)
{
return true;
}
return false;
}
m_nameList is an array that is declared in an constructor before this static bool:
public SeatManager(int maxNumberOfSeats)
{
m_totNumOfSeats = maxNumberOfSeats;
m_nameList = new string[m_totNumOfSeats];
m_priceList = new double[m_totNumOfSeats];
}
I am calling the CheckVacantSeat from another class with this:
bool validSeats = SeatManager.CheckVacantSeats(seatNumber, m_nameList);
I can’t figure out what is wrong with it. So i need some help figuring out why m_nameList does not work for me?
Thanks in advance!!
//Regards
The problem is that you have marked your method as static. Since it is static, it has “no” state, and cannot access class members which are not marked as static.
You can mark m_nameList as static, but that means that it’s value is shared across all reads and writes. m_nameList looks like a simple lookup table so maybe this is what you want?
Recommended reading is static and Static Classes and Static Members.