Access variable from other namespaces - c#

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.

Related

How to use strings from class for language strings?

I want to realise an multilang in the app. I tried to create an class 'lang1.cs' in the project, then I created class, but I don't understand, how to use variables inside Form1 like MFAT.LngEnglish.About. My class for language stings:
namespace MFAT
{
class LngEnglish
{
string About = "About";
}
You have to create (e.g. within the constructor) an instance of the class then you can access the variable
Like this:
LngEnglish lngEnglish = new LngEnglish();
string about = lngEnglish.About;
You might want to use a static class so that you do not need to instantiate a new object every time you need to access the strings.
For example:
namespace MFAT
{
public static class LngEnglish
{
public static const string About = "About";
}
}
Then you can use it like:
var aboutText = LngEnglish.About;

How can I access a list that is created in a static object that is part of another class?

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 */ } }
}

Using parameter in constructors C#

I'm abit new in C#. I have some code like this:
namespace Example
{
public partial class Example_Setting : Form
{
public Example_Setting(String somethings)
{
}
private myPlace()
{
MessageBox.Show(somethings);
}
}
I don't know how to get value of somethings variable in myPlace().How can I do?
An example would be:
namespace Example
{
public partial class Example_Setting : Form
{
string somethings; // <-- declare a variable in the class
public Example_Setting(String somethings)
{
this.somethings = somethings; // save param to variable
}
private myPlace()
{
MessageBox.Show(somethings); // now data is here for use
}
}
I think you can use below code.Declaring the other variable and assign it into constructore and then you can use it in whole class.
public partial class Example_Setting : Form
{
public string some;
public Example_Setting(String somethings)
{
this.some = something;
}
private myPlace()
{
MessageBox.Show(this.some);
}
}
Others have already demonstrated an example. I just want to point out why you can't access somethings from myPlace.
In the example provided in your question, somethings is scoped locally to the constructor. That is, once the constructor has completed, somethings is no longer available to reference and use. In the examples others have provided, they scope somethings to the class, and then assign a the value provided in the constructor parameter. Since somethings is scoped to the class, your other methods (and properties) can access it. Note that if you use public, others outside of the class can use it as well. Best practice though is to keep it private and if you need public access, to use a property.

In case i have class with only one static variable - should i declare this class as static?

This is my class how hold my last visited Path:
public class LastPath
{
private static string _lastPath;
public static string lastPath
{
get { return _lastPath; }
set { _lastPath = value; }
}
}
If all members of a class is static and your class is not meant to instantiated it should be static.
In this case your class satisfies the above rule or guideline so marking it as static will make sense because you don't have any instance member.
LastPath path = new LastPath();
path.????// Nothing to access, so prevent instantiation by marking class static.
Being said that If you have only one field in your class and no methods I'd argue that you probably don't need a class at all, just refactor it to some other class where it would make sense.
make the class static and make a public property as static and you are done, like this:
public static class LastPath
{
public static string lastPath { get;set;}
}
I would say you to do this:
public static class LastPath
{
public static string lastPath
{
get;set;
}
}
You should declare it as static as static classes cannot be instantiated, while non static classes can be instantiated, which is not needed.
First - it looks odd to me to create class for holding single variable. Consider to use simple string variable lastVisitedPath instead. If you use this variable in single class, then make it field of that class.
Second - naming is not very readable. Here is how getting last path looks like: LastPath.lastPath. You see this useless duplication? Also keep in mind, that due to Microsoft naming guidelines public members should have Pascal Case names. Consider to create class with descriptive name like GlobalValues or Cache which reflects its purpose:
public static class GlobalValues // holds values which are globally accessible
{
public static string LastVisitedPath;
// other global values
}
So that usage will look like GlobalValues.LastVisitedPath or Cache.LastVisitedPath. Of course, if these classes don't supposed to be instantiated, they should be static.

can't acess public static member of one class from another class

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++;
}

Categories

Resources