Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

'Correctest' may to expose persistent Class methods and properties to Forms

I am new to C# and am porting an eMbedded VB project over to the VS.NET 2003. I am finding C# a far 'neater' language which strongly discourages 'spagetti' where VB is sometimes too inviting.

My query is this, I have established how to create a Class to provide static variables to my Windows application. The simple example is as follows:

1. Create a new Windows project and add a Class item (Class1)
2. Add a button (Button1) and a label (Label1) to the form
3. Add this following to the button1_Click event:
label1.Text=new Class1().Resultant().ToString();
4. Replace all of Class1 with the following:
using System;
namespace WindowsApplication1
{
      public class Class1
      {
            private static int Counter =0;
            public int Resultant()
            {
                  return Counter++;
            }
      }
}

I then tried something along these lines:
label1.Text=myClass.Resultant().ToString();
Where myClass is declared in Form1. This is where I am a little perplexed.

a) Will the process of creating  new class everytime I wish to return a property or call a method result in overheads if I called the (for example) had a loop that returned a value at processor speed. I guess I would simply declare the temporary instance and have my iterative loop afterwards.

b) Is there a way that I can declare myClass somewhere in Form1 and have it available through all of Form1

Thanks in advance from an enthusiastic newbie.


0
nvms
Asked:
nvms
  • 3
1 Solution
 
purpleblobCommented:
Well first off, yes like declaring a variable within the scope of a module, you could declare a variable

private Class1 myClass = new myClass();

in your class (for example the line your Form's class declaration - you'll see your button and other UI components declared there) and just like you've done with the static in Class1 (but without the static keyword :-) ).

In answer to your first question - undoubtedly creating a new object everytime will come with overheads and affect effeciency. In what you're doing here, the effect is so small you wouldn't notice it - but remember when you've finished using the Class1 instance the GC (garbage collector has to, at some point tidy up after you).

Hope this helps

0
 
purpleblobCommented:
Oh, also forgot to say in the previous respone (I know your example is probably just for testing ideas out) but you could have had the Resultant method as public static as you're not really needing a specific instance of an object in this case.

Therefore you could use the syntax

Class1.Resultant();

i.e. now need to create an instance of Class1

0
 
nvmsAuthor Commented:
Thanks for your comments, I have done lots of homework and practice and C# is definately very cool! I have a utilties class with lots of core values I can expose and use anywhere
Best wishes

Stewart
0
 
purpleblobCommented:
Glad to have been of assistance - good luck with your C# experience :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now