0

I would like to pass values from one class file to another class.

E.g:

Step1:

Class1.cs

public class Class1
    {
        public string LogedInPerson { get; set; }

        public string Name
        {
            get { return this.LogedInPerson; }
            set { this.LogedInPerson = value; }
        }
    }

Step2:

Value has been assigned in below method:

test.xaml.cs

public void assignValue()
{
     Class1 obj = new Class1();
     obj.LogedInPerson = "test123";
}

Step3:

I would like to get "test123" values from Class2.cs.

E.g:

public void test()
        {
            string selected_dept = ?? //How to get "test123" from here.
        }
0

3 Answers 3

3

You can have variables class that includes public variables. Define instance of class1 in variables class .

public static class1 myclass=new class1();

in test.xml.cs set value

public void assignValue()
{
 myclass.LogedInPerson = "test123";
}

in class2.cs

public void test()
    {
        string selected_dept = myclass.LogedInPerson;
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Initialize Class1 outside assignValue() methos

Class1 obj = new Class1();

public void assignValue()
{

     obj.LogedInPerson = "test123";
}
public string returnValue()
{

  return obj.LogedInPerson;
}

if your second class name test.xaml then call it like this, but I don't think you can use class name test.xaml so use a nice name instead there eg: Class2

public void test()
{
    test.xaml test = new test.xaml();
    test.assignValue();
    string selected_dept = test.returnValue(); //How to get "test123" from here.
}

Comments

0

I believe this question is more on the topic of basic Object Oriented Programming principles, not so much about WPF specific features. Therefore, I will provide you a non-WPF specific answer, as it will allow me to address your question in the most direct way.

In OOP, a method can return a result to the caller. So, for instance,

public string GetReturnObject(){

     return "This is a return object";

}

You can create a new object and pass it back to the caller,

public void Test(){

     string data = GetReturnObject(); 

}

And now data will be assigned the object that was returned from the method that Test() called. So, if you modify your AssignValue method by adding a return type and passing the instantiated Class1 object back to the caller, you will have the answer you need

public Class1 assignValue()
{
     Class1 obj = new Class1();
     obj.LogedInPerson = "test123";
     return obj;
}

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.