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

    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

    D B L realJSOPR J 8 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

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

      Brian_TheLion wrote:

      After learning C# for a while it seems that you need to keep within certain rules to have the program compile.

      True for ANY language, not just C#.

      Brian_TheLion wrote:

      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.

      It's no the rules of C# so much as it is a strict implementation of Object Oriented Programming, which other languages support, not just C#. "Global variables", in my humble opinion, are a lazy and error prone way of moving data between objects.

      Brian_TheLion wrote:

      the program I have in mind that I want to write has many varables between classes

      You're thinking of data used in more than one class as "global". Well, "global" is just another container, just like a class instance is a container, that can be passed into other class methods.

      Brian_TheLion wrote:

      It also makes the program easier to deal with when changes are made.

      Actually, this isn't true. You have code all over the place that can manipulate "global" variables and debugging problems with that code can be a nightmare because there is no central repository controlling access to those variables.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      B realJSOPR 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

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

        Please accept that my intention in saying this to you is positive, addressed to that vital entity in you that learns ... that develops new skills over time. So far, I see you as having been "messing around" with C#; I see no evidence of systematic study. You continue to ask questions which ask us to "spoon-feed" you information you could easily have found for yourself. Go ahead to C++, have your global variables, but, don't kid yourself that you have taken a serious look at C#.

        «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
        • B BillWoodruff

          Please accept that my intention in saying this to you is positive, addressed to that vital entity in you that learns ... that develops new skills over time. So far, I see you as having been "messing around" with C#; I see no evidence of systematic study. You continue to ask questions which ask us to "spoon-feed" you information you could easily have found for yourself. Go ahead to C++, have your global variables, but, don't kid yourself that you have taken a serious look at C#.

          «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
          #4

          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

          L OriginalGriffO B 3 Replies Last reply
          0
          • D Dave Kreskowiak

            Brian_TheLion wrote:

            After learning C# for a while it seems that you need to keep within certain rules to have the program compile.

            True for ANY language, not just C#.

            Brian_TheLion wrote:

            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.

            It's no the rules of C# so much as it is a strict implementation of Object Oriented Programming, which other languages support, not just C#. "Global variables", in my humble opinion, are a lazy and error prone way of moving data between objects.

            Brian_TheLion wrote:

            the program I have in mind that I want to write has many varables between classes

            You're thinking of data used in more than one class as "global". Well, "global" is just another container, just like a class instance is a container, that can be passed into other class methods.

            Brian_TheLion wrote:

            It also makes the program easier to deal with when changes are made.

            Actually, this isn't true. You have code all over the place that can manipulate "global" variables and debugging problems with that code can be a nightmare because there is no central repository controlling access to those variables.

            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
            #5

            Thanks Dave for your answers to what I have written. To give you a better idea of what I'm aiming for please consider this example. This is for a text adventure game where the player can pick up and drop objects. You have an inventory class and a get/drop class The player might type "get slippers" from the main() class The main() class recognizes this as a 'get' command and sends the item "slippers" to the get/drop class. The get/drop class would have to check the inventory class to see if the player already is carrying slippers and also to check if the player can carry more items, if these checks pass then the get.drop class would have to remove the slippers from the room and send "slippers to the inventory class so that they are added to the items carried by the player which is kept in the inventory class. Finally the get/drop class sends back to the main() class a test message for the player "Slippers picked up". So there needs to be a way for C# to communicate between classes and one class to be able to control another class. Brian

            L D 2 Replies 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

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

              Once you start understanding the difference between a "class" (which you keep referring to), and an "instance" of a class, you will make (some) progress.

              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

              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

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Brian_TheLion wrote:

                I have a program I want to convert to C#.

                And there is your problem! You are taking an existing C++ program and assuming that the "best idea" is to translate it to C#, because C++ and C# are so similar. But they aren't. They are completely different languages that share some common syntax. They work differently. Ignore computers for a moment and think about languages. Write a letter to your friend in English. You then remember that Hans doesn't speak English, only German. So what do you do? Well, they both use the same alphabet, so it can' be that hard. Grab a English-German dictionary, and look up each word in turn. You will end up with a letter full of German words - but is it a good German letter? Does it make sense? Does it say what you wrote the original to say? Almost certainly not, because the English word "Current" for example, has many different meanings: "the flow of water", "the power of electricity", "modern and trendy", "a dried grape" which will all have different words in German: "die Strömung", "der Strom", "gegenwärtig", "die Rosine". Which one did you use? Literal translation doesn't work for languages - that's why Google translate is so incredible, it tries to work out from the whole context what you are talking about. In fact it lists many different translations for "Current":

                derStrom current, power, stream, electricity, flux, river
                Strömung flow, current, stream, trend, drift, tendency
                aktuell current, latest, actual, topical, up, relevant
                gegenwärtig present, current, existing
                laufend running, ongoing, current, present, routine, runny
                derzeitig current, present, prevailing, of that time
                augenblicklich present, current, immediate, momentary, temporary
                geltend established, current, in force, prevailing, operative
                gebräuchlich common, customary, usual, conventional, current, standard
                gängig common, popular, current, going, possible
                bestehend existing, established, present, current, standing, prevailing
                jetzig present, current
                nunmehrig current, present
                herrschend ruling, reigning, dominant, prevalent, prevailing, current
                marktgängig marketable, merchantable, current

                And it will use the appropriate one. Google Translate[

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                B 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Brian_TheLion wrote:

                  I have a program I want to convert to C#.

                  And there is your problem! You are taking an existing C++ program and assuming that the "best idea" is to translate it to C#, because C++ and C# are so similar. But they aren't. They are completely different languages that share some common syntax. They work differently. Ignore computers for a moment and think about languages. Write a letter to your friend in English. You then remember that Hans doesn't speak English, only German. So what do you do? Well, they both use the same alphabet, so it can' be that hard. Grab a English-German dictionary, and look up each word in turn. You will end up with a letter full of German words - but is it a good German letter? Does it make sense? Does it say what you wrote the original to say? Almost certainly not, because the English word "Current" for example, has many different meanings: "the flow of water", "the power of electricity", "modern and trendy", "a dried grape" which will all have different words in German: "die Strömung", "der Strom", "gegenwärtig", "die Rosine". Which one did you use? Literal translation doesn't work for languages - that's why Google translate is so incredible, it tries to work out from the whole context what you are talking about. In fact it lists many different translations for "Current":

                  derStrom current, power, stream, electricity, flux, river
                  Strömung flow, current, stream, trend, drift, tendency
                  aktuell current, latest, actual, topical, up, relevant
                  gegenwärtig present, current, existing
                  laufend running, ongoing, current, present, routine, runny
                  derzeitig current, present, prevailing, of that time
                  augenblicklich present, current, immediate, momentary, temporary
                  geltend established, current, in force, prevailing, operative
                  gebräuchlich common, customary, usual, conventional, current, standard
                  gängig common, popular, current, going, possible
                  bestehend existing, established, present, current, standing, prevailing
                  jetzig present, current
                  nunmehrig current, present
                  herrschend ruling, reigning, dominant, prevalent, prevailing, current
                  marktgängig marketable, merchantable, current

                  And it will use the appropriate one. Google Translate[

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

                  Hi Griff. First of all I'm not trying to translate C++ code to C#. I just thought that C++ might be better suited as it supports Gobal variables, but I have not abandoned C#. The original code was written in the BASIC language back in the 1980's I did attempt to convert it to Visual Basic some years ago and like you say it does not work when you try to convert the code exactly how it is written. Since then having a good idea on how the BASIC code works and have wanted to convert it over to a different language but not exactly how it was originally written. The aim in using a different programming language was to get it working in it's basic state then improve on the program. The problem seems to be in getting classes to communicate with each other in C# so that Class A gets a value from Class B (maybe some boolean condition or value is needed from Class B) in order for Class A to supply the correct info to the main() class. Class A may have to update a variable in Class B also. Can one class control another class? Someone suggested that I should look at instances of classes so maybe the answer is there. Brian

                  P OriginalGriffO D 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
                    #9

                    Brian_TheLion wrote:

                    you need to keep within certain rules to have the program compile

                    That is the same for all programming languages, even down to assembler and machine code.

                    Brian_TheLion wrote:

                    There is no global variable allowed

                    As others have mentioned, global variables tend to cause more problems than they solve; don't use them.

                    Brian_TheLion wrote:

                    Maybe some programs are better suited to C# than others.

                    Possibly, but you can use C# to write a wide range of different applications.

                    Brian_TheLion wrote:

                    All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt.

                    I have no idea what that is supposed to mean. Assuming you mean running in a command window, there are many console applications that do just that.

                    Brian_TheLion wrote:

                    the program I have in mind that I want to write has many varables between classes.

                    And that is where C# makes it easy for you; it is specifically designed to support proper object oriented programming. What this, and some of your other questions, suggests, is that you are blaming the language for your lack of understanding. Instead of trying to fit the language to your concept of the program, you need to stop coding and spend some good study time learning the language properly. Only when you have mastered that will you be able to see the best way forward in developing your application.

                    B realJSOPR 2 Replies Last reply
                    0
                    • B Brian_TheLion

                      Hi Griff. First of all I'm not trying to translate C++ code to C#. I just thought that C++ might be better suited as it supports Gobal variables, but I have not abandoned C#. The original code was written in the BASIC language back in the 1980's I did attempt to convert it to Visual Basic some years ago and like you say it does not work when you try to convert the code exactly how it is written. Since then having a good idea on how the BASIC code works and have wanted to convert it over to a different language but not exactly how it was originally written. The aim in using a different programming language was to get it working in it's basic state then improve on the program. The problem seems to be in getting classes to communicate with each other in C# so that Class A gets a value from Class B (maybe some boolean condition or value is needed from Class B) in order for Class A to supply the correct info to the main() class. Class A may have to update a variable in Class B also. Can one class control another class? Someone suggested that I should look at instances of classes so maybe the answer is there. Brian

                      P Offline
                      P Offline
                      phil o
                      wrote on last edited by
                      #10

                      class A
                      {
                      private B _b = new B();

                      public int GetValue()
                      {
                      return (_b.BoolValue) ? 1 : 0;
                      }

                      public void SetValue(int input)
                      {
                      _b.BoolValue = (input != 0);
                      }
                      }

                      class B
                      {
                      public bool BoolValue
                      {
                      get; set;
                      }
                      }

                      class Program{
                      static Main(string[] args)
                      {
                      A a = new A();
                      Console.WriteLine(a.GetValue()); // prints "0"
                      a.SetValue(42);
                      Console.WriteLine(a.GetValue()); // prints "1"
                      }
                      }

                      I cannot imagine you had a look at C# without having to instantiate any class at least once. Maybe because you tried to copy some existing code which was not written with OOP in mind? You should take it from the ground, and follow some basic tutorials about C# and OOP not directly related to your task; this way you may get some important concepts that you will apply later to your actual case.

                      noop()

                      B 1 Reply Last reply
                      0
                      • B Brian_TheLion

                        Hi Griff. First of all I'm not trying to translate C++ code to C#. I just thought that C++ might be better suited as it supports Gobal variables, but I have not abandoned C#. The original code was written in the BASIC language back in the 1980's I did attempt to convert it to Visual Basic some years ago and like you say it does not work when you try to convert the code exactly how it is written. Since then having a good idea on how the BASIC code works and have wanted to convert it over to a different language but not exactly how it was originally written. The aim in using a different programming language was to get it working in it's basic state then improve on the program. The problem seems to be in getting classes to communicate with each other in C# so that Class A gets a value from Class B (maybe some boolean condition or value is needed from Class B) in order for Class A to supply the correct info to the main() class. Class A may have to update a variable in Class B also. Can one class control another class? Someone suggested that I should look at instances of classes so maybe the answer is there. Brian

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        The problem is that you aren't writing a C# program (German letter): you are trying to translate a VB program (English letter) into C# (German letter) by translating each line of code (using a dictionary)

                        Quote:

                        You have an inventory class and a get/drop class

                        Why? Do you do that in the "real world"? Or do you think "I'm wearing trousers. In this pocket I have a hanky and my small change. In this pocket I have my phone, in this one my wallet. My wallet contains my credit cards, store cards, and bank notes"? In an object oriented design, you would have an abstract Container class (or possibly an IContainer Interface) which had Add, Remove, and List methods - because just like your trousers, they "know" where to put your phone - left pocket; wallet - back pocket; and so on. And if the Trousers class derives from Container and so does the Wallet class, you remove a card from your wallet (and it's gone from your trousers as well) pay for your sandwich, and put it back. The idea is that the object knows how to do things that directly affect it: not that you have global functions that manipulate global objects, or a class of "actions" you can "apply" to objects. Why should an inventory be a global variable? Is there only one in the entire game? Or should "monsters" have one as well so you can "Loot the body"? Think of Oblivion / Skyrim and loads of things have inventories: player, barrels, shops, chests, bags, boxes, bodies, npcs, ... and they are all handled the same way because they aren't globals - they are objects contained in the world. As I said, translating doesn't produce good code - using the original as a specification does, because you then write good code in the target language. It doesn't matter which language pair you pick: blind translation isn't a good idea.

                        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        B 1 Reply Last reply
                        0
                        • L Lost User

                          Brian_TheLion wrote:

                          you need to keep within certain rules to have the program compile

                          That is the same for all programming languages, even down to assembler and machine code.

                          Brian_TheLion wrote:

                          There is no global variable allowed

                          As others have mentioned, global variables tend to cause more problems than they solve; don't use them.

                          Brian_TheLion wrote:

                          Maybe some programs are better suited to C# than others.

                          Possibly, but you can use C# to write a wide range of different applications.

                          Brian_TheLion wrote:

                          All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt.

                          I have no idea what that is supposed to mean. Assuming you mean running in a command window, there are many console applications that do just that.

                          Brian_TheLion wrote:

                          the program I have in mind that I want to write has many varables between classes.

                          And that is where C# makes it easy for you; it is specifically designed to support proper object oriented programming. What this, and some of your other questions, suggests, is that you are blaming the language for your lack of understanding. Instead of trying to fit the language to your concept of the program, you need to stop coding and spend some good study time learning the language properly. Only when you have mastered that will you be able to see the best way forward in developing your application.

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

                          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

                          M L 2 Replies Last reply
                          0
                          • B Brian_TheLion

                            Thanks Dave for your answers to what I have written. To give you a better idea of what I'm aiming for please consider this example. This is for a text adventure game where the player can pick up and drop objects. You have an inventory class and a get/drop class The player might type "get slippers" from the main() class The main() class recognizes this as a 'get' command and sends the item "slippers" to the get/drop class. The get/drop class would have to check the inventory class to see if the player already is carrying slippers and also to check if the player can carry more items, if these checks pass then the get.drop class would have to remove the slippers from the room and send "slippers to the inventory class so that they are added to the items carried by the player which is kept in the inventory class. Finally the get/drop class sends back to the main() class a test message for the player "Slippers picked up". So there needs to be a way for C# to communicate between classes and one class to be able to control another class. Brian

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            Hi, Classes get instantiated into objects; all the objects from a single class have the same behavior. Most often these objects are tangible, real-life objects. They have properties that describe them (Age, Color, Size, etc) and methods that operate on them. In an adventure game you would have one or more Adventurer, lots of Rooms, each Room could have Door objects and Window objects, each Adventurer would have an Inventory, an Inventory would hold InventoryItems which could be Keys and Slippers (deriving from InventoryItem), etc. None of these classes would be named after verbs, a get/drop class makes no sense whatsoever. A Get() or a Drop() method would make lots of sense on InventoryItems. If you want to learn object-oriented programming (OOP) then please study the matter by reading a book on the subject, before you start designing and coding. If you refuse to learn OOP properly then please don't touch any of the languages that are intended for OOP, such as Java or C#. FYI: C++ was launched trying to support OOP while maintaining compatibility with non-OOP practices (as in C). I have never been convinced C++ did the programming world a favor. However I am sure Java and C# do. Except maybe for the latest syntax-sugaring additions to those languages. :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            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

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

                              Brian_TheLion wrote:

                              it seems that you need to keep within certain rules to have the program compile.

                              Well, that's true of ANY language.

                              Brian_TheLion wrote:

                              Maybe some programs are better suited to C# than others.

                              That's not true at all.

                              Brian_TheLion wrote:

                              All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt.

                              Again, not true at all. Having a GUI is pretty much dependent on what/who the app is for. I've written several command line apps in the last four months for our DBAs to run from SQL Agents. These apps didn't need a ui.

                              Brian_TheLion wrote:

                              I'm being drawn towards C++ as it does allow global variables compared to C#.

                              If you want global vars, simply create a static Globals class and add the properties and methods you need in it. I do it all the time. In point of fact, EVERY app I write has a static Globals class for that very reason. FWIW, I transitioned from unmanaged C++ to C# back in 2007. It's really not that difficult to do.

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

                              1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Brian_TheLion wrote:

                                After learning C# for a while it seems that you need to keep within certain rules to have the program compile.

                                True for ANY language, not just C#.

                                Brian_TheLion wrote:

                                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.

                                It's no the rules of C# so much as it is a strict implementation of Object Oriented Programming, which other languages support, not just C#. "Global variables", in my humble opinion, are a lazy and error prone way of moving data between objects.

                                Brian_TheLion wrote:

                                the program I have in mind that I want to write has many varables between classes

                                You're thinking of data used in more than one class as "global". Well, "global" is just another container, just like a class instance is a container, that can be passed into other class methods.

                                Brian_TheLion wrote:

                                It also makes the program easier to deal with when changes are made.

                                Actually, this isn't true. You have code all over the place that can manipulate "global" variables and debugging problems with that code can be a nightmare because there is no central repository controlling access to those variables.

                                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                Dave Kreskowiak

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

                                Dave Kreskowiak wrote:

                                "Global variables", in my humble opinion, are a lazy and error prone way of moving data between objects.

                                But like in C++, you don't want to have to reallocate memory every time you need static data. "Global" (static) properties and methods are still a necessary and vital construct in C#. For instance, I have a static SessionVars object in my web apps that provide a foolproof (and type-safe) method for accessing session variables. It's "globally" accessible to the entire app (controllers, views, and other classes). I also have a static Globals class that provides properties (that don't belong in SessionVars) and methods for the entire app. Classifying all "global" objects as the crutch of the lazy programmer is pretty - well - globally wrong. Like any other construct, you have to use static classes apporpriately.

                                ".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
                                • L Lost User

                                  Brian_TheLion wrote:

                                  you need to keep within certain rules to have the program compile

                                  That is the same for all programming languages, even down to assembler and machine code.

                                  Brian_TheLion wrote:

                                  There is no global variable allowed

                                  As others have mentioned, global variables tend to cause more problems than they solve; don't use them.

                                  Brian_TheLion wrote:

                                  Maybe some programs are better suited to C# than others.

                                  Possibly, but you can use C# to write a wide range of different applications.

                                  Brian_TheLion wrote:

                                  All programs seem to have a user interface as I've never come across a program that only runs under the DOS prompt.

                                  I have no idea what that is supposed to mean. Assuming you mean running in a command window, there are many console applications that do just that.

                                  Brian_TheLion wrote:

                                  the program I have in mind that I want to write has many varables between classes.

                                  And that is where C# makes it easy for you; it is specifically designed to support proper object oriented programming. What this, and some of your other questions, suggests, is that you are blaming the language for your lack of understanding. Instead of trying to fit the language to your concept of the program, you need to stop coding and spend some good study time learning the language properly. Only when you have mastered that will you be able to see the best way forward in developing your application.

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

                                  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 F 2 Replies Last reply
                                  0
                                  • B Brian_TheLion

                                    Thanks Dave for your answers to what I have written. To give you a better idea of what I'm aiming for please consider this example. This is for a text adventure game where the player can pick up and drop objects. You have an inventory class and a get/drop class The player might type "get slippers" from the main() class The main() class recognizes this as a 'get' command and sends the item "slippers" to the get/drop class. The get/drop class would have to check the inventory class to see if the player already is carrying slippers and also to check if the player can carry more items, if these checks pass then the get.drop class would have to remove the slippers from the room and send "slippers to the inventory class so that they are added to the items carried by the player which is kept in the inventory class. Finally the get/drop class sends back to the main() class a test message for the player "Slippers picked up". So there needs to be a way for C# to communicate between classes and one class to be able to control another class. Brian

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

                                    Brian_TheLion wrote:

                                    You have an inventory class and a get/drop class

                                    No you wouldn't. You would have an Inventory class with Add, Get, and Drop methods. A class is always a noun. The methods exposed by the class are always verbs that perform operations on that noun. Your Inventory class should never output messages to the console. UI operations have nothing to do with managing the data stored by the Inventory class. This is true for ANY OOP language, including C++ and Java.

                                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                    Dave Kreskowiak

                                    B 2 Replies Last reply
                                    0
                                    • realJSOPR realJSOP

                                      Dave Kreskowiak wrote:

                                      "Global variables", in my humble opinion, are a lazy and error prone way of moving data between objects.

                                      But like in C++, you don't want to have to reallocate memory every time you need static data. "Global" (static) properties and methods are still a necessary and vital construct in C#. For instance, I have a static SessionVars object in my web apps that provide a foolproof (and type-safe) method for accessing session variables. It's "globally" accessible to the entire app (controllers, views, and other classes). I also have a static Globals class that provides properties (that don't belong in SessionVars) and methods for the entire app. Classifying all "global" objects as the crutch of the lazy programmer is pretty - well - globally wrong. Like any other construct, you have to use static classes apporpriately.

                                      ".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
                                      #18

                                      Using variables themselves for passing data to everywhere, like in the old BASIC days, is lazy in an OOP environment. Doing so is an attempt to bypass OOP principles and avoid learning how to do it correctly. I never said there is never a need for globally accessible DATA. I'm saying using global VARIABLES (outside of a class, because the old non-OOP days is where this idea comes from) to hold that data is the wrong way to do it. It's not even possible to do that in C# anyway, where everything must be in a class somewhere. Static classes are a good thing where globally accessible data can be managed properly, but, as you said, must be done appropriately and for the correct reasons.

                                      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

                                        Hi Griff. First of all I'm not trying to translate C++ code to C#. I just thought that C++ might be better suited as it supports Gobal variables, but I have not abandoned C#. The original code was written in the BASIC language back in the 1980's I did attempt to convert it to Visual Basic some years ago and like you say it does not work when you try to convert the code exactly how it is written. Since then having a good idea on how the BASIC code works and have wanted to convert it over to a different language but not exactly how it was originally written. The aim in using a different programming language was to get it working in it's basic state then improve on the program. The problem seems to be in getting classes to communicate with each other in C# so that Class A gets a value from Class B (maybe some boolean condition or value is needed from Class B) in order for Class A to supply the correct info to the main() class. Class A may have to update a variable in Class B also. Can one class control another class? Someone suggested that I should look at instances of classes so maybe the answer is there. Brian

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

                                        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 2 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

                                          J Offline
                                          J Offline
                                          jschell
                                          wrote on last edited by
                                          #20

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