Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using your own classes in methods.

Using your own classes in methods.

Scheduled Pinned Locked Moved C#
cssdata-structureshelp
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jblouir
    wrote on last edited by
    #1

    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. =)

    G 1 Reply Last reply
    0
    • J jblouir

      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. =)

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      The cRoom class is private and the North 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 the cRoom object that is needed for calling it. Just make the method private also.

      --- single minded; short sighted; long gone;

      J 1 Reply Last reply
      0
      • G Guffa

        The cRoom class is private and the North 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 the cRoom object that is needed for calling it. Just make the method private also.

        --- single minded; short sighted; long gone;

        J Offline
        J Offline
        jblouir
        wrote on last edited by
        #3

        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

        J G R 3 Replies Last reply
        0
        • J jblouir

          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

          J Offline
          J Offline
          jblouir
          wrote on last edited by
          #4

          Not all code paths return a value... What code paths is it refering to? code paths in a class or in the method?

          J 1 Reply Last reply
          0
          • J jblouir

            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

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            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;

            1 Reply Last reply
            0
            • J jblouir

              Not all code paths return a value... What code paths is it refering to? code paths in a class or in the method?

              J Offline
              J Offline
              jblouir
              wrote on last edited by
              #6

              the variables in my class cRoom are private but they all have public get/set returns I dont see what I am missing

              J 1 Reply Last reply
              0
              • J jblouir

                the variables in my class cRoom are private but they all have public get/set returns I dont see what I am missing

                J Offline
                J Offline
                jblouir
                wrote on last edited by
                #7

                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

                J 1 Reply Last reply
                0
                • J jblouir

                  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

                  J Offline
                  J Offline
                  jblouir
                  wrote on last edited by
                  #8

                  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. =)

                  C 1 Reply Last reply
                  0
                  • J jblouir

                    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. =)

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    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 )

                    1 Reply Last reply
                    0
                    • J jblouir

                      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

                      R Offline
                      R Offline
                      Rudolf Jan
                      wrote on last edited by
                      #10

                      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;
                      }
                      }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups