[RESOLVED] Beginning C# - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 43

Thread: [RESOLVED] Beginning C#

  1. #16
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Beginning C#

    Quote Originally Posted by Zaccheus View Post
    Have a look here for MS's recommended c# naming conventions:
    http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx


    I think traditionally a function returns a value and a method does not.
    No, that was a stupid thing that came about in VB. The terms are interchangeable, C# typically uses the term 'method'.

  2. #17
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Beginning C#

    I thought it came from Java, but you are right that in C# everything is called a method.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #18
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Beginning C#

    You may be right, I was thinking of the way that VB discriminates between "Functions" and "Subroutines", which is stupid.

  4. #19
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    well i originally learn C++ console programming and my teacher taught me to call them funcitons and thats why i didnt know if that was acceptable in C# or not.

  5. #20
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    okay so now is the ultimate concept i am needing to learn from the beginner project of making a calculator. reading keystrokes off of a keyboard! so i guess first question where should i start?

  6. #21
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Beginning C#

    Didn't your course / teacher tell you where to start?

  7. #22
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    alright i found a page on handling keyboard input on MSDN. so i made my little keystroke handler and it handles all my strokes for 0-9 plus the operations (+, -, *, /, &#37 but how do i handle the enter key?

  8. #23
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    BigEd781
    i dont have a C# instructor. i am my own teacher everything i do with this language i do on my own. so no one has told me anything (except what you guys post to all my dumb question, thanks for doing that by the way). the computer science teacher at my high school lent me a book on C# but it does not cover handling raw keystrokes in it. i've looked. the book is "Microsoft Visual C# 200 Step by Step" by "John Sharp". its a nice book for helping you use text boxes and stuff but i am using only one text box and the user does not input any form of data into it. only the program has the accesibility to do that.

  9. #24
    Join Date
    Oct 2010
    Posts
    12

    Re: Beginning C#

    You need to read up on the KeyPress event handler. I would also recommend you look at Regex as it is REALLY helpful later on, its very simple to get used to and also you can print a "cheat sheet" about it for later use. Still use mine


    Hope this helps

  10. #25
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Beginning C#

    See this & this.

    Quote Originally Posted by rockking View Post
    i fixed my own error by reading it in the post.
    I'm glad you figured it out on your own. About what you said earlier - that you didn't understand what "current context" meant: it just told you that a variable or class or anything else with that name was never defined in the context of that function (not anywhere in the scope of the function, or in the enclosing scope). The function didn't "know" about it.

    Quote Originally Posted by rockking View Post
    here is the actual method for calculating. obviously its primitive at the moment (only supports two operands and one operation) but its a start
    Code:
                gbl_result = gbl_operand[0] + gbl_operation[0] + gbl_operand[1];
                gbl_result -= gbl_operation[0];
                txtShow.Text = gbl_result.ToString();
    now could i make a for loop to subtract the values of the operations? so it would look like this
    Code:
                gbl_result = gbl_operand[0] + gbl_operation[0] + gbl_operand[1];
    for(int i = 0; i < gbl_operation.Length; i++)
    {
                gbl_result -= gbl_operation[i];
    }
                txtShow.Text = gbl_result.ToString();
    would that be technically the proper way to do it?
    I don't think it's going to work, not like that. I don't think you're clear about how arrays should be used. BTW, you should be performing operations on operands, so I'm not sure what you tied to achieve with gbl_result -= gbl_operation[i].

    Quote Originally Posted by rockking View Post
    so i have my calculator and it support up to 11 operands. each conataining at most 9 values.
    What do you mean by "up to 11 operands. each conataining at most 9 values"? Like up to 9 digits? Or what?
    For starters, I recommend that you try and get it to work with simply using a variable of type double. Don't worry about the digits for the time being - this would unnecessarily complicate matters somewhat, and it wouldn't contribute to your understanding of the language.

    Quote Originally Posted by rockking View Post
    obviously i dont want a switch statement or a bunch of else clauses. i will have to post some code here on what i am trying to do and i will list the errors that will most likely come along with it.
    Quote Originally Posted by rockking View Post
    here is a better way of putting. basically can i have an array value set to a character and then when i hit a button to run a specific method it reads that array and says "thats for adding" and it adds but then it runs and goes "thats subtract" and i hope you get the idea by now i will post the code for what i wanna accomplish.
    But when the method 'says "thats for adding" and it adds but then it runs and goes "thats subtract"...', that precisely implies a bunch of if-else clauses

    But, I'm glad you asked, because this is a perfect opportunity to introduce some concepts that will show you how classes and objects actually work together, while keeping it simple at the same time. I'm saying this because the calculator example is simple enough, and can be used to show these concepts without it feeling ill-contrived.

    Can you implement something like this (more explanation below)?

    Code:
    [  class Calculator....]..........NOTE: This class is probably called Form1 in your project.
    ------------------------
    // fields...
    - m_currentSum
    - m_currentEntry
    - m_operation  ----------------------------------->[ abstract class Operation      ]
    ------------------------                           ---------------------------------
    // methods & Event handlers                        + int AcceptOperand(int operand).........NOTE: Also abstract.
    ------------------------                           ---------------------------------
                                                                     /\
                                                                     |
                                                                (subclass of)
                                                                     |
                    ......[class AddOperation]-----------------------+
                    .                                                |
                    ......[class MultiplyOperation]------------------+
                    .
                    .
                    .
                    NOTE: (-), (/) and (&#37;) can be implemented using
                    AddOperation & MultiplyOperation, but you can
                    implement them separately if you want.
    
        Side NOTE: - denotes 'private',  + denotes 'public'
    So, the big deal here is what's called inheritance and composition.
    Inheritance models the "is a (kind of)" relation, as in "AddOperation is a (kind of) Operation".
    Composition, without getting into details, simply means that Operation is somehow a part of, or is at leas referenced by the Calculator.
    These two will enable you to avoid the if-else clauses.

    Assuming that the Calculator class is the Windows Form where you implemented the calculator UI, it would contain a bunch of handler methods for the buttons. When a number is entered it is first stored in m_currentEntry, and m_currentSum initialy has the same value. When the user presses, say [+], the [+] button handler will set m_operation to an instance of AddOperation (the first operand (m_currentSum) is passed at this point). Then, when the second number is entered, it is stored to m_currentEntry, and finaly, when [=] is pressed, the [=] button handler will simply say:
    m_currentSum = m_operation.AcceptOperand(m_currentEntry);
    Note that m_operation is of the type Operation, and it doesn't "know" what specific kind of operation it is. It just works (because the subclasses override AcceptOperand(), which does the actual calculation).

    Bear in mind that this is obviously designed for illustrative purposes.
    (Some would probably say that this is an overkill for a simple calculator app.)

    The benefits:
    • You can modify the Calculator and Operation-classes independently.
    • You can add new types (subclasses) of Operation.
    • You can change an existing implementation of an OperationXYZ class without ever having to touch the Calculator class, assuming that the interface stays the same.


    For example, you can later decide to add an SqrtOperation that would work on a single operand, in which case it could be initialized with the default (parameterless) constructor. Or you can add RaseToPowerOperation. Or you can, after some changes, implement a CompositeOperation that would link several operations together, thus performing some custom processing of the initial input.

    These are slightly advanced topics, and I suggest that, once you've learned the basic syntax and language constructs enough, you start learning something about OO programming, inheritance, composition, and such...
    A word of waring: inheritance is powerful, but it shouldn't be overused - prefer composition when possible. Actually, make sure you thoroughly understand the concepts before you decide to use one or the other.

    Also, check out the Math class, you will want to use it.
    Last edited by TheGreatCthulhu; October 12th, 2010 at 06:18 AM.

  11. #26
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    well thanks everyone for helping me. i have almost finished the calculator. i know its way beginner but i found that the concepts behind it are very important and help me with my future C# plans(XNA). i am slowly working my way into this book by John Sharp and i am now up to the point of getting the ADO.NET going. i need to work on finishing my calculator before i jump into that project though. keep up on this post or any other posts i leave here i will almost always be in need of assistance with my code. i have calculator working it now reads input from the keyboard(including &#37 which is nice. i have one little problem with my keyboard reading though. HOW ON EARTH DO YOU READ THE ENTER KEY!? i cant get it to work and the keypress doesnt allow more than one character between '' i tried to put 'CR' but it threw the error of to many characters. so what is the ASCII code for the enter key and how do i make the enter key be only one thing and not a click on the last button used? this will almost finish the project.

  12. #27
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Beginning C#

    Long is way to XNA
    OK, you might figure out how to do some of the simpler stuff, but beyond that you'll need experience with stuff that doesn't necessarily have to do much (or anything) with XNA - I'm talking about the need to understand how different parts of code work together, then general stuff related to design, the ability to understand how other people have designed the code you want to use, and the ability to find and understand information.
    The thing is, no API or framework will do all the things for you: you need to build on top of them.
    But don't get me wrong, I by no means want to discourage you. Be persistent!

    As for the enter key, are you using the KeyPress event? (You shouldn't.)
    See the very first line of my previous post, with the two links.
    BTW, you can't say 'CR' in C# because the ' ' denote a char literal, and CR are two chars, which technically makes it a string. (Note that the same applies in C++.)
    There's something called escape sequences (C++ has these too), one of which is '\r' (NOTE: this is treated as a single char) and it corresponds to carriage return - but this wouldn't work either, because the KeyPress event is not raised by noncharacter keys.

    Also, before you jump into ADO.NET, or any other stuff, i recommend that you familiarize at least with the basic concepts of OO-programming, such are inheritance, abstraction, polymorphism, encapsulation, composition etc... These are all related with how classes cooperate, and any C# book should talk about these topic at least a little. You'll certainly benefit from this knowledge later on.

    I'd like to see how you coded the calculator app - if you would like to share?

  13. #28
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    like i said XNA is a future plan(mainly a summer hobby and then on to college wish me luck there i still got a year of high school ) i did read your last post more clearly and did find the links. i skipped them i didnt even see the first line so thanks for telling me that. i looked at the overrides for the KeyPress event and i do believe that will be what is needed to handle the very thing i need. also will that completely override the enter key for the calculator? so if i click the button for 3 but then hit enter will it click another 3 or just the equal? i hope the equal. and i looked at the escape sequences. i remember those from C++ console programming. i used them quite a bit especially for writing to .txt files. i will post the code here once i finish the enter key and the decimals (needs counter and some more code) but i have no problem showing the code for critiques.

  14. #29
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,254

    Re: Beginning C#

    One thing to learn as you transition from high school to college is how to write with punctuation and proper grammar.

    Not trying to be mean here, but writing like you are texting isn't going to get you far in college.

  15. #30
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: Beginning C#

    I understand that. I have to work on that some more. Anyway I am using the whole KeyDown method I guess you could call it but it doesn't seem to work the way I need it to. How do I switch "KeyEventArgs e" to a string or int?
    Code:
            void Calculator_KeyDown(object sender, KeyEventArgs e)
            {            
                switch (e.ToString())
                {
                    case "Enter":
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                    case "Return":
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                }
            }
    I also tried this:
    Code:
            void Calculator_KeyDown(object sender, KeyEventArgs e)
            {            
                switch (e.KeyValue)
                {
                    case Keys.Enter:
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                    case Keys.Return:
                        {
                            btnEqual.PerformClick();
                            break;
                        }
                }
            }
    The second one returns these errors:
    Code:
    Error	1	Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'int'. An explicit conversion exists (are you missing a cast?)	F:\CS_Calculator\CS_Calculator\Calculator.cs	383	22	CS_Calculator
    Error	2	Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'int'. An explicit conversion exists (are you missing a cast?)	F:\CS_Calculator\CS_Calculator\Calculator.cs	388	22	CS_Calculator

Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center