Solved

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

Posted on 2003-11-06
4
214 Views
Last Modified: 2010-04-16
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
Comment
Question by:nvms
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
purpleblob earned 50 total points
ID: 9700414
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
 
LVL 6

Expert Comment

by:purpleblob
ID: 9700527
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
 

Author Comment

by:nvms
ID: 9709830
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
 
LVL 6

Expert Comment

by:purpleblob
ID: 9709956
Glad to have been of assistance - good luck with your C# experience :-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question