Looks like C# does not support Global Variables
-
I'm re-sending this as I had sent it to the Java forum by mistake. I just found out that you can't have global variables in C#. I think they are supported in other languages like C, C++, Python and maybe Visual Basic.Net. The program I want to convert to C# has a lot of variables that need to be used in other parts of the program such as procedures. I know you can add variables within the curly brackets for a procedure but in many cases the procedure needs to know about a lot of the variables to function and give a result and the same procedures are used many times in the code. I could use classes but I don't think you can have use the same variable in more than one class. I'll need to experiment a bit to see if there is anyway around this. Brian
-
I'm re-sending this as I had sent it to the Java forum by mistake. I just found out that you can't have global variables in C#. I think they are supported in other languages like C, C++, Python and maybe Visual Basic.Net. The program I want to convert to C# has a lot of variables that need to be used in other parts of the program such as procedures. I know you can add variables within the curly brackets for a procedure but in many cases the procedure needs to know about a lot of the variables to function and give a result and the same procedures are used many times in the code. I could use classes but I don't think you can have use the same variable in more than one class. I'll need to experiment a bit to see if there is anyway around this. Brian
-
I'm re-sending this as I had sent it to the Java forum by mistake. I just found out that you can't have global variables in C#. I think they are supported in other languages like C, C++, Python and maybe Visual Basic.Net. The program I want to convert to C# has a lot of variables that need to be used in other parts of the program such as procedures. I know you can add variables within the curly brackets for a procedure but in many cases the procedure needs to know about a lot of the variables to function and give a result and the same procedures are used many times in the code. I could use classes but I don't think you can have use the same variable in more than one class. I'll need to experiment a bit to see if there is anyway around this. Brian
The idea is that you don't need global variables in C#, and by and large it's true. You can simulate them using
static
:public static class Globals
{
public static int GlobalInteger = 666;
}
...
Console.WriteLine(Globals.GlobalInteger);but I've only done it with a handful of variables in all the C# code I've ever written. If you need lots of globals, it's normally a BIG sign that your whole design is wildly wrong. I'd seriously think about the whole structure of your app if I were you - it's sounding very much like you didn't read any of the books after all.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
The idea is that you don't need global variables in C#, and by and large it's true. You can simulate them using
static
:public static class Globals
{
public static int GlobalInteger = 666;
}
...
Console.WriteLine(Globals.GlobalInteger);but I've only done it with a handful of variables in all the C# code I've ever written. If you need lots of globals, it's normally a BIG sign that your whole design is wildly wrong. I'd seriously think about the whole structure of your app if I were you - it's sounding very much like you didn't read any of the books after all.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
The idea is that you don't need global variables in C#, and by and large it's true. You can simulate them using
static
:public static class Globals
{
public static int GlobalInteger = 666;
}
...
Console.WriteLine(Globals.GlobalInteger);but I've only done it with a handful of variables in all the C# code I've ever written. If you need lots of globals, it's normally a BIG sign that your whole design is wildly wrong. I'd seriously think about the whole structure of your app if I were you - it's sounding very much like you didn't read any of the books after all.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
I have been reading books and took a look at some tutorials on line Griff. I used the knowledge I gained to start work in writing my programs. My first attempt was to use procedures when the code was repeating in places. I managed to get the adventure compiler to work without any problems. I'm now working on the adventure driver. Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code. I'm now looking at using classes as a way to wrote the adventure driver code as I can't use global variables. Brian
-
I'm re-sending this as I had sent it to the Java forum by mistake. I just found out that you can't have global variables in C#. I think they are supported in other languages like C, C++, Python and maybe Visual Basic.Net. The program I want to convert to C# has a lot of variables that need to be used in other parts of the program such as procedures. I know you can add variables within the curly brackets for a procedure but in many cases the procedure needs to know about a lot of the variables to function and give a result and the same procedures are used many times in the code. I could use classes but I don't think you can have use the same variable in more than one class. I'll need to experiment a bit to see if there is anyway around this. Brian
I've just looked at the Java forum - thanks Richard - and seen what you are trying to do, and I was right: your design is poor. Why do you need a global variable for "current room", or "inventory"? Think about it: your player is in a current room, your player has an inventory - and that's true for each player in the game, regardless of whether you have a dozen players or one. So you make the Location part of each object (including player, swords, bullets, bad guys, whatever - they have a "Location" in the game) and Inventory a part of the player (and probably the bad guy). When you pick something up, it gets added to the objects collection of the inventory of the player. Drop it, and you remove it from the player inventory and added to the room inventory, or the level, or world inventory. Use it, and you remove it from the inventory and discard it. You pass a Player and possibly a room or level to your methods and they access the items from that. Global variables aren't needed, and it becomes a whole load easier to add a party of adventurers or whatever to your game instead of a single player.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
I have been reading books and took a look at some tutorials on line Griff. I used the knowledge I gained to start work in writing my programs. My first attempt was to use procedures when the code was repeating in places. I managed to get the adventure compiler to work without any problems. I'm now working on the adventure driver. Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code. I'm now looking at using classes as a way to wrote the adventure driver code as I can't use global variables. Brian
Member 14154627 wrote:
Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code.
So you have a Room class: it has a name, a location, a description, etc. Pass the room and the player and you have all the info you need!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
I've just looked at the Java forum - thanks Richard - and seen what you are trying to do, and I was right: your design is poor. Why do you need a global variable for "current room", or "inventory"? Think about it: your player is in a current room, your player has an inventory - and that's true for each player in the game, regardless of whether you have a dozen players or one. So you make the Location part of each object (including player, swords, bullets, bad guys, whatever - they have a "Location" in the game) and Inventory a part of the player (and probably the bad guy). When you pick something up, it gets added to the objects collection of the inventory of the player. Drop it, and you remove it from the player inventory and added to the room inventory, or the level, or world inventory. Use it, and you remove it from the inventory and discard it. You pass a Player and possibly a room or level to your methods and they access the items from that. Global variables aren't needed, and it becomes a whole load easier to add a party of adventurers or whatever to your game instead of a single player.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Hi Griff. In answer to your question. The program is designed to not just play a certain adventure but to be able to load many compiled adventures. The person writes his adventure as a script file. The script file is compiled using the adventure compiler then the adventure driver loads the compiled adventure to play the came. You need to know what room the player is in (current room) so yu can list the objects in that room and if the player tried to pick up an object then the condition is that the object has to be in the same room as the player (current room and Object Location is used to check for this. Brian
-
I have been reading books and took a look at some tutorials on line Griff. I used the knowledge I gained to start work in writing my programs. My first attempt was to use procedures when the code was repeating in places. I managed to get the adventure compiler to work without any problems. I'm now working on the adventure driver. Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code. I'm now looking at using classes as a way to wrote the adventure driver code as I can't use global variables. Brian
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 asDescription
,Location
,Name
, etc. Now start creating Room instances and use them any way you see fit. Once you havepublic class Room {
public static List AllRooms=new List();
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! :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Member 14154627 wrote:
Due to the type of game it has at least 32 variables for things such as Romm Names, Room Locations, Room Descriptions etc Some procedures require many variables to produce a result and some variables are used many times in procedures. The use of arrays also helped to simplify the code.
So you have a Room class: it has a name, a location, a description, etc. Pass the room and the player and you have all the info you need!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Is this what you mean Griff public class Room() { public String [] RoomName; public int [] RoomLocation; public String RoomDescription; public int [,] Direction; } The other way would be to use a structure but classes might have been solutions. Brian
-
Hi Griff. In answer to your question. The program is designed to not just play a certain adventure but to be able to load many compiled adventures. The person writes his adventure as a script file. The script file is compiled using the adventure compiler then the adventure driver loads the compiled adventure to play the came. You need to know what room the player is in (current room) so yu can list the objects in that room and if the player tried to pick up an object then the condition is that the object has to be in the same room as the player (current room and Object Location is used to check for this. Brian
That's why you have a Room class, and a Player class. Part of the Player Location is the Room he is in ... You are thinking in a "C" language way, not an OOPs way - and that won't help you at all with C# apps. Start considering your game as composed of objects which "know" how to do things to themselves instead of trying to think of the "program" as being in charge and deciding what to do. For example, a Player might have a WalkForward method, which internally finds the Room he is in, finds his location in the room, finds which way he is facing, and asks the Room to move one step in that direction. The Room checks if there is a wall in the way, or a pit full of Grues, or a heavy weight about to drop on that square and returns a result to the Player method which updates its location or takes damage. The Room doesn't care which player it is, the Player doesn't care which Room it is - they just use the data they have to work with any Room / Player combination.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
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 asDescription
,Location
,Name
, etc. Now start creating Room instances and use them any way you see fit. Once you havepublic class Room {
public static List AllRooms=new List();
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! :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Hi Luc. Thanks for the example code. Will this work for arrays? As I have many rooms I'm using RoomName[1] = "Hallway", RoomName[2] = "Lounge", etc. Brian
-
Is this what you mean Griff public class Room() { public String [] RoomName; public int [] RoomLocation; public String RoomDescription; public int [,] Direction; } The other way would be to use a structure but classes might have been solutions. Brian
No. Why would you have an array of names? Does a room have multiple names? None of the ones in my house do ... Think about your house or flat, or whatever: what would you need to map that out as a collection of rooms?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
No. Why would you have an array of names? Does a room have multiple names? None of the ones in my house do ... Think about your house or flat, or whatever: what would you need to map that out as a collection of rooms?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Hi Griff. Each room does have an array of directions that you can go in to enter other rooms (N,S,E,W). Originally I used a double array suych as Direction[room number, direction] = 6 Directions were 1 - 6 for North, South,....Up, Down. Direction[1,1] = 6. If I'm in room 1 and move North then I go to room 6. Brian
-
Hi Luc. Thanks for the example code. Will this work for arrays? As I have many rooms I'm using RoomName[1] = "Hallway", RoomName[2] = "Lounge", etc. Brian
Yes, you can create arrays of anything you like, as long as the items have the same type (all integers, or all rooms, or...). Most often you are better of using a List, which basically is an array that grows automatically when you add items (see AllRooms in my example). Now is the time to stop fiddling around; do not just try and convert some existing code into C# code as long as you are unfamiliar with C# and OOP; so start reading: 1. a decent book on C#, at least the chapters that pertain to object-oriented programming; 2. a couple of CodeProject articles (including source code!) that deal with something that appeals to you. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Yes, you can create arrays of anything you like, as long as the items have the same type (all integers, or all rooms, or...). Most often you are better of using a List, which basically is an array that grows automatically when you add items (see AllRooms in my example). Now is the time to stop fiddling around; do not just try and convert some existing code into C# code as long as you are unfamiliar with C# and OOP; so start reading: 1. a decent book on C#, at least the chapters that pertain to object-oriented programming; 2. a couple of CodeProject articles (including source code!) that deal with something that appeals to you. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Yes I admit that I have always had problems in understanding classes and now and then I come across some code that makes it a bit easier to understand. I had considered using Lists but I think there was no direct access to the data in a list except using the foreach command. Brian
-
That's why you have a Room class, and a Player class. Part of the Player Location is the Room he is in ... You are thinking in a "C" language way, not an OOPs way - and that won't help you at all with C# apps. Start considering your game as composed of objects which "know" how to do things to themselves instead of trying to think of the "program" as being in charge and deciding what to do. For example, a Player might have a WalkForward method, which internally finds the Room he is in, finds his location in the room, finds which way he is facing, and asks the Room to move one step in that direction. The Room checks if there is a wall in the way, or a pit full of Grues, or a heavy weight about to drop on that square and returns a result to the Player method which updates its location or takes damage. The Room doesn't care which player it is, the Player doesn't care which Room it is - they just use the data they have to work with any Room / Player combination.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Hi Griff. I have come from a background of programming in Basic then Visual Basic, so I tend to slip into this type of programming. I understand that the class method is a more modern way to program. It's just getting my head around classes even when reading about them. Brian
-
Yes I admit that I have always had problems in understanding classes and now and then I come across some code that makes it a bit easier to understand. I had considered using Lists but I think there was no direct access to the data in a list except using the foreach command. Brian
You need to study the materials that are made for studying purposes; they will teach you: 1. thinking in an object-oriented way, i.e. with classes and objects 2. getting an overall grasp of a language, so you know, rather than think you know, what capabilities have been provided. Reading a book will, in an orderly fashion, provide you with most all of the answers to the hundreds of questions you otherwise will come up with in random order; you better invest a few days learning all the concepts and possibilities, rather than wasting the same or more time in stumbling around and learning not much. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
You need to study the materials that are made for studying purposes; they will teach you: 1. thinking in an object-oriented way, i.e. with classes and objects 2. getting an overall grasp of a language, so you know, rather than think you know, what capabilities have been provided. Reading a book will, in an orderly fashion, provide you with most all of the answers to the hundreds of questions you otherwise will come up with in random order; you better invest a few days learning all the concepts and possibilities, rather than wasting the same or more time in stumbling around and learning not much. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Hi Luc. I seem to learn the best from code examples. There use to be the complete code of programs on the internet but it seems more difficult to find these days...maybe people don't want their programs copied and like to keep their code to themselves. Cope examples are good but there are times when you like to view the complete picture. Brian
-
Hi Luc. I seem to learn the best from code examples. There use to be the complete code of programs on the internet but it seems more difficult to find these days...maybe people don't want their programs copied and like to keep their code to themselves. Cope examples are good but there are times when you like to view the complete picture. Brian
X|
Luc Pattyn [My Articles] Nil Volentibus Arduum