0

I have three controller classes in c#. Say AController, BController, CController. I used to set the value of private member of BController using AController.

public BController{
string test = "";
public BController(string input)
{
this.test = input;
}
}

How can I access the test from CController.

Any approach rather then assigning the member as static variable

Anees Deen
  • 1,385
  • 2
  • 17
  • 31
  • Perhaps, you have mistaken the logic? Could you provide more code and some more info about your needs? – noobed Oct 29 '14 at 14:21
  • possible duplicate of [Reflect Over List of Controllers](http://stackoverflow.com/questions/3680609/reflect-over-list-of-controllers) – Alexander Oct 29 '14 at 14:21

3 Answers3

1

You can return a redirect to CControllerAction.

public class BController()
{
    public ActionResult BControllerAction()
    {
         return RedirectToAction("CControllerAction", "CController", new { param = "some string" })
    }
}

public class CController()
{
    public ActionResult CControllerAction(string param)
    {
         if(!String.IsNullOrEmpty(param))
         {
            //do smth
         }
    }
}
Artyom Pranovich
  • 6,814
  • 8
  • 41
  • 60
0

You can get and set the value of a private field using the Properties pattern

public string TestB {
    get {return this.test;}
    set {this.test = value;}   
}
Tinwor
  • 7,765
  • 6
  • 35
  • 56
0

Your question does not specify the technology you are using. Hence my first suggestion is based on plain C# class.

The test property is a private field and cannot be accessed outside the class. You can expose it via a property. You can make the field as internal if you are going to access it with the current assembly. Have a look at Access Modifier for more informaiton. I would suggest you to use a property as it give you more control.

private string test;
public string Test
{
 get{return test;}
 set{test=value'}
}

If you are using MVC then you can make use of TempData. TempData is a dictionary derived from TempDataDictionary class. It is used to store data which is only needed for the following request. For more information, refer TempData

Amit Rai Sharma
  • 4,015
  • 1
  • 28
  • 35