You must Sign In to post a response.
  • Category: .NET

    Pass object as parameter?

    Hi Friends,

    How to pass object as parameter in the mothods?Please give a simple example
  • #692790
    see the code below

    private void button2_Click(object sender, EventArgs e)
    ArrayList arrList = new ArrayList();


    private void DoWork(object objInput )
    //you should know the actual type
    // contained in the object to
    //manipulate the data

    ArrayList arrList = (ArrayList)objInput;
    string strName = (string)arrList[0];


  • #692791
    Hai Arjunan.S,
    Passing object as a parameter is the common scenarios where you use the layers/tiers like in 3-tier, n-tier applications.
    In those type of application, we generally use the objects to pass from one layer to another and it has lot of advantages.
    Let see an example:
    Create a Business layer and data layer and then pass the Business objects to both the layers from the UI. This is the common example of 3-tier or n-tier applications.
    So first we need to create the Business Objects. i will take a class and few properties. You can go through the below link for the details of how we are passing the business object to DataAccess and the same from UI to Business Layer.

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #692811
    You need to set type as the class of which type you want to send the object for example a custom classs you have as Student then need to pass the student object as

    Student objStd = New Student();
    objStd .Name = "Anil Pandey";

    private void DisplayName(Student obj )

    Label1.Text = obj.Name.ToString();

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #692815
    It is simple like normal variable passing. Just we use customize class for object.
    check following example

    //Create object of class
    class1 objClass = New class1();
    objClass.Prop1 = "This is test property";
    ReturnName (objClass);

    private void DisplayName(class1 objecttoFunction)

    hope it helps

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #692826

    Passing the object as parameter is same as passing any other data type. Instead of writing the datatype name just write the object name in the function.

    G. S.

  • #692970

    First of all i want to explain you the way we pass parameter of value type and reference type work differently,

    When we pass value type as a parameter it will pass the value of that parameter whereas in case of reference type i.e. object it will pass the reference i.e the address of the object.

    Example :-

    List<string> listofNames = new List<string>(){"Ramesh","Mahesh","Suresh"};


    private void DisplayNames(List<string> values)
    foreach(var item in values)

    Output : RameshMaheshSuresh

    Thanks & Regards


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.