Using your own classes in methods.
-
I am pretty sure I have some sort of syntax problem here but I can't get my head around it, dont see anything wrong. I have made a class called cRoom, I create several room objects from this and shove them in an ArrayList. class cRoom { //variables left out for better viewing public cRoom(//insert loads of strings and ints here) { } // All the public get/sets go here, again left out for better viewing. } //at some point in my Main() program ill call a method called North //rActive is a cRoom object, arrRoomList is an array of cRooms rActive = North(rActive,arrRoomList) // The method for North public static cRoom North(cRoom activeRoom, ArrayList roomList) { if (activeRoom.NORTH != 0) { foreach (cRoom room in roomList) { if (room.RID == activeRoom.NORTH) { return room; } } } else { Console.WriteLine("I can't go that way."); return activeRoom; } } Anyway when the code is compiled I get two errors. Inconsistent accesibility: return type ....cRoom is less accessible than method.....North Inconsistent accesibility: parameter type...cRoom is less accessible than method....North Thanks in advance. =)
-
I am pretty sure I have some sort of syntax problem here but I can't get my head around it, dont see anything wrong. I have made a class called cRoom, I create several room objects from this and shove them in an ArrayList. class cRoom { //variables left out for better viewing public cRoom(//insert loads of strings and ints here) { } // All the public get/sets go here, again left out for better viewing. } //at some point in my Main() program ill call a method called North //rActive is a cRoom object, arrRoomList is an array of cRooms rActive = North(rActive,arrRoomList) // The method for North public static cRoom North(cRoom activeRoom, ArrayList roomList) { if (activeRoom.NORTH != 0) { foreach (cRoom room in roomList) { if (room.RID == activeRoom.NORTH) { return room; } } } else { Console.WriteLine("I can't go that way."); return activeRoom; } } Anyway when the code is compiled I get two errors. Inconsistent accesibility: return type ....cRoom is less accessible than method.....North Inconsistent accesibility: parameter type...cRoom is less accessible than method....North Thanks in advance. =)
The
cRoom
class is private and theNorth
method is public. That means that the method is available outside of the surrounding class, but it's unusable as it's impossible to create thecRoom
object that is needed for calling it. Just make the method private also.--- single minded; short sighted; long gone;
-
The
cRoom
class is private and theNorth
method is public. That means that the method is available outside of the surrounding class, but it's unusable as it's impossible to create thecRoom
object that is needed for calling it. Just make the method private also.--- single minded; short sighted; long gone;
Thanks! Worked a charm. Its decided to throw another one at me now. 'Adventure.Program.North(Adventure.Program.cRoom, System.Collections.ArrayList)': not all code paths return a value which I am guessing has something to do with the ArrayList im only returning the cRoom object
-
Thanks! Worked a charm. Its decided to throw another one at me now. 'Adventure.Program.North(Adventure.Program.cRoom, System.Collections.ArrayList)': not all code paths return a value which I am guessing has something to do with the ArrayList im only returning the cRoom object
-
Thanks! Worked a charm. Its decided to throw another one at me now. 'Adventure.Program.North(Adventure.Program.cRoom, System.Collections.ArrayList)': not all code paths return a value which I am guessing has something to do with the ArrayList im only returning the cRoom object
jblouir wrote:
im only returning the cRoom object
Not always, that's what the error message is saying. Look at the logic in the method. If activeRoom.NORTH is non-zero but not found in the list, the method doesn't return anything.
--- single minded; short sighted; long gone;
-
Not all code paths return a value... What code paths is it refering to? code paths in a class or in the method?
-
the variables in my class cRoom are private but they all have public get/set returns I dont see what I am missing
eh woops I think I just figured it out edit: eh nope, still the same thing each room has a number associated to their north south east and west exits if its 0 there is no exit that direction otherwise its 101 or 102 etc. so if activeRoom.NORTH != 0 then its going to == 102 or another number then I take that number and shove it into int RID then I search each cRoom in the arraylist for that value and if it finds it then it takes the rooms value and drops it into the activeRoom and its returned
-
eh woops I think I just figured it out edit: eh nope, still the same thing each room has a number associated to their north south east and west exits if its 0 there is no exit that direction otherwise its 101 or 102 etc. so if activeRoom.NORTH != 0 then its going to == 102 or another number then I take that number and shove it into int RID then I search each cRoom in the arraylist for that value and if it finds it then it takes the rooms value and drops it into the activeRoom and its returned
Ok I solved the problem... For some reason it didnt like the fact that I had the return activeRoom; in the follow position foreach (cRoom room in roomList) { if (room.RID == RID) { activeRoom = room; return activeRoom; } } So I moved it here foreach (cRoom room in roomList) { if (room.RID == RID) { activeRoom = room; } } return activeRoom; and it worked fine I guess I cant have a return value in a foreach loop Thanks for your help. =)
-
Ok I solved the problem... For some reason it didnt like the fact that I had the return activeRoom; in the follow position foreach (cRoom room in roomList) { if (room.RID == RID) { activeRoom = room; return activeRoom; } } So I moved it here foreach (cRoom room in roomList) { if (room.RID == RID) { activeRoom = room; } } return activeRoom; and it worked fine I guess I cant have a return value in a foreach loop Thanks for your help. =)
jblouir wrote:
I guess I cant have a return value in a foreach loop
You can, although it's good form to only have one return, at the end of a method.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Thanks! Worked a charm. Its decided to throw another one at me now. 'Adventure.Program.North(Adventure.Program.cRoom, System.Collections.ArrayList)': not all code paths return a value which I am guessing has something to do with the ArrayList im only returning the cRoom object
If you declare a method that returns a value, you must make sure that all places where you may exit the function will return a value of that type. Example:
public int MyFunc()
{
if (errorcondition)
{
return 2;
}
else
{
// no returns
...
}
// here must be a return value
return 0;
}
}