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

Thread: [RESOLVED] Beginning C#

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

    [RESOLVED] Beginning C#

    hey everyone.
    i am new to C# and i'm wondering if you can call a function or a method i guess is what its called but can i call one on load that has a "trigger" that runs a for loop to clear an array? heres what i have for my form load:
    Code:
            public void calculate()
            {
                if (gbl_trigger[0] == 0)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        operand[i] = 0;
                        button15.Text = i.ToString();
                        gbl_trigger[0] = 1;
                    }
                }
            }
        
    
            private void Calculator_Load(object sender, EventArgs e)
            {
                gbl_trigger[0] = 0;
                calculate();
            }
    i am using the .NET framework 4.0 and Visual Studio 2010
    Last edited by rockking; October 8th, 2010 at 12:33 PM.

  2. #2
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Beginning C#

    Quote Originally Posted by rockking View Post
    hey everyone.
    i am new to C# and i'm wondering if you can call a function or a method i guess is what its called but can i call one on load that has a "trigger" that runs a for loop to clear an array? heres what i have for my form load:[...]
    Wait... What exactly is your question? Can you formulate it a bit better?
    Does the code work for you? Have you tried it? Are there any errors? What are they saying?

    Meanwhile, here are a few remarks:

    Quote Originally Posted by rockking View Post
    [...] call a function or a method i guess is what its called [...]
    Technically, in C# it's called a 'method', but traditionally such a construct is called a 'function' - a term that developers familiar with other languages might prefer, so you can say whichever you like better, people will understand.

    Quote Originally Posted by rockking View Post
    Code:
                if (gbl_trigger[0] == 0)
    A word on that: don't give your variables such names, it's a bad practice. This could be OK for a small application where there would be only one trigger, and where it's meaning would be obvious. However, since you used an array, I'm guessing there's more than one trigger.
    Imagine now that you suddenly decide to work on this new super-cool idea instead, and you start a different programming project, forgetting all about the current one for a while. Then, after some time, you decide it's time that your old still-on-hold projects gets some attention. But, hey, you weren't involved with it for some time, and you've forgot a thing or two. And than you see that, an go:
    What to hell do gbl_trigger[0], gbl_trigger[1], gbl_trigger[2], ... trigger?

    Then a (physical or virtual) pile of paper later, you realize it would have been much wiser if you gave the variable a more descriptive name in the first place.
    (Not to mention what would happen if a different programmer was assigned with the task to continue developing your project.)
    In relation to what I said, an array is probably not the best construct for the job: an enum would do much better.


    Quote Originally Posted by rockking View Post
    Code:
                if (gbl_trigger[0] == 0)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        operand[i] = 0;
                        button15.Text = i.ToString();
                        gbl_trigger[0] = 1;
                    }
                }
    It seams that you're trying to use gbl_trigger[0] to control the behavior of the loop and/or button. This looks very ugly.
    But again, before I (or someone else here) can suggest a better approach, you need to rephrase your question into something more coherent, and give some more detail.
    Last edited by TheGreatCthulhu; October 8th, 2010 at 04:55 PM.

  3. #3
    Join Date
    Dec 2009
    Posts
    596

    Re: Beginning C#

    In answer to your two questions: Yes you can.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Beginning C#

    Well, OK - now that I've read it more carefully, I can see what's the OP's question.
    But still, it would be good if the OP posted some additional details on what he/she wants done.

    There are a few points that need attention.
    For example, why the OP feels that the array needs to be zeroed-out on load?
    Or why the OP didn't use the operand.Length property, instead of the literal 10 in the loop condition?

    Or why is the line
    gbl_trigger[0] = 1
    inside the loop?
    Why the OP feels there's a need for such a trigger?

    Why the OP used an array for the trigger variable? Are there other triggers? Is an array a good choice?

    Is calculate() the best name for the method, since currently it doesn't calculate anything?
    Maybe it'll be changed in the future, so the name is actually valid?

    These are all questions that could be discussed, and answering some of them could give the OP a more thorough understanding of the language. But this requires that the OP provides some more detail.
    Last edited by TheGreatCthulhu; October 9th, 2010 at 07:46 PM.

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

    Re: Beginning C#

    okay thanks for these comments. i see what you guys mean by all those things. and also i understand just by looking what "operand.Length" would do. i will definately be using that on this project. i did rename the calculate() method to initialize() its called and it works correctly i think. i just have the bad habit of ensuring that all my variables are set to the value i want and also, i realized that you cant change the text property of a button from within the actual application. took me a few good self curses to figure it out. now i replaced "button15" with txtShow its a text box. i am used to some Visual Basic programming so dont be surprised if i label my stuff kinda like this "txtShow". and just so you know since i am so knew to C# i am making a basic calculator.

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

    Re: Beginning C#

    also im sure you can answer this but if i have a method above the method im calling will it call successfully or do i have to have a prototype at the top to link them like i do in C++
    ex:
    Code:
    void main();
    void program()
    {
    main();
    }
    void main()
    {
    //Main program here
    }

  7. #7
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Beginning C#

    Quote Originally Posted by rockking View Post
    and also i understand just by looking what "operand.Length" would do.
    The main thin with that is - what if later you decide that the operand array should have 20, or 30, or only 5 members? You would have to find all the places where you've written 10 and make the change. (This isn't much of trouble if there's only one such place, but usually there will be more - or way more; and besides that, the code could change more than once.)

    Quote Originally Posted by rockking View Post
    i just have the bad habit of ensuring that all my variables are set to the value i want
    That's not such a bad habit. OK - in C# all the array members would be set to their default values of zero, but better safe than sorry...

    Quote Originally Posted by rockking View Post
    and also, i realized that you cant change the text property of a button from within the actual application. took me a few good self curses to figure it out.
    What do you mean? You should be...
    Are we talking Windows Forms?
    How have you tried to do it?

    Quote Originally Posted by rockking View Post
    also im sure you can answer this but if i have a method above the method im calling will it call successfully or do i have to have a prototype at the top to link them like i do in C++
    ex:
    Code:
    void main();
    void program()
    {
    main();
    }
    void main()
    {
    //Main program here
    }
    No, in C# you don't need any forward declarations - once it's written, it's accessible from anywhere within the same assembly (if you're using the VS IDE, read: project), or from any of the referencing assemblies - assuming it's public or accessible via inheritance.
    Last edited by TheGreatCthulhu; October 10th, 2010 at 07:03 PM.

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

    Re: Beginning C#

    okay so i have tried to change the text properties actually. when i first started my calcualtor (i know very beginnerish) i had the result area as a disabled button. i figured it looked better as a disabled button than as a disabled textbox. but everytime i hit one of the numbers the code threw out an exception and crashed. why it did it idk it just kept telling me something about the value not being in the right paramaters or something. idk i gave up on the inactive button and went to a textbox.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Beginning C#

    Does it work now? If so - then OK, problem solved.
    However, the best thing to do when an error appears is to try and understand the reasons behind it. It's not always easy, but for a beginner, these errors can usually provide valuable insight into the language, because they are often related to some concept not being well understood.

    Furthermore, C# error messages are pretty straightforward most of the time - and usually lead you right to the very heart of the problem. Keeping this in mind, you can see why it's best to always read them carefully, and why it's important to post them here if you can't figure them out.

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

    Re: Beginning C#

    lol alright i will definately do that from now on. and so with that all being said i am running into an issue. more or less a question. so i have my calculator and it support up to 11 operands. each conataining at most 9 values. between each operand i will have a operator (+, -, *, /, &#37 i have the operators as an array because it seems like it would be easiest to make a statement. ex:
    Code:
    gbl_result = gbl_operand[0] + gbl_operation[0] + gbl_operand[1];
    so on and so forth until you reach the maximum entered. there is however the suspicion of how do i identify each possible operation to be set to a specific value? i have five values but i can have up to i think 7. 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.

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

    Re: Beginning C#

    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.

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

    Re: Beginning C#

    okay here goes first shot at getting the statements to work. i have two primarily used variables. "gbl_operand" and "gbl_operNum" here is the method for the number 6
    Code:
            private void button6_Click(object sender, EventArgs e)
            {
                gbl_number[6] = 6;
                gbl_number[0] = (gbl_number[0] * 10) + gbl_number[6];
                gbl_operand[operNum] = gbl_number[0];
                txtShow.Text = gbl_operand[operNum].ToString();
            }
    it gives me the following error though and says that:
    Code:
    Error	1	The name 'operNum' does not exist in the current context	F:\CS_Calculator\CS_Calculator\Calculator.cs	44	38	CS_Calculator
    do you understand this error because i dont understand what it means by current context.


    "gbl_operand" is an integer and "gbl_operNum" is an integer
    declarations
    Code:
            int[] gbl_trigger = new int[5]; //global trigger
            int[] gbl_operand = new int[9]; //global operands
            int[] gbl_number = new int[11];//global number to be assigned to operand
            char[] gbl_operation = new char[5]; //global operation character
            int gbl_result; //self explanatory maybe, global integer for final result
            int gbl_operNum; //suppose to be global integer that is used to determine the current operand: initialized to 0 during form load
    Last edited by rockking; October 10th, 2010 at 08:20 PM.

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

    Re: Beginning C#

    haha wow do i feel stupid i fixed my own error by reading it in the post. i used the wrong variable name. i feel like a complete idiot now.

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

    Re: Beginning C#

    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?

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

    Re: Beginning C#

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

    Quote Originally Posted by rockking View Post
    a function or a method
    I think traditionally a function returns a value and a method does not.
    My hobby projects:
    www.rclsoftware.org.uk

Page 1 of 3 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center