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. My thoughts on C#

My thoughts on C#

Scheduled Pinned Locked Moved C#
designcsharpc++cssdiscussion
74 Posts 13 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.
  • F F ES Sitecore

    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
    
    B Offline
    B Offline
    Brian_TheLion
    wrote on last edited by
    #50

    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

    1 Reply Last reply
    0
    • M Mycroft Holmes

      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

      B Offline
      B Offline
      Brian_TheLion
      wrote on last edited by
      #51

      Hi Mycroft. I'm considering everything at the moment which is why I have not started to write my program in C#. Brian

      1 Reply Last reply
      0
      • B BillWoodruff

        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

        B Offline
        B Offline
        Brian_TheLion
        wrote on last edited by
        #52

        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

        1 Reply Last reply
        0
        • L Lost User

          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.

          B Offline
          B Offline
          Brian_TheLion
          wrote on last edited by
          #53

          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

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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 Kreskowiak

            B Offline
            B Offline
            Brian_TheLion
            wrote on last edited by
            #54

            Dave 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

            1 Reply Last reply
            0
            • B Brian_TheLion

              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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #55

              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.

              B 1 Reply Last reply
              0
              • B Brian_TheLion

                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

                J Offline
                J Offline
                jsc42
                wrote on last edited by
                #56

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

                B 1 Reply Last reply
                0
                • B Brian_TheLion

                  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

                  N Offline
                  N Offline
                  Nathan Minier
                  wrote on last edited by
                  #57

                  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

                  B 3 Replies Last reply
                  0
                  • B Brian_TheLion

                    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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #58

                    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

                    B 1 Reply Last reply
                    0
                    • N Nathan Minier

                      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

                      B Offline
                      B Offline
                      Brian_TheLion
                      wrote on last edited by
                      #59

                      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

                      N 1 Reply Last reply
                      0
                      • B Brian_TheLion

                        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

                        N Offline
                        N Offline
                        Nathan Minier
                        wrote on last edited by
                        #60

                        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

                        B 1 Reply Last reply
                        0
                        • L Lost User

                          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.

                          B Offline
                          B Offline
                          Brian_TheLion
                          wrote on last edited by
                          #61

                          Hi Richard. It's more of understanding what I have read. so if I still don't understand it then I ask questions. You can't ask a book questions. Not all books give a clear understanding on C#. Lets take an example: A book will tell you that the 'set' command is to set a value and the 'get' command is to get a value, but it fails to tell me why I need to use the set and get commands. Why not just have Name = "Peter" like some programming languages, so I'm thinking when and why should I use these commands; some C# code does not use the get set commands. After doing some research on the internet and studying example code I now have a better idea on how these commands are used. It's also sometimes the way something is explained that makes it easier to understand. I downloaded and started to read Net book zero a few weeks ago, I must take another looks at this book. Thanks for the reminder. Brian

                          L 1 Reply Last reply
                          0
                          • N Nathan Minier

                            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

                            B Offline
                            B Offline
                            Brian_TheLion
                            wrote on last edited by
                            #62

                            What is MEF Nathan?

                            N 2 Replies Last reply
                            0
                            • B Brian_TheLion

                              What is MEF Nathan?

                              N Offline
                              N Offline
                              Nathan Minier
                              wrote on last edited by
                              #63

                              The Managed Extensibility Framework. It's a built-in way to make composable applications in C#, and can double as a dependency injection mechanism. It's not terribly hard to use, but might be a little more overhead than you want to deal with when you're starting out. [Managed Extensibility Framework (MEF) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/framework/mef/)

                              "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                              B 1 Reply Last reply
                              0
                              • B Brian_TheLion

                                What is MEF Nathan?

                                N Offline
                                N Offline
                                Nathan Minier
                                wrote on last edited by
                                #64

                                You know, here's a much easier way, which is basically using a global in C# without the work. I just don't advise it in general, but it does have uses:

                                public class SomeClass
                                {
                                public static string SomeValue = "This is a Value";
                                }

                                public class SomeOtherClass
                                {
                                public string LocalValue = SomeClass.SomeValue;
                                }

                                "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                                B 1 Reply Last reply
                                0
                                • N Nathan Minier

                                  The Managed Extensibility Framework. It's a built-in way to make composable applications in C#, and can double as a dependency injection mechanism. It's not terribly hard to use, but might be a little more overhead than you want to deal with when you're starting out. [Managed Extensibility Framework (MEF) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/framework/mef/)

                                  "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                                  B Offline
                                  B Offline
                                  Brian_TheLion
                                  wrote on last edited by
                                  #65

                                  Thanks Nathan. MEF looks like another branch in the NET framework tree like WPF. It's good to know that it exists. I'll take a closer look at it with the link and code you provided thanks. Brian

                                  1 Reply Last reply
                                  0
                                  • N Nathan Minier

                                    You know, here's a much easier way, which is basically using a global in C# without the work. I just don't advise it in general, but it does have uses:

                                    public class SomeClass
                                    {
                                    public static string SomeValue = "This is a Value";
                                    }

                                    public class SomeOtherClass
                                    {
                                    public string LocalValue = SomeClass.SomeValue;
                                    }

                                    "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                                    B Offline
                                    B Offline
                                    Brian_TheLion
                                    wrote on last edited by
                                    #66

                                    Thanks Nathan. Looks like a lot less code. Is there a reason why you advise against using this type of code? Brin

                                    N 1 Reply Last reply
                                    0
                                    • N Nathan Minier

                                      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

                                      B Offline
                                      B Offline
                                      Brian_TheLion
                                      wrote on last edited by
                                      #67

                                      Hi Nathan. I just noticed your link to "SOLID Programming". There are some good examples at that site thanks. Brian

                                      1 Reply Last reply
                                      0
                                      • B Brian_TheLion

                                        Thanks Nathan. Looks like a lot less code. Is there a reason why you advise against using this type of code? Brin

                                        N Offline
                                        N Offline
                                        Nathan Minier
                                        wrote on last edited by
                                        #68

                                        Largely because it's hard to maintain, and very common to forget that you stuck a static property on an object 3 years ago when you first wrote the code. It can also lead to unexpected object states when you can change a static property from outside that object, and proper value validation logic - let alone proper state checks - is often ignored in accessors. Lastly, it's simply not an OOP approach. It's an old-school method that is more-or-less a legacy of structured programming. Yes, static properties are very cool when used to modify the behavior of a genus of objects on the fly, but that's simply not what we're doing here. IMO if you find yourself writing software like this you're not thinking about it in a way that will lead to a good end result. If you're just learning it's fine. Use it as a tool to make something that works. Just keep in the back of your mind that you're learning the skills to do it better the next time, and that how you conceptualize your code will have a huge impact on how good your code ends up being.

                                        "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                                        B 1 Reply Last reply
                                        0
                                        • N Nathan Minier

                                          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

                                          B Offline
                                          B Offline
                                          Brian_TheLion
                                          wrote on last edited by
                                          #69

                                          Just one quest question Nathan. You gave me a link to SOLID Programming. In the code examples at that site they use a dollar sign in front of a variable. What's the reason for this? Brian

                                          N 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