Solved

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

Posted on 2003-11-06
4
195 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now