Basically I want my main method to be able to pass the type and sound of the animal through the SomeMethod parameters.
What I have at the moment:
[code]
public class Animal
{
private string type;
private string sound;
public Animal (string type, string sound)
{
this.type = type;
this.sound = sound;
}
public static void SomeMethod()
{
Animal dog = new Animal("dog", "woof");
Console.WriteLine("new {0} makes the sound {1}",dog.type, dog.sound);
}
}
[/code]
My thinking:
[code]
public class Animal
{
private string type;
private string sound;
public Animal (string type, string sound)
{
this.type = type;
this.sound = sound;
}
public static void SomeMethod(Animal.type, Animal.sound)
{
Animal dog = new Animal();
Console.WriteLine("new {0} makes the sound {1}",dog.type, dog.sound);
}
}
[/code]
Is this java?
You use the constructor to do that, why make "SomeMethod" to do the same?
or do you want to print the current stuff?
[code] public class Animal{
private string type;
private string sound;
public Animal (string type, string sound){
this.type = type;
this.sound = sound;
}
public static void SomeMethod(){
Console.WriteLine("new {0} makes the sound {1}",this.type, this.sound);
}
}[/code]
Language helps. A lot.
Sorry! C#!
[code]
public class Animal
{
private string type;
private string sound;
public Animal (string type, string sound)
{
this.type = type;
this.sound = sound;
}
public static void SomeMethod( string Type, string Sound )
{
Animal dog = new Animal(Type, Sound);
Console.WriteLine("new {0} makes the sound {1}",dog.type, dog.sound);
}
}
[/code]
This should do it.
Awesome thanks a lot!
[QUOTE=timgames;24917497]Is this java?
You use the constructor to do that, why make "SomeMethod" to do the same?
or do you want to print the current stuff?
[code] public class Animal{
private string type;
private string sound;
public Animal (string type, string sound){
this.type = type;
this.sound = sound;
}
public static void SomeMethod(){
Console.WriteLine("new {0} makes the sound {1}",this.type, this.sound);
}
}[/code][/QUOTE]
He was using Console.WriteLine(), so it was safer to assume C# than Java.
Also, your code idea wouldn't work because you're trying to call "this" from a static method.
[QUOTE=Darwin226;24917538][code]
public class Animal
{
private string type;
private string sound;
public Animal (string type, string sound)
{
this.type = type;
this.sound = sound;
}
public static void SomeMethod( string Type, string Sound )
{
Animal dog = new Animal(Type, Sound);
Console.WriteLine("new {0} makes the sound {1}",dog.type, dog.sound);
}
}
[/code][/QUOTE]
This works, but it doesn't really draw any benefit from the existence of the Animal class. The first line of SomeMethod() takes the two parameters and stores them into an Animal, only to read them back out on the very next line. There's no use having that Animal variable there at all; you could just use the parameters directly.
It'd be better, from an abstraction standpoint, to make SomeMethod() take a single Animal object parameter, instead of two strings. That way, code that calls it doesn't need to care about the data that makes up an Animal, and if you later add another field to Animal — say, babyType, that'd hold names like "calf" or "kitten" — you can make SomeMethod() print that value without having to add another parameter in all the places that call it.
At that point, a logical next step is to change it from being a static method that takes an Animal parameter, to an instance method that runs [i]on[/i] an Animal, as tingames suggested:
[code]public class Animal{
private string type;
private string sound;
public Animal (string type, string sound){
this.type = type;
this.sound = sound;
}
public void SomeMethod(){
Console.WriteLine("new {0} makes the sound {1}",this.type, this.sound);
}
}[/code]
(I took the liberty of removing the "static" keyword that he accidentally left in.)
In Darwin226's version, you'd use SomeMethod like this:
[code]
Animal.SomeMethod("cow", "moo");
[/code]
If you changed it to take an Animal parameter, you'd use it like this:
[code]
Animal cow = new Animal("cow", "moo");
Animal.SomeMethod(cow);
[/code]
And in tingames' version you'd do this:
[code]
Animal cow = new Animal("cow", "moo");
cow.SomeMethod();
[/code]
which is the most OO of the three.
Sorry, you need to Log In to post a reply to this thread.