Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


avelle

Member Since 03 Apr 2011
Offline Last Active Apr 15 2012 03:01 PM

Posts I've Made

In Topic: need some help with code(noob)

12 April 2012 - 08:26 AM

Hi Scrap, I just wanted to mention something that would hopefully make your life easier in the future. When comparing characters such as y, Y, n, and N, you can force the comparison to be either all lower case or all upper case which would be useful in this instance.

You could turn the following:
if (again1 == "n" || again1 == "N")

into:
if (again.ToUpper() == "N")

and it would take whatever was in the variable 'again' and capitalize it for the comparison. It's important to note that it wouldn't save it as upper case beyond this statement. For that you would need to do
again = again.ToUpper()

I hope that's helpful.

In Topic: [.net] .NET in .exe?

16 August 2011 - 12:26 PM

The PC(s) you intend to execute the program on need to have the .NET framework version (1.1, 2.0, 3.5, etc) you used to compile the program.

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

14 April 2011 - 12:46 PM


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.


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.

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

14 April 2011 - 12:09 PM



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.


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.


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.

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

14 April 2011 - 11:03 AM

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.

PARTNERS