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.
  • B Brian_TheLion

    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

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #36

    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

    B 1 Reply Last reply
    0
    • D 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 Kreskowiak

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

      In that case it looks like I need a Player class Dave. Brian

      D 1 Reply Last reply
      0
      • J jschell

        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.

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

        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

        1 Reply Last reply
        0
        • B Brian_TheLion

          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

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #39

          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 1 Reply Last reply
          0
          • realJSOPR realJSOP

            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

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

            A static class is a very different animal.

            1 Reply Last reply
            0
            • B Brian_TheLion

              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

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

              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 1 Reply Last reply
              0
              • realJSOPR realJSOP

                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

                F Offline
                F Offline
                F ES Sitecore
                wrote on last edited by
                #42

                #realJSOP wrote:

                I don't understand (and have never seen) a global static class present problems

                You've lived a very sheltered life :)

                realJSOPR 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

                  F Offline
                  F Offline
                  F ES Sitecore
                  wrote on last edited by
                  #43

                  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 1 Reply Last reply
                  0
                  • F F ES Sitecore

                    #realJSOP wrote:

                    I don't understand (and have never seen) a global static class present problems

                    You've lived a very sheltered life :)

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #44

                    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

                    D 1 Reply Last reply
                    0
                    • B Brian_TheLion

                      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

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #45

                      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

                      D B 2 Replies Last reply
                      0
                      • realJSOPR realJSOP

                        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

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #46

                        Is 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

                        1 Reply Last reply
                        0
                        • B Brian_TheLion

                          In that case it looks like I need a Player class Dave. Brian

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #47

                          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

                          1 Reply Last reply
                          0
                          • realJSOPR realJSOP

                            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

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #48

                            I'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

                            1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              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

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

                              Thanks for your support realUSOP. Brian

                              1 Reply Last reply
                              0
                              • 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
                                          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