15,898,858 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by AtaChris (Top 4 by date)
AtaChris
2 days ago
View
Assume MyClass looks like as follows:
public MyClass
{
public MyClass()
{
// Parameterless constructor. Can be used by the class to initialize
// any value data required by an instance of this class but has no
// dependency on any outside data.
}
public MyClass(AnotherClass instance)
{
// Should take the instance of another class to use its memberfunctions and properties
}
...
}
And I would like to instantiate it as follows:
AnotherClass
{
MyClass _myClass {get; set} = new(this);
}
In such a case the compiler would say:
The this keyword was found outside of a property, method, or constructor or
A field initializer cannot reference the non-static field, method, or property 'name'.
AtaChris
8-Apr-24 12:21pm
View
Richards solution is what i was looking for.
Thank to you Richard an everybody else for the help !!!
AtaChris
8-Apr-24 3:30am
View
Thank you for this approach which would definitifely solve the Problem.
Maybe you can have a look to my comment @ Solution 2.
I would like to avoid to do value comparisons etc. in my Master class.
I need this feature for logging purposes.
My goal is to have a looger that shows namespace + class + property + old and new value of property when events in classes are fired.
The logger invocation should be located in the event handler of my Master class.
And I would like to hand over the info I got from sub classes directly to the logger.
AtaChris
8-Apr-24 3:14am
View
Thank you, this seems to be a solution that answers the question.
The only problem I see is the following:
My class Person and other classes not listed here carry many properties of different types.
This means I would have provide for every class a seperate event handler in Master class and I would have to ckeck each property of the classes in these event handlers. Another problem is, that I would have to compare old and new value in these event handlers to find out if they have changed.
I am looking for a solution where PropertyChangedEventHandler? sends the class and property name as well as its old and new value to a more generic PropertyChangedHandler in Master Class which is responsible for all Sub classes, so that I can retrieve the information I want from sender and/or e wihtout any forther validations in Master classes PropertyChangedHandler ?
In other words: is there a way to "inject" oldvalue and new value into
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
So that all info I need is already prepared and send by Person class.