SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Working with dynamic Control Using Generic

    Suppose I want to write a method that can accept any Control like GridView, DetailsView etc as parameter and will make their visibility False, make them null etc.

    So I write a method :
    private void MakeObjectNull<T>(ref T obj)
    {

    }
    And try to call it in the following way :
    MakeObjectNull<DetailsView>(ref DetailsView1);
    Can anyone tell me what I should write in the method "MakeObjectNull" to do the following operation like :
    obj.Visible = false; obj.Dispose(); obj=null; etc

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need a function for that. Just call the methods of the object.

  3. #3
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Csharp:
    private void MakeObjectNull<TControl>(TControl obj)
       where TControl : Control
     {
          // Because we are expecting something that inherits from teh WebControl class,
    // we are able to access all Control properties
           obj.Visible = false;
     
       var disposable = obj as IDisposable;
     
       if ( disposable != null )
          disposable.Dispose();
     }

  4. #4
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If we dispose the object why setting visible to false?
    I'm assuming the control was removed from the tree.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    It works fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •