Data modification between classes C#

multiple classes c#
c# class
abstract class c#
base class c#
c# class structure best practices
c# inheritance constructor
c# person class
c# class constructor

I have a main class and an object which references the main class through constructor. The question is, how can I alter a property from the main class, within the object class?

public class First
{
   public int x = 0;
   public Second second;
   public First()
   {
      second = new Second(this);
   }    

   public run()
   {
      second.change();
   }


}
public class Second
{
   private First _first;
   public Second(First first)
   {
       _first = first;
   }

   public change()
   {
      first.x = 2;
   }
}

I don't want to use static classes because, First is in fact a DLL which runs inside Protrader, which in turn may run multiple First instances in parallel. Having static classes will leak data between these instances. So, I need to change a variable in the First class, by passing it as a parameter to the constructor of another class. (ref this) doesn't work. I also tried to create a wrapper class of first. public First Wrapper; and within First constructor instantiate Wrapper = this.. and then second = new Second(ref Wrapper). That compiled, but, didn't work either.

The purpose of this exercice is because I had a huge First class which compiles to a single DLL. Protrader will load a single DLL and runs it in paralell on different instruments at the same time (dolar, iene, euro , etc). If I use static methods the state control variables will "leak" to other instruments and that will ruin the logic.

These First class already have 2000 lines, and keeps growing. I decided to modularize it. Export some methods that it currently has to specializes classes such as log, events, trading commands, etc. But, these classes alter properties within each other and within the main class.

Can't you just make a function inside the First class that can modify the variable?

Like so:

public class First
{
   public int x = 0;
   public Second second;
   public First()
   {
      second = new Second(this);
   }    

   public run()
   {
      second.change();
   }

   public modifyX(int changedX)
   {
       x = changedX
   }

}
public class Second
{
   private First _first;
   public Second(First first)
   {
       _first = first;
   }

   public change()
   {
      _first.modifyX(2);
   }
}

Classes and Structs, https://docs.microsoft.com › › Programming guide › Classes and structs In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created. For more information, see Classes, Objects, and Structure types.

The property x in class First could also be written as

private int _x;
public int x
{
    get { return _x; }
    set { _x = value; }
}

Made some minor changes to the code you provided, hope it helps

public class First
{
    public int x { get; set; }

    public Second second;
    public First()
    {
        x = 0;
        second = new Second(this);
    }    

    public void run()
    {
        second.change();
    }


}
public class Second
{
    private First _first;
    public Second(First first)
    {
        _first = first;
    }

    public void change()
    {
        _first.x = 2;
    }
}

Inheritance, Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. When you define a class to derive from another class, the derived class implicitly gains all the members of  The singleton will work even if the data changes. The idea is that you don't instantiate multiple instances of your "global" class, that they all have to share the single instance. Basically, form1.text = GlobalClass.Instance.Varia bleOrMetho d() makes sure that any variables, even if changed, are all in the one class and not

Looks like the code was right from the start. I was not able to run my DLL because of another problem. Thank you all for the help provided!

Data Transformations with LINQ (C#), You can modify the sequence itself without modifying the elements For example, you can transform data from SQL rows or text files into XML. class Student { public string First { get; set; } public string Last {get; set;} public  C# Class. In c#, Class is a data structure and it will combine various types of data members such as fields, properties, member functions, and events into a single unit. Declaring a Class in C#. In c#, classes are declared by using class keyword. Following is the declaration of class in c# programming language.

Classes and Objects in C#, A class is a data structure that combines state (fields) and actions (methods Access is limited to this class or classes derived from this class. Modifications to a value parameter don't affect the argument that was passed for  DTO (Data Transfer objects) is a data container for moving data between layers. They are also termed as transfer objects. DTO is only used to pass data and does not contain any business logic. They only have simple setters and getters. For example, below is an Entity class or a business class. You can see that it has business logic in the setters.

Data bind custom objects, Entity Framework auto-generates all the boilerplate change-tracking code, which Loading data into objects (typically from a database). to loop through the rows in a data table, and populate an object with data: C# Copy. C# is an object-oriented programming (OOP) language and does not support global variables directly. The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances.

Fields, A class or struct may have instance fields, static fields, or both. field F, you can create two objects of type T, and modify the value of F in By contrast, a static field belongs to the class itself, and is shared among all instances of that class. Data that your class exposes to client code should be provided  Classes are declared by using the class keyword followed by a unique identifier, as shown in the following example: C#. // [access modifier] - [class] - [identifier] public class Customer { // Fields, properties, methods and events go here } The class keyword is preceded by the access level.

Comments
  • Can you show us how you're calling this in main? I'm not clear as to what you're trying to achieve.
  • Are you allowed to use a function to modify the property?
  • Also, don't have public fields. Properties should be used instead.
  • Are you attempting to reflect the changes across all instances of First no matter where the reference is? So if I have First f1 and First f2 and make my change in Second, then f1 and f2 both reflect that change?
  • What matters is, inside First, at any point, I will call run().. and then, run will call second.change() which in turn should alter the property x in First.
  • This will only impact the value of x in Second's instance of First; in which case you just make x a property.
  • The purpose of a Second class is because I had a huge class, and then I exported some of its methods to a Second Class , but, these exported methods also alters properties from First Class. Hence.. I need data persistence between classes without using static methods, and also, I want to be able to change properties from First inside any other classes..
  • This isn't a globally reflected change though; you'll only be impacting Second's reference to First.