I am writing windows forms application which needs to have global variable to change from one Form class and then to be used from another Form class
this is class for global variables
namespace Testi
{
public class publicVar
{
public static int kitxvisN = 0;
public static int sworipas = 0;
}
}
and this is class where I want to use it
namespace Testi
{
public partial class Form1 : Form
{
publicVar something = new publicVar();
something.kitxvisN++;
....
but it says invalid token ++ . . .
what is wrong can somebody help me?
You're trying to access static member as if it was not static. You don't need, and even can't use class instance to access static members. Use class name instead:
publicVar.kitxvisN++;
Because there is no instance variable, you access the members of a
static class by using the class name itself.
from Static Classes and Static Class Members (C# Programming Guide)
Update
Other thing is, you can't use code like that directly on class level. You need some method to put it inside it.
public void MyMethod()
{
publicVar.kitxvisN++;
}
static members can't be used with objects. They can be accessed by class name such as publicVar.kitxvisN ++;
Simply try with:
publicVar.kitxvisN++;
Because it's a static member and which does not require an instance to access that. static member should simply be accessed by class name itself.
You can't place those methods directly under class declaration. It has to be inside of a method. E.g.
namespace Testi
{
public partial class Form1 : Form
{
public static void Main(string[] args) {
publicVar.kitxvisN++;
}
Related
What I would like to do is to have a list that is in an object counts that is defined in the App class as a static when my application starts:
Here's the object:
public class Counts
{
public Counts()
{
public static List<CntQty> CardClicks2m;
}
}
In my application I declare this
am using the following code:
public partial class App : Application
{
public static Counts counts = new Counts();
public App()
{
}
}
Now I try to use load some data into the list but it gives me the error below. Note that this function is in another class.
public void GetClickHistory()
{
App.counts.CardClicks2m = db2.Query<CntQty>(sql);
The last line of the code is giving me an error saying
counts.CardClicks2m cannot be accessed with an instance reference;
qualify it with a type name instead.
I have tried a few different ways to make this work. One by removing the static and creating the object in the app constructor. This also didn't seem to work so I am hoping someone can point me in the right direction or at least suggest something.
i guess scope of variable is not right , you need to do like this
public class Counts
{
public static List<CntQty> CardClicks2m;
public Counts()
{
}
}
The error is pretty self explanatory, you cannot access a static property using an instance variable. all you need to do is use the typename. So this:
App.Counts.CardClicks2m
Becomes this:
Counts.CardClicks2m
You may need to specify the full namespace of the Counts class:
Some.Namespace.Counts.CardClicks2m
The error you are receiving is because you are trying to access to a static member from a instance object
public void GetClickHistory()
{
App.counts.CardClicks2m = db2.Query<CntQty>(sql);
EDITED after comments:
Try this (accessing the static member from the class):
public void GetClickHistory()
{
Counts.CardClicks2m = db2.Query<CntQty>(sql);
As CardClicks2m is declared static, it must be accessed from the class scope. If Counts class doen't have any other code, you can declare
public static class Counts
And there is not neccesary to create an instance of counts
public static Counts counts = new Counts(); //this is not neccesary
As your CardClicks2m-member is static there´s no need to have any instance of your Counts- or aven your App-class. The member exists once per appdomain however. Having said this in order to access CardClicks2m you don´t have to create an instance of your Counts-class within App.
Use this instead:
class MyClass
{
void DoSometjing()
{
Counts.CardClicks2m = db2.Query<CntQty>(sql).ToList();
}
}
Be aware to that Query will surely not return a List<CntQty>, but an IQueryable<CntQty>, that´s why you should call ToList afterwards.
Furthermore you can´t declare a member within a method or constructor. Thus declare it within the class´-body instead of the constructor:
public class Counts
{
public static List<CntQty> CardClicks2m;
public Counts() { /* any further initialzation */ } }
}
I've the following situation:
1) I have an internal static class where my software initialize a form
2) I would like to get the instance of this form to use for other reasons.
Example of code:
Class1:
namespace x {
internal static class Program {
private static Form mainx;
private static void Main() {
.....
.....
mainx=new Form(.....);
Application.run(mainx);
}
}
}
Class2:
I want to use one thing like this:
Form1 try=Program.mainx;
How can i do it?
If both assemblies are signed, you can use the InternalsVisibleToAttribute to expose internal members of an assembly to another.
I often use this to enable unit testing of internal classes without having to expose them as public.
You could mark the assembly with the internal class as a friend assembly on the other assembly, with the attribute InternalsVisibleTo. You will find more informations about this on the MSDN.
You need to add this line to your AssemblyInfo class (in the Properties folder), i.e. on the last line. This must be added in the project where you have declared the internal class.
[assembly:InternalsVisibleTo("NameOfOtherAssembly")]
If you with to retrieve the mainx property of your Program class, you need to make a visible (public or internal) getter on your class:
internal static class Program
{
private static Form mainx;
...
public static Form GetForm()
{
return mainx;
}
}
In your second class, you should then be able to get the form by calling GetForm():
Form1 try=Program.GetForm();
I am getting this problem. I created a class in a Windows Forms Application - WFA. It has one namespace as XmlParsing. it has two classes, both public, one partial. One class is named as myWindow; this is also public partial class. The other is MemberFunction class; this is public only. It has few strings and simple get n set methods. Now the issue is none of the variables and get n set methods are showing up in the myWindow class.
Please help. This is how I am doing stuff:
namespace XmlParsing
{
MemberFunction Class is here
myWindow Class is Here
}
Both are completely separate. I don't get where m running out of my limits.
Make sure your properties/variables are defined as public in your class. For example
class myWindow
{
public string MyProperty { get; set; }
public int Field1;
public static int StaticField;
}
Also if they are non-static members then you have to create an object of the class to access them.
myWindow objMyWindow = new myWindow();
objMyWindow.MyProperty = "Some string";
objMyWindow.Field1 = 10;
If you have defined a field as static you can access it against class name as well, like:
myWindow.StaticField = 100; //accessing static field
You may consider renaming your class and use Pascal case for class names.
I am trying to set/read a variable in class bluRemote from another namespace/class like so:
namespace BluMote
{
class bluRemote
{
public string cableOrSat = "CABLE";
........
}
}
and the other cs file (which is the form):
namespace BluMote
{
public partial class SettingsForm : Form
{
if (BluMote.bluRemote.cableOrSat == "CABLE")
{
BluMote.bluRemote.cableOrSat = "SAT";
}
.......
}
}
I know i am doing it wrong but I'm more used to doing stuff like this in VB so its like night and day ha :o)
What you are trying to do is work with static variables so you would need to change your class to this:
namespace BluMote
{
public static class bluRemote
{
public static string cableOrSat = "CABLE";
........
}
}
It is better if you stay away from static classes (for the most part) and instead focus on an object oriented approach where you have an instance (object) of bluRemote.
So instead of making the bluRemote class static you keep it the same and do:
public partial class SettingsForm : Form
{
private bluRemote _remote = new bluRemote(); // possibly created somewhere else
public void SomeFunction()
{
if (_remote.cableOrSat == "CABLE")
{
_remote.cableOrSat = "SAT";
}
}
.......
}
You're trying to access an instance variable - i.e. one which has a potentially different value for each object - just by class name. That only works for static variables.
You need to have an instance of bluRemote, and ask that for its value. However, I would strongly suggest that:
You rename your class to follow .NET naming conventions
You don't make variables public; use properties
Also note that there's only one namespace here - BluMote. Both of your classes are declared in that namespace.
As you've declared the cableOrSat field, you'll need to set it on an instance of the bluRemote class, but you are trying to set it using the name of the class itself.
If you declare the cableOrSat field as:
public static string cableOrSat = "CABLE";
You will be able to access it through the class name itself.
I have a number of classes in a Classes file, and I want them all to be able to access the same, global method to save duplicating code. Problem is, I can't seem to access a method from another class in my file - any ideas?
So my class1.cs layout is similar to this:
public class Job1
{
public Job1()
{
}
}
public class Methods
{
public static void Method1()
{
//Want to access method here from Job1
}
}
You'll need to specify the class they are in. Like this:
public Job1()
{
Methods.Method1()
}
If the class Job1 is in a different namespace from Methods then you'll need to either add a using clause, or specify the the namespace when calling the method. Name.Space.Methods.Method1()
Actually. Public Job1(){} is a constructor and not a method. It can be called from main class by creating object form the JOB1 class. Here add the following code:
public static void method1()
{
Job1 j1=new Job1();
}
constructor can be invoked by creating a object to the corressponding class....
To access methods of other classes, the methods must be static with a public Access modifier.
static - Not bound to an instance of the class but shared by all other instances.
private - data can only be accessed from inside the same class.
public - data can be accessed from other classes but must be referenced.