My thoughts on C#
-
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
-
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'm considering everything at the moment which is why I have not started to write my program in C#. 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
Hi Bill. It might seem that I have not done much reading on C# but that is not the case. I've borrows 4 books on C# from the library and have been studying them. However it's difficult to find a good book on a on C# that explain it well and books such as "Beginning Visual C# 2015 Programming" don't do a good job at explaining how OOP works in C#. Even after reading I still need to ask questions to get a better understanding on parts of C#. As well as this group I've also found Google useful for some of my questions on C#. 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.
Hi Richard. That's what I'm trying to do by reading books on the subject and asking questions. I read somewhere that C# is starting to spread to programming apps on tablets. Brian
-
Brian_TheLion wrote:
The original code was written in the BASIC language back in the 1980's
There's the problem. You're trying to force the old, non-OPP code way of doing things into an OOP world where it just doesn't work. You cannot do a line-for-line conversion. You have to understand what the INTENT of the old code and rewrite using modern techniques. This is will result is radically different code because BASIC is NOT VB.NET, or C#, or Java, or C++. You know that your app needs an inventory. How that's implemented in the new version is going to be done VERY differently from how your existing BASIC code implemented it.
Brian_TheLion wrote:
Someone suggested that I should look at instances of classes
You create instances of classes all the time. Every time you "new up" a class. For example:
IInventory playerInventory = new InventoryManager();
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakDave wrote You have to understand what the INTENT of the old code and rewrite using modern technique I agree with you Dave and that's what I'm aiming for at the moment. I think if I was programming for the fist time then it might be easier for me as old programming habits take time to die. Brian
-
Hi Richard. That's what I'm trying to do by reading books on the subject and asking questions. I read somewhere that C# is starting to spread to programming apps on tablets. Brian
What you really need to do is to stop coding, stop posting questions, and work through some solid study guides to get a full understanding of the basics of the language, classes and structs, value types and reference types, generics, etc, etc. I started by working through .NET Book Zero by Charles Petzold[^] a couple of times, before I attempted to write my first (very simple) C# application.
-
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
Brian_TheLion wrote:
Having used programs like Quick Basic over the years makes it more difficult to break free from the procedure programming method
I, too, started on procedural languages like FORTRAN, COBOL and BASIC and initially found the Object Oriented way seemed artificial, clunky and back-to-front. Now, having spent several years in an OO environment, I can see the benefits and would never go back to the old ways. It can be hard to unlearn things that work and to undo your mental images of how things interact, but it is worth it in the long run. I'd suggest that you park your adventure game and write something from scratch, forcing yourself to use the OO paradigm (horrible word) and then evaluate what worked well and what seem odd. Then revisit your adventure game looking at the objects (players, inventory items, rooms) and seeing how they map onto classes and then look at their properties (inventory items are-in rooms, players -have- inventory items, players are-in rooms, rooms are-adjacent-to- other rooms) then look at methods that change the properties (players move-to rooms).
-
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
Brian_TheLion wrote:
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.
Yeah, that's not how you do that. There are a few approaches that are valid from a C# point of view, but by and large a variable in the global namespace is never the answer unless the language itself forces that on you (thank you, JavaScript). There are better tools: a static container class, a service locator, or best of all dependency injection.
Brian_TheLion wrote:
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.
Good OOP uses many, many classes that work in conjunction to build a system.I suggest you take some time to learn about [SOLID Programming](https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design). Global variables also make maintenance much, much harder in complex software. Modern IDEs have made this a little less significant, but for good, flexible software you should still prefer composability to imperative structure.
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
-
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
Your quest for the "ultimate programming language" seems to be an adventure game in itself. Except you're ignoring all the clues left by those that have come before you. Assembler PC Basic Lattice C (MS C) dBase Clipper VB Cirrus (MS Access V1) FoxPro ... C#
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.' ― Confucian Analects
-
Brian_TheLion wrote:
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.
Yeah, that's not how you do that. There are a few approaches that are valid from a C# point of view, but by and large a variable in the global namespace is never the answer unless the language itself forces that on you (thank you, JavaScript). There are better tools: a static container class, a service locator, or best of all dependency injection.
Brian_TheLion wrote:
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.
Good OOP uses many, many classes that work in conjunction to build a system.I suggest you take some time to learn about [SOLID Programming](https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design). Global variables also make maintenance much, much harder in complex software. Modern IDEs have made this a little less significant, but for good, flexible software you should still prefer composability to imperative structure.
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor
Hi Nathan. I think its more out of the frustration when a outside variable is not recognized in a class and I'm wishing it was a global variable to solve the problem. But like others have said Global variables don't make good programming code. Brian
-
Hi Nathan. I think its more out of the frustration when a outside variable is not recognized in a class and I'm wishing it was a global variable to solve the problem. But like others have said Global variables don't make good programming code. Brian
Yep, that's why I mentioned other tools. I like using MEF for this:
public class MyConfig
{
[Export("MyValue")]
public string SharedValue => "This is a value";
}[Import]
public class MyBusinessObject
{
[Import("MyValue")]
public string ConfigValue { get; set; } // Will be initialized to "This is a value"
}[Import]
public class MyOtherBusinessObject
{
[Import("MyValue")]
public string Value { get; set; } // Will be initialized to "This is a value"
}This requires a little bit of bootstrapping to get MEF running, but results in a concise dependency injection mechanism without the bulkiness that some frameworks have.
"Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor