-
April 13th, 2014, 12:47 PM
#1
New to C#! Arguments to Private Variables
Hello, right now i'm learning C# and I need some one to clarify what my teachers trying to say. We have to make a simple score sheet which I did but not the way he wanted, we're suppose to have Player One with x points which I did but this is the part I don't get.
3 setName
argument: name
argument type: string
return type: nothing
Method sets the value of the private variable Name.
You are suppose to assign the name arg to the private variable]
If private string Name; is already defined and in the setName(){} wouldn't Name = "Player One" suffice? From my understanding Name is already set as a string before adding it in the function, so the argument would be: Name = "Player One", because there's no return for setName(){} it's a void. Nothing was defined if the function was suppose to be public or private so I made it public. And "Player One" would be the value for Name.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ScoreSheet
{
class Player
{
private string Name;
private float Score;
public void setName()
{
Name = "Player One";
}
public string getName()
{
return Name;
}
public float addPoints(float points)
{
Score = points;
return points;
}
public float getPoints()
{
return Score;
}
}
class Program
{
static void Main(string[] args)
{
Player play = new Player();
play.setName();
play.getName();
Console.WriteLine("Player One, your points are: " + play.addPoints(10));
play.getPoints();
}
}
}
-
April 13th, 2014, 02:20 PM
#2
Re: New to C#! Arguments to Private Variables
You are suppose to pass in a method argument to the setName() method.
Code:
public void SetName(string name)
{
Name = name;
}
Hopefully, after completing this exercise, the instructor will point out that in C#, there is no need to create Setter and Getter methods (i.e. SetName/GetName) because in C#, there are something called [auto] properties which automatically create setters and getters for you under the covers.
The corresponding auto property with a public setter and getter is:
Code:
public string Name { get; set; }
As an aside, it would be nice if schools taught the standard coding convention for the language they are teaching in. In other words, in C#, method names are PascalCase, not camelCased (unlike in Java, where method names typically are pascalCased). So setName(...) should be declared as SetName(...).
-
April 13th, 2014, 02:47 PM
#3
Re: New to C#! Arguments to Private Variables
Originally Posted by Arjay
You are suppose to pass in a method argument to the setName() method.
Code:
public void SetName(string name)
{
Name = name;
}
Hopefully, after completing this exercise, the instructor will point out that in C#, there is no need to create Setter and Getter methods (i.e. SetName/GetName) because in C#, there are something called [auto] properties which automatically create setters and getters for you under the covers.
The corresponding auto property with a public setter and getter is:
Code:
public string Name { get; set; }
As an aside, it would be nice if schools taught the standard coding convention for the language they are teaching in. In other words, in C#, method names are PascalCase, not camelCased (unlike in Java, where method names typically are pascalCased). So setName(...) should be declared as SetName(...).
Thanks for the clear cut defination, I was thinking backwards it seems. The ironic part about this course is that we're learning C# before we get into unreal script. As for why he does setter/getters he mainly works in C++ so I guess he does it as close as possible to C++.
Last edited by aleeterninja; April 13th, 2014 at 02:58 PM.
-
April 13th, 2014, 03:38 PM
#4
Re: New to C#! Arguments to Private Variables
Originally Posted by aleeterninja
As for why he does setter/getters he mainly works in C++ so I guess he does it as close as possible to C++.
That's good if the course ends you up in C++; otherwise, you kind of miss out on learning C#'s features.
At any rate, glad I could help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|