Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im very sorry for this idiot question. I am like a baby in C# programming. Can you please help me how can I bring out my output via calling my method in Person Class using label.content object but it looks not possible. Please see my code below.

What I have tried:

namespace myClassPractice
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button_Click(object sender, RoutedEventArgs e)
{
Person employee = new Person();
employee.LastName = "Newmann";
employee.FirstName = "James";
employee.Sleeping();
}
}
}
class Person
{
public string FirstName;
public string LastName;

public void Sleeping()
{
label.Content=FirstName + "is sleeping!"); //it looks I cannot use label.content object here.
}

}
Posted
Updated 9-Jul-16 20:37pm

You can't use label.Content there because it doesn't exist!
label isn't a part of the Person class, so you can't access it. (It isn't obviously part of any class in your program, as you don't show it being declared anywhere - but I'm guessing you added it to your MainWindow class.)

Think about it: your Window shows Controls - like your Label - and may contain several instances of a Person: "James Newmann", "Lorenzo Cielo Maglaya Jr.", "Original Griff" none of which are aware of the existence of your Window. But...you may have a second Window which you want to use to display all the detail for a particular person once you have selected them on your MainWindow. And if they don't know which Window they are supposed to show on, how could they "decide" which Label to display on?

What you actually want to do is return a string from your Person class which indicates if that individual is asleep:
C#
public bool IsAsleep { get; set; }
public string Sleeping()
   {
   if (IsAsleep)
      {
      return FirstName + " is sleeping!";
      }
   return FirstName + " is awake.");
   }

If your main window you can then use it:
C#
private void button_Click(object sender, RoutedEventArgs e)
    {
    Person employee = new Person();
    employee.LastName = "Newmann";
    employee.FirstName = "James";
    employee.IsAsleep = true;
    label.Content = employee.Sleeping();
    }


BTW: in future, tag your questions a bit more carefully - this is WPF specific and should have a WPF tag.
 
Share this answer
 
Comments
ELMAGLAYA 10-Jul-16 3:16am    
Thanks for your detailed explanation. I really appreciate it.
OriginalGriff 10-Jul-16 3:26am    
You're welcome!
Suvendu Shekhar Giri 10-Jul-16 3:33am    
Just the perfect. #salute
5ed !
That's the wrong place to set property of a label, I believe although I am not a WPF developer. Your Person class should be independent of the UI and codebehind partial class, that's how we make business and presentation layer decoupled. It has it's own advantages and you can search web for more details.

There ways you can remove the errors by doing a very change to your existing code but that's no advisable. So, check following approach which I believe suggests a better approach -
Method:
C#
public string Sleeping()
{
   return FirstName + "is sleeping!";
}

and while calling you can do something like-
C#
label.Content=employee.Sleeping();


Hope, it helps :)
 
Share this answer
 
Comments
ELMAGLAYA 10-Jul-16 3:13am    
It makes sense to me now. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900