I'm learning about objects and classes and following along with code on another site. It creates a new class Student
that takes in firstName
, lastName
, score
, and multiple variables for the birthday.
class Student {
[string]$firstName = ""
[string]$lastName = ""
[int]$score = 3
[System.DateTime]$birthday
Student ([string]$firstName, [string]$lastName, [int]$score, [int]$day, [int]$month, [int]$year) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = (Get-Date -minute 0 -second 0 -millisecond 0 -hour 0 -year $year -Day)
}
Student ([string]$firstName, [string]$lastName, [int]$score, [int]$day, [int]$month) {
$this.firstName = $firstName
$this.lastName = $firstName
$this.score = $score
$this.birthday = (Get-Date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day $day -Month $month)
}
[string] GetFullName() {
return ("{0} {1}" -f $this.firstName, $this.lastName)
}
[string] GetReportLine() {
return ("{0} {1}" -f $this.firstName, $this.lastName)
}
}
As I said, I'm learning, but I thought the GetReportLine()
would display the first name and last name. However, when I create a new Student object
[Student]::new
$some = [Student]::new("Some", "Guy", 3, 2, 2)
and then tried calling it, writing all the code exactly as shown online, I get this as the output
$some.GetReportLine()
Some Some
when I was expecting this as the output
Some Guy
So, I've concluded either there's an error in the code or I'm just not understanding how the code works correctly.
Could you all check and let me know which it is? I'm just trying to wrap my head around it.
CodePudding user response:
If only IDEs could catch this kind of mistake, which happens to me wayyyyy too often. Your code works as written...lastname = $firstName.
class Student
{
Student ([string]$firstName, [string]$lastName, [int]$score, [int]$day, [int]$month) {
$this.firstName = $firstName
# ↓↓↓↓ ☢