Reputation: 93
I'm beginner when it comes to OOP. Yesterday I was trying to read some mvvm/wpf examples and of course I get into trouble... I have some problem with understand some of code below:
{
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Addres { get; set; }
}
This is just normal Person class, nothing really unusal here. The problem is that I can't understand below code:
private void SayHi_Click(object sender, RoutedEventArgs e)
{
Person person = new Person
{
FirstName=FirstName.Text,
LastName=LastName.Text,
Addres=Address.Text
};
The part that I do not understand is:
Person person = new Person
{
FirstName=FirstName.Text,
LastName=LastName.Text,
Addres=Address.Text
};
I'm not sure what this is exactly. I thought that every new object should be initialized like this one: Class class = new Class();. Why there is no () after "new Person"? Instead of () we have {}. I know that we can use default, parameterized, static and private construcotrs. Could someone explain this to me? Similar situation in below tutorial from CodeProject:
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
We have a Song Class
public class Song
{
#region Members
string _artistName;
string _songTitle;
#endregion
#region Properties
/// The artist name.
public string ArtistName
{
get { return _artistName; }
set { _artistName = value; }
}
/// The song title.
public string SongTitle
{
get { return _songTitle; }
set { _songTitle = value; }
}
#endregion
}
And we have of course View Model for this class:
public class SongViewModel
{
Song _song;
public Song Song
{
get
{
return _song;
}
set
{
_song = value;
}
}
public string ArtistName
{
get { return Song.ArtistName; }
set { Song.ArtistName = value; }
}
}
And again, this part of code is something that I can't understand:
public class SongViewModel
{
Song _song;
public Song Song
What this "Song _song;" is? This is object of Song Class? And this Property "Song Song" is also wierd... Probably I have a lack of knowledge
Upvotes: 2
Views: 94
Reputation: 102793
I had to leave a new answer here, because I have seen 2 people get it wrong. The initializer syntax:
Person person = new Person
{
FirstName = FirstName.Text,
LastName = LastName.Text,
Address = Address.Text
};
Is not exactly equivalent to creating a new variable "person" and then assigning the properties. Rather, it is equivalent to creating a temp variable, assigning the properties, and then assigning the result to "person":
Person person;
var temp = new Person();
temp.FirstName = FirstName.Text;
temp.LastName = LastName.Text;
temp.Address = Address.Text;
person = temp;
The distinction can actually be quite important, especially working in a view model. You should be able to see this, if you imagine that you are assigning to a property whose setter raises the "PropertyChanged" event, which in turn has one or more view elements listening to it. Using the temp variable results in a big performance gain, because every event listener gets fired only once, rather than twice (once when the Person
property is set, and then again when each of its properties are initialized).
Upvotes: 6
Reputation: 6296
This:
Person person = new Person
{
FirstName=FirstName.Text,
LastName=LastName.Text,
Addres=Address.Text
};
And:
Person person = new Person();
person.FirstName=FirstName.Text;
person.LastName=LastName.Text;
person.Addres=Address.Text;
Are equivalent. The first is syntactic sugar.
Upvotes: 0
Reputation: 61369
The provided code is using an Object Initializer (MSDN)
From that page:
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements. The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax).
Basically, it means you don't need the ()
, and can specify public field/property values in the {}
.
Also; find a different site to learn MVVM. Directly creating Model/ViewModel objects from the UI is a big no-no.
Upvotes: 3