|
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
|
|
|
|
|
Hi Bill.
I have not completely given up on C#. I was just considering C++
I have a program I want to convert to C#. I have all the variable names I'm going to use and also some of the classes I'm going to create in breaking up the code into classes.
To make myself more clearer I need to be able to do the following.
Example:
Main() class
Class A
Class B
Main() class calls for a value or a boolean response.
The result that Class A sends to the Main() class depends on a value in Class B, so Class A needs to check on value in Class B. Just before sending to Main() class the result, Class A needs to update Class B.
In C# it seems that classes have strong walls and the main communication is between the main() class and the class and not between the classes themselves.
I have been checking with various tests to see what is possible. So far I have managed to send a variable from the main() class to class A and have class A change the variable and have Class A send it back to the main() Class.
I have not found a way for Class A to read a variable from Class B or for Class A to change a variable in Class B.
Brian
|
|
|
|
|
Brian_TheLion wrote: I have 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[^]
So ... back to computers. Why would a "literal translation" of C++ work as a C# program?
The answer is, it doesn't. C++ comes with a lot of "baggage" because it evolved from C with added OOPs and has been expanded and modified by a huge committee since then. C# was a new-from-the-ground-up language designed for object orientation (and is being modified by a huge committee so ...) which shares some common syntax.
Translating letters word by word doesn't work: you write a new letter that means the same thing.
Translating programs doesn't work: it produces bad code in the "destination" language. Instead, use the original as a specification, and rewrite the code for the new language. That way, you get good code that does the same job.
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!
|
|
|
|
|
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
|
|
|
|
|
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()); a.SetValue(42);
Console.WriteLine(a.GetValue()); }
}
I cannot imagine you had a look at C# without having to instantiate any class at least once. Maybe because you tried to copy some existing code which was not written with OOP in mind? You should take it from the ground, and follow some basic tutorials about C# and OOP not directly related to your task; this way you may get some important concepts that you will apply later to your actual case.
noop()
|
|
|
|
|
Thanks phil for the example code, I'll study it.
I find code examples useful in learning how C# works and can be applied to my own code.
Brian
|
|
|
|
|
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!
|
|
|
|
|
Thanks Griff.
I like the way you use examples.
The reason why I was thinking of having an inventory class was to keep track of objects that the player picked up, like the trousers that keeps a wallet and other things. If the player drops an object then the object is removed from the player and appears in the current room.
There would be a limit of either the number of objects the player can carry or the maximum weight the player could carry which would be handled by the inventory class. If the player picks up gold then the inventory class would increase the players score. Some objects can't be picked up such as a building, water, etc so the inventory class would need to check if the object is moveable. Also the inventory class checks to see if the player is already has the item to be picked up.
What I wanted to ask you if this is a good reason to have an inventory class? It tends to separate the code away from the main() class code.
Brian
|
|
|
|
|
Brian_TheLion wrote: The original code was written in the BASIC language back in the 1980's
There's the problem. You're trying to force the old, non-OPP code way of doing things into an OOP world where it just doesn't work.
You cannot do a line-for-line conversion. You have to understand what the INTENT of the old code and rewrite using modern techniques. This is will result is radically different code because BASIC is NOT VB.NET, or C#, or Java, or C++.
You know that your app needs an inventory. How that's implemented in the new version is going to be done VERY differently from how your existing BASIC code implemented it.
Brian_TheLion wrote: Someone suggested that I should look at instances of classes
You create instances of classes all the time. Every time you "new up" a class. For example:
IInventory playerInventory = new InventoryManager();
|
|
|
|
|
I agree with you Dave as I tried a few years ago to convert the Basic code program into Visual Basic code. I now have a good idea of how the code works so I don't need to copy the original code but will do a rewrite that fits into the structure of C#.
It seems that programs like C# make for a better programmer as you don't have the lazy ways of writing code like spaghetti with all its goto commands. The like the structure approach of languages like C#.
Maybe the next thing I need to do is to work out all classes I need and what code will be as methods in the main() class. I could write all of the code in the Main() class but in doing so I would gain any experience about using classes and the advanages of using classes.
Brian
|
|
|
|
|
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
|
|
|
|
|
Brian_TheLion wrote: I have not completely given up on C# You cannot "give up" on something you have not invested hard work in, something you have spent more time writing questions about than you have spent studying it.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Hi Bill.
It might seem that I have not done much reading on C# but that is not the case. I've borrows 4 books on C# from the library and have been studying them. However it's difficult to find a good book on a on C# that explain it well and books such as "Beginning Visual C# 2015 Programming" don't do a good job at explaining how OOP works in C#.
Even after reading I still need to ask questions to get a better understanding on parts of C#. As well as this group I've also found Google useful for some of my questions on C#.
Brian
|
|
|
|
|
Brian_TheLion wrote: 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.
|
|
|
|
|
Hi Richard.
No I'm not blaiming C# at all, all I'm looking for is a way to use this programming language that suits my needs.
I think it's because I'm use to older languages that had less rules such as quick basic.
As a hobby programmer I want to improve on any programming skills I have so I turned to C# and invested my time in trying to learn this language.
I understand it's the type of language that can't be learnt in a couple of weeks and slowly things that did not make sense at first are starting to make more sense now.
I'm still trying to find a way of one class changing the variables in another class if that's possible.
As for my message about almost all programs requiring a user interface...
You have a choice when writing a c# program of a console program that uses a dos window or a program that has buttons that the user can click on. I don't know of any program sold these days that is a console program.
Brian
|
|
|
|
|
Brian_TheLion wrote: I'm looking for is a way to use this programming language that suits my needs And there is another large problem. You are basing you "needs" on a legacy system written in basic and hoping to convert it to a modern language and development style. You should really accept that you are up for a rewrite.
Look only at the functionality of the original game NOT the code that does the work in basic. If you are going to use a modern OOP language you will need to understand OOP principles BEFORE you begin trying to design the application.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Hi Mycroft.
I'm doing my best to try and understand OOP from reading and studying examples.
The code that was originally written in BASiC Will be pulled apart and put to geather so that it works under the more modern C# structure, so it is like you say a rewrite. I have a good knowledge of how the code works to do a rewrite.
Brian
|
|
|
|
|
Brian_TheLion wrote: Will be pulled apart and put to geather so that it works under the more modern C# structure It feels like your thinking is incorrect, don't consider pulling the old code apart and restructuring it. Only inspect the code to help you define the functionality of the application NOT how it should be put together.
Seriously do not try and apply basic methodology and structures to c#, everything is now an object with properties and methods. Define your objects...
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Hi Mycroft.
I may have not used the best term when I said "pulled apart" I was meaning studying the original code to see how it worked to get ideas on how the adventure should work for the new code.
Having used programs like Quick Basic over the years makes it more difficult to break free from the procedure programming method, but I don't give up that easierly.
Brian
|
|
|
|
|
Brian_TheLion wrote: Having used programs like Quick Basic over the years makes it more difficult to break free from the procedure programming method
I, too, started on procedural languages like FORTRAN, COBOL and BASIC and initially found the Object Oriented way seemed artificial, clunky and back-to-front. Now, having spent several years in an OO environment, I can see the benefits and would never go back to the old ways. It can be hard to unlearn things that work and to undo your mental images of how things interact, but it is worth it in the long run.
I'd suggest that you park your adventure game and write something from scratch, forcing yourself to use the OO paradigm (horrible word) and then evaluate what worked well and what seem odd. Then revisit your adventure game looking at the objects (players, inventory items, rooms) and seeing how they map onto classes and then look at their properties (inventory items are-in rooms, players -have- inventory items, players are-in rooms, rooms are-adjacent-to- other rooms) then look at methods that change the properties (players move-to rooms).
|
|
|
|
|
Good advice thanks jsc42.
My Text adventure program is more involved than a simple game so it might be better to try writing some less challenging programs first such as a number guessing program to get some practice.
Brian
|
|
|
|
|
Hi Mycroft.
I'm considering everything at the moment which is why I have not started to write my program in C#.
Brian
|
|
|
|
|
Brian_TheLion wrote: I'm looking for is a way to use this programming language that suits my needs. It will. C# is a rich language that can handle just about any problem you can think of. But, as I and others keep saying, you need to learn and understand the language, and its rules, first.
Brian_TheLion wrote: I'm still trying to find a way of one class changing the variables in another class if that's possible. That is what Properties and Methods are there for.
|
|
|
|
|
Hi Richard.
That's what I'm trying to do by reading books on the subject and asking questions.
I read somewhere that C# is starting to spread to programming apps on tablets.
Brian
|
|
|
|
|
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.
|
|
|
|
|