Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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