My thoughts on C#
-
Hi Mycroft. I'm doing my best to try and understand OOP from reading and studying examples. The code that was originally written in BASiC Will be pulled apart and put to geather so that it works under the more modern C# structure, so it is like you say a rewrite. I have a good knowledge of how the code works to do a rewrite. Brian
Brian_TheLion wrote:
Will be pulled apart and put to geather so that it works under the more modern C# structure
It feels like your thinking is incorrect, don't consider pulling the old code apart and restructuring it. Only inspect the code to help you define the functionality of the application NOT how it should be put together. Seriously do not try and apply basic methodology and structures to c#, everything is now an object with properties and methods. Define your objects...
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
No! GetDrop is not a noun, it is two verbs, it cannot possibly be a candidate for a class. There is no main() class in my world; besides, class names use TitleCase and no parentheses. If Slippers can be gotten, they need a Get() method. If Keys can be gotten, they need a Get() method. Where are the slippers? where are the keys? does the adventurer know? No. Do all the rooms know? No. The slippers and the keys know where they are, it is information that pertains to them, and to nothing elxe. There should be an Inventory object (or even one per Adventurer), and everything inside such Inventory should derive from InventoryItem, which can hold common properties and/or common methods of gettable objects. An item's location would be an obvious property of InventoryItem; and maybe your complex test belongs there, so you don't have to repeat such test code in every single inventory item's code. Inside InventoryItem class, you probably will have good use for a static List where all items get stored, so you can search them. That is how a Room can figure what is present at any point in time. This was my last technical contribution to this topic. From all you write (and all you apparently don't pick up reading our answers) it is very clear to me you are still missing the basics of OOP. If you don't work on this (shut down your computer and go read one or two OOP books now!) you are wasting your and our time. Books tend to present their subject in a structured way, dealing with way more aspects than you are thinking of right now; random questions in a forum can't match that, they become useful after you acquired the foundation, not before. Good luck. Over and out. :java:
Luc Pattyn [My Articles] Nil Volentibus Arduum
Hi Luc. One thing I did learn from reading your text and texts from some others is that the name of a class needs a noun word. I had thought that you could call the class any name. Strangely that is not taught in the books I have been reading (or I missed it). Thanks for your information, it was not a wasted effect. Brian
-
class A
{
private B _b = new B();public int GetValue()
{
return (_b.BoolValue) ? 1 : 0;
}public void SetValue(int input)
{
_b.BoolValue = (input != 0);
}
}class B
{
public bool BoolValue
{
get; set;
}
}class Program{
static Main(string[] args)
{
A a = new A();
Console.WriteLine(a.GetValue()); // prints "0"
a.SetValue(42);
Console.WriteLine(a.GetValue()); // prints "1"
}
}I cannot imagine you had a look at C# without having to instantiate any class at least once. Maybe because you tried to copy some existing code which was not written with OOP in mind? You should take it from the ground, and follow some basic tutorials about C# and OOP not directly related to your task; this way you may get some important concepts that you will apply later to your actual case.
noop()
Thanks phil for the example code, I'll study it. I find code examples useful in learning how C# works and can be applied to my own code. Brian
-
Brian_TheLion wrote:
You have an inventory class and a get/drop class
No you wouldn't. You would have an Inventory class with Add, Get, and Drop methods. A class is always a noun. The methods exposed by the class are always verbs that perform operations on that noun. Your Inventory class should never output messages to the console. UI operations have nothing to do with managing the data stored by the Inventory class. This is true for ANY OOP language, including C++ and Java.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakHi Dave. I have learn from yourself and from a few replies that the name of a class should be a noun. That presents a another problem is how do I get a message back to the player from the inventory class such as a condition where it was not possible pick up an item. I'd have a rich text message box on the form attached to a string called messages and hopefully there is a way to check for changes in the string so all I need to do is to change the text in the message string which would cause the rich text box to display the string as the string has changed. I'll study the OnPropertyChanged() command. Brian
-
Brian_TheLion wrote:
Will be pulled apart and put to geather so that it works under the more modern C# structure
It feels like your thinking is incorrect, don't consider pulling the old code apart and restructuring it. Only inspect the code to help you define the functionality of the application NOT how it should be put together. Seriously do not try and apply basic methodology and structures to c#, everything is now an object with properties and methods. Define your objects...
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
Hi Mycroft. I may have not used the best term when I said "pulled apart" I was meaning studying the original code to see how it worked to get ideas on how the adventure should work for the new code. Having used programs like Quick Basic over the years makes it more difficult to break free from the procedure programming method, but I don't give up that easierly. Brian
-
Hi Dave. I have learn from yourself and from a few replies that the name of a class should be a noun. That presents a another problem is how do I get a message back to the player from the inventory class such as a condition where it was not possible pick up an item. I'd have a rich text message box on the form attached to a string called messages and hopefully there is a way to check for changes in the string so all I need to do is to change the text in the message string which would cause the rich text box to display the string as the string has changed. I'll study the OnPropertyChanged() command. Brian
The Inventory class isn't responsible for communicating with the user at all, nor is it responsible for picking up items. The Inventory class just manages what is already in inventory, adding items to it, and removing items from it. Nothing else. Think of as a bag of items. Can a bag pickup items from the room? No. The Player has to pickup the items and place them in the bag. Forget OnPropertyChanged until you learn the basics of OOP. It's not going to help you.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
The Inventory class isn't responsible for communicating with the user at all, nor is it responsible for picking up items. The Inventory class just manages what is already in inventory, adding items to it, and removing items from it. Nothing else. Think of as a bag of items. Can a bag pickup items from the room? No. The Player has to pickup the items and place them in the bag. Forget OnPropertyChanged until you learn the basics of OOP. It's not going to help you.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakIn that case it looks like I need a Player class Dave. Brian
-
As a note. If you want to learn how to create games then I would suggest finding one of the game frameworks and using that. There are adventure frameworks but others as well. Using one of those and creating a games (plural) provides a better understanding of the 'domain' for creating games. If you want to learn OO programming and you are using an adventure game as an excuse then learning the rules for the programming language (singular) is going to be your actual goal. The game itself is unlikely to be viable because without a detailed understanding of the 'domain' both from the business layer and the development layer your initial attempts will have problems with structure. And I speak from experience on that having attempted more than a couple games when I first started. But that is not a detriment because that will aid you in the future as you will understand intrinsically why something doesn't work versus because someone told you that.
Thanks jschell. Having an text adventure game waiting to be written does give me something to aim for. Some books and tutorials give you examples on how each of the C# commands can be used but it's when you bring everything together in a project then I find that you learn more. Step-by-step guilds in building a program are useful and I did one of those recently. Brian
-
Hi Bill. I have not completely given up on C#. I was just considering C++ I have a program I want to convert to C#. I have all the variable names I'm going to use and also some of the classes I'm going to create in breaking up the code into classes. To make myself more clearer I need to be able to do the following. Example: Main() class Class A Class B Main() class calls for a value or a boolean response. The result that Class A sends to the Main() class depends on a value in Class B, so Class A needs to check on value in Class B. Just before sending to Main() class the result, Class A needs to update Class B. In C# it seems that classes have strong walls and the main communication is between the main() class and the class and not between the classes themselves. I have been checking with various tests to see what is possible. So far I have managed to send a variable from the main() class to class A and have class A change the variable and have Class A send it back to the main() Class. I have not found a way for Class A to read a variable from Class B or for Class A to change a variable in Class B. Brian
Brian_TheLion wrote:
I have not completely given up on C#
You cannot "give up" on something you have not invested hard work in, something you have spent more time writing questions about than you have spent studying it.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
Richard MacCutchan wrote:
As others have mentioned, global variables tend to cause more problems than they solve; don't use them.
He can get his "global" accessibility by creating a static class. I don't understand (and have never seen) a global static class present problems.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Hi Richard. No I'm not blaiming C# at all, all I'm looking for is a way to use this programming language that suits my needs. I think it's because I'm use to older languages that had less rules such as quick basic. As a hobby programmer I want to improve on any programming skills I have so I turned to C# and invested my time in trying to learn this language. I understand it's the type of language that can't be learnt in a couple of weeks and slowly things that did not make sense at first are starting to make more sense now. I'm still trying to find a way of one class changing the variables in another class if that's possible. As for my message about almost all programs requiring a user interface... You have a choice when writing a c# program of a console program that uses a dos window or a program that has buttons that the user can click on. I don't know of any program sold these days that is a console program. Brian
Brian_TheLion wrote:
I'm looking for is a way to use this programming language that suits my needs.
It will. C# is a rich language that can handle just about any problem you can think of. But, as I and others keep saying, you need to learn and understand the language, and its rules, first.
Brian_TheLion wrote:
I'm still trying to find a way of one class changing the variables in another class if that's possible.
That is what Properties and Methods are there for.
-
Richard MacCutchan wrote:
As others have mentioned, global variables tend to cause more problems than they solve; don't use them.
He can get his "global" accessibility by creating a static class. I don't understand (and have never seen) a global static class present problems.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013#realJSOP wrote:
I don't understand (and have never seen) a global static class present problems
You've lived a very sheltered life :)
-
After learning C# for a while it seems that you need to keep within certain rules to have the program compile. It looks like a case of modifying a program so that it works under C#. There is no global variable allowed so in order to move variables between classes means re-writing the program so it fits within the C# rules. Maybe some programs are better suited to C# than others. I do like being able to design a user interface. All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt. Imagine what programs like Audacity would be like if they only used the DOS prompt. I'm being drawn towards C++ as it does allow global variables compared to C#. I know that it's not good to use global variables and most variables should remind within their own class but it's not always easy to design a program like this and the program I have in mind that I want to write has many varables between classes. I could write it with less classes but I want to have classes for certain purposes that can be reused in other programs. It also makes the program easier to deal with when changes are made. Comments are welcome thanks. Brian
I've knocked up this basic template to give you an idea what people are talking about. There is a "global" object you need to track like the player and also the current location, so you can either create these as an instance of a variable and keep a hold of them, passing them to functions\events as needed, or you could create a "static" class that will hold a reference to your player object and current location object. As I said, it's the basics, you'd need to tweak for things like containers as game objects also so you could pick up a bag, or put a small bag inside a big bag etc.
public class GameObject
{
public string Name { get; set; }
public int Weight { get; set; }
}public abstract class Container
{
public int MaxWeight { get; set; }
public int MaxItems { get; set; }public List Objects { get; private set; } public Container() : this(0, 0) { } public Container(int maxWeight, int maxItems) { this.Objects = new List(); this.MaxItems = maxItems; this.MaxWeight = maxWeight; } public GameObject Find(string name) { return this.Objects.FirstOrDefault(o => o.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); } public bool CanContain(GameObject gameObject) { // if max items is set make sure we have room if (this.MaxItems > 0 && Objects.Count >= this.MaxItems) { return false; } // if max weight is set make sure we have capacity if (this.MaxWeight > 0 && Objects.Sum(o => o.Weight) + gameObject.Weight > this.MaxWeight) { return false; } return true; }
}
public class Player : Container
{
public bool Get(Container container, string name)
{
// rather than returning true\false you can return an enum that is specific to
// why the get failed
GameObject targetObject = container.Find(name);if (targetObject == null) { return false; } if (!this.CanContain(targetObject)) { return false; } container.Objects.Remove(targetObject); this.Objects.Add(targetObject); return true; }
}
public class Room : Container
{
public string Name { get; set; }public Dictionary Exits { get; set; } public Room() { this.Exits = new Dictionary
-
#realJSOP wrote:
I don't understand (and have never seen) a global static class present problems
You've lived a very sheltered life :)
Or just maybe, I've never abused the construct. :) In all actuality, I was annoyed with the lack of support for global vars when I moved from C++ to .Net, but I adapted.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Hi #realUSOP. I agree that in using Global variables is a lazy approach and does not make a good programmer. The reason why I had a need to use Global variables was so classes could get variable information from each other. The inventory class would check the objects class to find out if the object location was in the same room as the player for the player to be able to pick up the object. I'm thinking of classes grouping code together but after reading my replies maybe this is not the case. Brian
I came from C++ to .Net, and I disagree. When used appropriately, global vars are a viable - and even necessary - part of C++.Simply replace the global vars with a static class that contains the vars, and you're off an running. I don't understand how this would be a bad thing, and will continue to use my static globals class for this kind of stuff...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
I came from C++ to .Net, and I disagree. When used appropriately, global vars are a viable - and even necessary - part of C++.Simply replace the global vars with a static class that contains the vars, and you're off an running. I don't understand how this would be a bad thing, and will continue to use my static globals class for this kind of stuff...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Is it weird that I rarely use global variables? I've used static classes with constants defined for "magic values" and objects that are required throughout the code, like a logger, but true global variables, not really.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
In that case it looks like I need a Player class Dave. Brian
You're catching on.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Or just maybe, I've never abused the construct. :) In all actuality, I was annoyed with the lack of support for global vars when I moved from C++ to .Net, but I adapted.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013I've seen too many cases where other people have abused it, and it makes for some really nasty code. The better (not good, just better!) code ends up being more about convenience than it does about expressing a cohesive purpose. The worse stuff just screams "I can't figure out another way to do this." Compound that many times over the span of the entire app and the lack of comments or any documentation and, yeah, it gets ugly.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I came from C++ to .Net, and I disagree. When used appropriately, global vars are a viable - and even necessary - part of C++.Simply replace the global vars with a static class that contains the vars, and you're off an running. I don't understand how this would be a bad thing, and will continue to use my static globals class for this kind of stuff...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Thanks for your support realUSOP. Brian
-
I've knocked up this basic template to give you an idea what people are talking about. There is a "global" object you need to track like the player and also the current location, so you can either create these as an instance of a variable and keep a hold of them, passing them to functions\events as needed, or you could create a "static" class that will hold a reference to your player object and current location object. As I said, it's the basics, you'd need to tweak for things like containers as game objects also so you could pick up a bag, or put a small bag inside a big bag etc.
public class GameObject
{
public string Name { get; set; }
public int Weight { get; set; }
}public abstract class Container
{
public int MaxWeight { get; set; }
public int MaxItems { get; set; }public List Objects { get; private set; } public Container() : this(0, 0) { } public Container(int maxWeight, int maxItems) { this.Objects = new List(); this.MaxItems = maxItems; this.MaxWeight = maxWeight; } public GameObject Find(string name) { return this.Objects.FirstOrDefault(o => o.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); } public bool CanContain(GameObject gameObject) { // if max items is set make sure we have room if (this.MaxItems > 0 && Objects.Count >= this.MaxItems) { return false; } // if max weight is set make sure we have capacity if (this.MaxWeight > 0 && Objects.Sum(o => o.Weight) + gameObject.Weight > this.MaxWeight) { return false; } return true; }
}
public class Player : Container
{
public bool Get(Container container, string name)
{
// rather than returning true\false you can return an enum that is specific to
// why the get failed
GameObject targetObject = container.Find(name);if (targetObject == null) { return false; } if (!this.CanContain(targetObject)) { return false; } container.Objects.Remove(targetObject); this.Objects.Add(targetObject); return true; }
}
public class Room : Container
{
public string Name { get; set; }public Dictionary Exits { get; set; } public Room() { this.Exits = new Dictionary
Thanks very much F-ES Sitecore for taking the time to write the code. It will be very useful and I'll learn more by studying your code. Brian