-3

i have a windows form and a class named testclass.cs . the below sample code is for testclass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testApp
{
     public class testclass
    {
       public void disableEverything()
       {
           //some operations goes here
       }
       public void welcome()
       {
         //some code goes here
       }
    }
 } 

and inside my form i have code for create object inside a button click event the below code shows the form1

       using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Text;

       namespace testApp
       {
          public partial class Form1: Form
          {
            private void button1_Click(object sender, EventArgs e)
            {
               testclass obj = new testclass();
               obj.welcome();

            }
          }
        }

my question is if i click button1 five times it will create 5 objects of the class testclass. suppose if i want to call disableEverything method of some purticular class objects like 3rd object, second object , how can i call this? should i use a List<testclass> and call list[index].disableEverything() . suggest me a good soultion for this

Lazy Programer
  • 171
  • 1
  • 1
  • 12
  • 6
    Living up to your name I see. – Equalsk Dec 28 '17 at 11:33
  • 2
    Destroy class being inside the class? It's non-sense even if it'd be allowed. What is your question about? Be more specific. – JohnyL Dec 28 '17 at 11:33
  • If you don't want to create multiple instances of this class you should place this object in Global and instantiate it when the form loads up. – Zorkind Dec 28 '17 at 11:34
  • 1
    Also you should learn about IDisposable, instead of making your own version of it. – Zorkind Dec 28 '17 at 11:35
  • @JohnyL , its like disable something(example a bool value set to false) of that particular class using their object. – Lazy Programer Dec 28 '17 at 11:37
  • You need those instances to be in scope when you want to set a property or call a method on them. There is no one right way, especially not knowing what your system is intended to do. – Crowcoder Dec 28 '17 at 11:39
  • Guys im not talking about destroy the class object. i want to disable some features of that purticular class, and if i want to destroy that object i have to call `obj.Destroy();` by inheriting IDisposable interface? – Lazy Programer Dec 28 '17 at 11:40
  • You should implement `IDisposable` interface. Do you have some unmanaged resources references? – JohnyL Dec 28 '17 at 11:41
  • @JohnyL Yes! Its there – Lazy Programer Dec 28 '17 at 11:43
  • Still don't get you. When you press button 5 times, 5 objects come to life. In your example, they exist until the end of event handler. Further, they are reclaimed by Garbage Collector. What are you about to disable - it's not clear... – JohnyL Dec 28 '17 at 11:56
  • @JohnyL, ok ! what to do if i want to keep object lifetime until form close ? – Lazy Programer Dec 28 '17 at 12:01
  • `what to do if i want to keep object lifetime until form close ?` Store it in a field (maybe a `List`) of the form? – mjwills Dec 28 '17 at 12:40

1 Answers1

1

I dunno what you are implement, but for unmanaged resources, you can implement IDisposable, and use "Using" for release the resources after work:

namespace testApp
{
  public class testclass : IDisposable
  {
      bool disposed = false;
     //Instantiate a SafeHandle instance.
      SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
    // Public implementation of Dispose pattern callable by consumers.
    public void Dispose()
   { 
     Dispose(true);
     GC.SuppressFinalize(this);           
   }

   // Protected implementation of Dispose pattern.
   protected virtual void Dispose(bool disposing)
   {
     if (disposed)
      return; 
     if (disposing) {
      handle.Dispose();
      // Free any other managed objects here.
      //
    }

    // Free any unmanaged objects here.
     //
    disposed = true;
   }  


    public void welcome()
    {
         //some code goes here
     }
   }
 }

private void button1_Click(object sender, EventArgs e)
{
       using (var obj = new testclass())
       {

       }
 }

https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx

Rui Estreito
  • 262
  • 1
  • 10