English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

hey ppl plz help me out on classes in c#

i have implemented a class
how do i create a instance of a class which is accessible throughout the project.
i can create a instance under the click event of a button.but then this instance is not accessible from another button.
plz teleme me how how to declare an instance of a class that i visible throught.

plz plz write an example code also plz.
thx

2007-12-10 19:27:21 · 2 answers · asked by shrinivas 1 in Computers & Internet Programming & Design

2 answers

See linked article, it explains variable scope in VB.NET, but same rules apply to C#

http://www.dummies.com/WileyCDA/DummiesArticle/id-1936.html

2007-12-10 20:59:07 · answer #1 · answered by Anonymous · 0 0

I'll assume that you already have a Windows project started in C#. Open it up. I'll show you how to add a second project to the solution and execute code in it.

You will want to open the second project by right clicking on the solution within Solution Explorer and choosing Add, New Project. Choose Class Library as your template. Replace the code that is automatically generated for you with the following code.
--------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace CollisionLibrary
{
public class Point
{
private double _x;
private double _y;
public double X
{
get { return _x; }
set { _x = value; }
}
public double Y
{
get { return _y; }
set { _y = value; }
}
public Point()
{
X = 0;
Y = 0;
}
public Point(double x, double y)
{
X = x;
Y = y;
}
public double DistanceFromOrigin()
{
return System.Math.Sqrt(X*X + Y*Y);
}
}
}
---------------
Just to make sure everything is OK, you can compile it. Correct any errors before you proceed.

Now go back to the original project in the solution explorer and look for the +References entry. Right click on this line and choose Add Reference. Choose the Projects tab. Select the original project and choose OK. You will now see the new project listed under the original project reference list in solution explorer. In other words, the original project can now reference code in the new project.

If you open the form design view containing the button and double-click on the button, this will open to the button click event in the code. You can now add code to create an instance of the Point class in this function.

CollisionLibrary.Point point = new CollissionLibrary.Point();
point.X = 10.1;
point.Y = 20.5;
distance = point.DistanceFromOrigin();

You can add other public functions to this class that does something else as you wish. Make sure that this is public, or you will not be able to access the function.

I hope this answers your question. Notice how an instance of a point object is created by using the new operator. You execute public functions against an instance.

I understand that you want to have a single object that is controlled like one. Be aware, that you will have two instances of the object if you instantiate this twice within the button code of different buttons. You can instantiate the object within the form load event and then call methods from the code within each button. You can also simply add code to the form as public functions. Each button can call functions on this object. That is probably your best option.

Another option is to instantiate the object within the button code and make sure that all state information (data) is maintained within the Class Library (.DLL) as static data. Do not save or maintain data within the button code itself.

You will have an instance of the class, but you can use static variables, XML, or a database to store and share any common data. I would think that you can save the data to text boxes (hidden or visible) and make sure that your functions in the library rely on this data rather than create local data variables.

2007-12-11 04:03:50 · answer #2 · answered by Skeptic 7 · 0 0

fedest.com, questions and answers