Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-06
4
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
purpleblob earned 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

670 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