Click here to Skip to main content
14,269,640 members
   

C#

 
AnswerRe: Looks like C# does not support Global Variables Pin
OriginalGriff27-Mar-19 23:57
protectorOriginalGriff27-Mar-19 23:57 
GeneralRe: Looks like C# does not support Global Variables Pin
Richard MacCutchan28-Mar-19 0:08
protectorRichard MacCutchan28-Mar-19 0:08 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 0:09
memberBrian_TheLion28-Mar-19 0:09 
GeneralRe: Looks like C# does not support Global Variables Pin
OriginalGriff28-Mar-19 0:21
protectorOriginalGriff28-Mar-19 0:21 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 0:35
memberBrian_TheLion28-Mar-19 0:35 
GeneralRe: Looks like C# does not support Global Variables Pin
OriginalGriff28-Mar-19 0:42
protectorOriginalGriff28-Mar-19 0:42 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 0:49
memberBrian_TheLion28-Mar-19 0:49 
GeneralRe: Looks like C# does not support Global Variables Pin
Luc Pattyn28-Mar-19 0:32
professionalLuc Pattyn28-Mar-19 0:32 
Hi,

if a room (or many rooms) is a relevant item in your application domain, then by all means define a class named Room, and give it all the public properties you are going to need, such as Description, Location, Name, etc. Now start creating Room instances and use them any way you see fit.

Once you have
public class Room {
    public static List<Room> AllRooms=new List<Room>();
    private string name;
    private Point location;
    private bool isLightOn;
    ...

    public Room(string name, Point location, ...) {
        this.name=name;
        AllRooms.Add(this);
    }

    public string Name { get {return name;}}

    public Point Location { get {return location;}}

    public void SwitchOnLight() {
        isLightOn=true;
    }

    public static Room FindByName(string name) {
        foreach(Room r in AllRooms) {
            if (r.Name==name) return r;
        }
        return null; // or throw some exception
    }
}


you can operate on rooms in many ways:

Room myRoom1=new Room("kitchen", ...);
Room myRoom2=new Room("study", ...);

Room someRoom=Room.FindByName("kitchen");
someRoom.SwitchOnLight();


and you can pass an entire room, with all its properties automatically also available, to whatever method you choose to create:

Room myRoom1=new Room("kitchen", ...);
inventory(myRoom1);


If it has a name, it most certainly deserves to be described by a class!

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 0:41
memberBrian_TheLion28-Mar-19 0:41 
GeneralRe: Looks like C# does not support Global Variables Pin
Luc Pattyn28-Mar-19 0:51
professionalLuc Pattyn28-Mar-19 0:51 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 0:57
memberBrian_TheLion28-Mar-19 0:57 
GeneralRe: Looks like C# does not support Global Variables Pin
Luc Pattyn28-Mar-19 1:07
professionalLuc Pattyn28-Mar-19 1:07 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 1:31
memberBrian_TheLion28-Mar-19 1:31 
GeneralRe: Looks like C# does not support Global Variables Pin
Luc Pattyn28-Mar-19 1:37
professionalLuc Pattyn28-Mar-19 1:37 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 2:03
memberBrian_TheLion28-Mar-19 2:03 
GeneralRe: Looks like C# does not support Global Variables Pin
OriginalGriff28-Mar-19 3:26
protectorOriginalGriff28-Mar-19 3:26 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 16:54
memberBrian_TheLion28-Mar-19 16:54 
GeneralRe: Looks like C# does not support Global Variables Pin
OriginalGriff29-Mar-19 2:33
protectorOriginalGriff29-Mar-19 2:33 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion29-Mar-19 3:39
memberBrian_TheLion29-Mar-19 3:39 
GeneralRe: Looks like C# does not support Global Variables Pin
OriginalGriff1-Apr-19 20:01
protectorOriginalGriff1-Apr-19 20:01 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion2-Apr-19 1:04
memberBrian_TheLion2-Apr-19 1:04 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion2-Apr-19 14:23
memberBrian_TheLion2-Apr-19 14:23 
GeneralRe: Looks like C# does not support Global Variables Pin
#realJSOP29-Mar-19 2:28
mve#realJSOP29-Mar-19 2:28 
AnswerRe: Looks like C# does not support Global Variables Pin
OriginalGriff28-Mar-19 0:20
protectorOriginalGriff28-Mar-19 0:20 
GeneralRe: Looks like C# does not support Global Variables Pin
Brian_TheLion28-Mar-19 0:26
memberBrian_TheLion28-Mar-19 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.