Jump to content
  • Advertisement
Sign in to follow this  
jwbowyer

Calling a method in C#

This topic is 3756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just have a small question related to C#. I'm going through the book "Head First C#" and am currently at the beginning of the third chapter. The book had me write a windows application that took a string which was inputted in a text box, and then threw it back out in the form of a message box the number of times the user indicated. After the user hit ok on that message box, another message box would pop up showing the amount of characters that were in the first message box. The program worked out perfectly, but I just need one verification. My form1 class contains the button1_click method.

        private void button1_Click(object sender, EventArgs e)
        {
            int len = Talker.BlahBlahBlah(textBox1.Text, (int) numericUpDown1.Value);
            MessageBox.Show("The message length is " + len + " characters");
        }


This method calls the BlahBlahBlah method in a separate class named Talker.
 class Talker
    {
        public static int BlahBlahBlah(string thingToSay, int numberOfTimes)
        {
            string finalString = "";
            for (int count = 1; count <= numberOfTimes; count++)
            {
                finalString = finalString + thingToSay + "\n";
            }
            MessageBox.Show(finalString);
            return finalString.Length;
        }
    }
}

Now my question is that when the method is called the two variables used are textBox1.text string and the integer numericUpDown1.Value. But the actual method uses two completely different variables, the string thingToSay, and the integer numberOfTimes. Are the values of textbox1.text and numericUpDown1.Value stored automatically in thingToSay and numberOfTimes? And if so, why are two totally different variables used instead of the initial ones that were used when the method was called? Also I noticed one oddity when the method was called. This line: int len = Talker.BlahBlahBlah(textBox1.Text, (int) numericUpDown1.Value); Why is the (int) in there? I get two odd errors if I take it out, one is that I'm trying to a decimal into an integer, and one is something about an overload. I'm guessing that the first error means that the numeric box stores things as a float... But the second I don't get. I've also noticed that the parentheses are absolutely necessary why is that?

Share this post


Link to post
Share on other sites
Advertisement
Well actually something different happens for the string parameter and the int parameter. You see in C# "string" is a reference type, while "int" is a value type. When you pass a string to a method, that method's parameter receives a reference to the same string. However when you pass an int, the value of that int is copied to parameter. This means changes to the int in that method won't be reflected in the original variable. If you want, you can use the keyword ref for a value type, then changes to the parameter will be reflected in the original.

The reason you have to cast the "Value" property to an "int" because "Value" returns a value of type "System.Decimal". The Decimal type is used for storing floating-point number with very-very-high precision, for instance in financial calculations. You must cast it to an int, since they're not the same type.

Share this post


Link to post
Share on other sites
Need to clear something up just to make sure that the new C# programmers understand it.

When you pass parameters to a method and you do not include either the ref modifier then the parameters are passes by value (that means a copy of the value is passed into the method). For value types is pretty much always works as you would expect. For reference types it can get a bit more complicated. Without the ref you are passing in a copy of the reference. But does it really matter? In most cases no. But in some cases it can be a big issue.

Take the following.
[source lang="C#]
MyClass aclass = new MyClass("A");

SomeMethod(aclass);

void SomeMethod(MyClass in_class)
{
in_class = new MyClass("B");
}





So is aclass; A or B. It is A. The reason is that we passed a copy of the reference not the actual reference. So in SomeMethod, we changed the copy of the reference to the new MyClass but aclass is not changed.

Just keep it in mind.
theTroll

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!