I've managed to make a bit of progress in C# But I'm currently stuck

Started by
13 comments, last by Khaiy 13 years ago

[quote name='avelle' timestamp='1302800594' post='4798467']
[quote name='GraySnakeGenocide' timestamp='1302798010' post='4798448']
When removing static from both methods, I get the following error:

"An object reference is required for the non-static field, method or property "ConsoleApplication1.Program.ToFahrenheit(string);


My fault, I don't write console applications so ignore what I said there and keep them static. "static void ToFahrenheit(string fTemp)" and the same for Celsius should work though.
[/quote]

Its not that its a console app, its that Main() is a static function. Unless the conversion functions are put into a class and that class is created in Main or the functions are declared as static then Main will never be able to find them.
[/quote]

Aha, I forgot all about that. I thought it probably had something to do with Main() but couldn't remember what it was exactly. Thanks for clearing that up.
Advertisement
You need the methods to be static because you aren't instantiating any objects. A static method operates from the class itself, while a non-static method is called from a specific object of the class that has already been created. Your project has a single class (Program), and you don't actually create an object of type Program (which is fine for what this program does). But that means that any method you call will need to be static. Does that make sense?

Also, do you feel like you've got a better grasp on return types and parameters now? If not, I'd be happy to try to explain them.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~


You need the methods to be static because you aren't instantiating any objects. A static method operates from the class itself, while a non-static method is called from a specific object of the class that has already been created. Your project has a single class (Program), and you don't actually create an object of type Program (which is fine for what this program does). But that means that any method you call will need to be static. Does that make sense?

Also, do you feel like you've got a better grasp on return types and parameters now? If not, I'd be happy to try to explain them.


I'm still shakey on how the parameters work, as well as the return types.

As Avelle mentioned: "Also you're returning an integer when your method return type is a string."

I try having the return types as ints, and it caused the "cannot convert errors.

So I have a long way to go.

But yeah, if you could help me understand the parameters/return type stuff better i'd appreciate it.

[quote name='Khaiy' timestamp='1302804769' post='4798493']
You need the methods to be static because you aren't instantiating any objects. A static method operates from the class itself, while a non-static method is called from a specific object of the class that has already been created. Your project has a single class (Program), and you don't actually create an object of type Program (which is fine for what this program does). But that means that any method you call will need to be static. Does that make sense?

Also, do you feel like you've got a better grasp on return types and parameters now? If not, I'd be happy to try to explain them.


I'm still shakey on how the parameters work, as well as the return types.

As Avelle mentioned: "Also you're returning an integer when your method return type is a string."

I try having the return types as ints, and it caused the "cannot convert errors.

So I have a long way to go.

But yeah, if you could help me understand the parameters/return type stuff better i'd appreciate it.
[/quote]

The reason it threw that conversion error when you changed the return type of the function to an int is because in Main the variable temp is a string and it won't allow you to assign an int as a string.

I'm sure someone else can better explain this but I'll make an attempt as well. Parameters are just want you want to pass to your function, it can be any kind of data type or object. In your earliest example of code:


Console.WriteLine("Fahrenheit it is!, Please enter a temperature to convert: ");
int temp;
temp = int.Parse(Console.ReadLine();


you defined temp as an integer. In general you would have wanted your ToFahrenheit method to accept an integer parameter instead of a string. Frankly I'm dumbfounded that it let you return an integer in a function that had a string return type without screaming bloody murder. I'm not sure if this is a C# thing, but I was always under the impression that what you returned HAD to match the return type. Anyways, as far as I know, it's best practice to match what you return with the declared return type of the function, but I'm sure there are much more experienced programmers on here that can tell you for sure.
I'm still shakey on how the parameters work, as well as the return types.
As Avelle said, a parameter is just the kind of thing that you want passed to your function, and what it'll be called in the function definition. So a function definition like:

<BR>static void Function(int reps, string thing) <BR>{ <BR> int i = 0; <BR> <BR> while (i < reps) <BR> { <BR> Console.WriteLine("{0}", thing); <BR> } <BR> <BR> Console.WriteLine("The string {0} was printed {1} times.", thing, reps); <BR> i++;<BR>}<BR>

takes two arguments (a parameter is the type of thing passed to the function, an argument is the thing that is actually passed). The first has to be of type int, and will be called "reps" in the rest of the function definition. The second must be of type string and will be referred to as "thing" in the rest of the definition.

In the program, you would call the function like this:

<BR><BR>// Other code <BR><BR>int number = 12; <BR>string text = "whatever"; <BR><BR>Function(number, text); <BR>

So when you call the function as above, the arguments passed fit the parameters in the method signature (the function, return type, modifiers, and parameters, but not the definition) and the function executes, using the number "number" (hard coded to 12, in this case) everywhere "reps" appears in the function definition and the string stored in "text" everywhere "thing" is in the definition.

That's about it for basic parameters, they define what arguments the function can accept, and provide a generic name to list for those arguments when defining the function.

A return type is sort of what the function actually produces. "Void" as a return type means that the function doesn't return anything, all that happens is whatever's in the function code. With a return type other than void, the function does whatever it does and then presents the result to whatever called that function in the first place. The return type has to match the type of the value returned, although C# can do some type casting behind the scenes for you in some situations. So if you want to have

<BR>int theNumber = CalculateNumber(); <BR>

then the return type of the CalculateNumber function will have to be an int, because that is what theNumber's type is. You can get away with having it be something like a float, because C# is good at implicit type casting compared with some other languages, but it's a better practice to define things as you need them to be and do any type casting explicitly in your code.

C# can't automatically convert from string to int for you though. When you use the int.parse() function, the program converts the argument from a string to an int. You never change the contents of the fTemp or cTemp variables from the string passed as the argument (they're passed to the functions as strings), so if you return them your return type will have to be a string as well or else you'll get an error, as you saw. You could return your farenheit and celsius variables (rather than fTemp or cTemp) if you want to return an int, because they are the string arguments parsed to integer values.

In this particular case, I don't think that you need a return value for these functions (return type void should be fine), because you don't use a value stored in "temp" after the function call anyways. The function itself does the conversion and prints the result to the screen, and then that's it-- so there's no reason to re-assign the value of "temp" after the conversion.

I hope that that helps clear things up a bit for you. The general syntax stuff can be confusing at first, but once you've got it down things will be a lot clearer.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

This topic is closed to new replies.

Advertisement