How to use strings from class for language strings? - c#

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;

Related

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

Get public variable from another class

I have a class where there's a public static variable that I want to access from another class. Here's the class :
In GlobalVariable.cs
public class GlobalVariable
{
public static int MoisVS = 3;
}
And I want to access to "MoisVS" from another class. Here's the class :
In ArretPage.cs
var globalvariable = new ProjetTN.Class.GlobalVariable();
globalvariable.MoisVS; //<--- Make something like that.
I know it's possible in a WinForm app but is that possible in a Xamarin.forms app ?
You defined your MoisVS as static so you can't access it from an instance variable. Accessing static members of a class you're using the class name itself. So for example accessing your MoisVS will look like:
GlobalVariable.MoisVS
If you want to accesss it as an instance property you have to change your class to:
public class GlobalVariable
{
public int MoisVS = 3;
}
If there are only static or const values in your class. You can also decide to make your whole class static
public static class GlobalVariable
{
public static int MoisVS = 3;
public const string MyString = "";
}
this will prevent you from using the new keyword to create an instance of that class.
I will break this into steps.
1) File -> New File -> General -> Choose Empty Class -> Name it (e.g.UICONTROL_NAMES)
2) Add the following: (Sample code)
using System;
namespace iOSControls.HelperClass
{
public static class UICONTROL_NAMES
{
public const string textField = "Text Fields";
public const string inputTextField = "Input types - TextFields";
public const string button = "Buttons";
public const string label = "Label";
}
Have a note:
UICONTROL_NAMES class is inside a folder group called HelperClass and iOSControls is the project name.
3) On your ViewController.cs, add namespace directive.
using iOSControls.HelperClass;
using Foundation;
4) Access const strings in UICONTROL NAMES:
string controlName = UICONTROL_NAMES.textField;

Class Variables and Methods not Available to other class

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.

Access variable from other namespaces

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.

Global Static Class with Objects

Ok, so I know you can't have objects in a static class but i need a class that i can hold objects that are accessible from different classes. I am making a dll that will provide extended functionality to another program so i can't just inherit or pass classes around either. if need be i can just maybe make the properties of each object i need to be in the static class which would work but not be as friendly as i would like. anyone have any other ideas on how to accomplish something like this?
Actually, you can have objects in a static class -- they just have to be static objects.
For instance:
public static class SharedObjects
{
private static MyClass obj = new MyClass();
public static MyClass GetObj()
{
return obj;
}
}
And from elsewhere in your program you can call instance methods/properties/etc.:
SharedObjects.GetObj().MyInstanceMethod();
One option is to have a class with the accessors methods accessing a static object (or objects). The other parts of your system can use the class either as static or as a non-static. Here is the code:
public class GlobalInformation {
public static GlobalInformation CreateInstance() {
// Factory method through GlobalInformmation.CreateInstance()
return new GlobalInformation();
}
public GlobalInformation() {
// Regular use through new GlobalInformation()
}
static GlobalInformation() {
// Static initializer called once before class is used.
// e.g. initialize values:
_aString = "The string value";
}
public string AccessAString {
get {
return _aString;
}
}
public Foo AccessAnObject() {
return _anObject;
}
private static string _aString;
private static readonly Foo _anObject = new Foo();
}
Other parts of your system would use it as follows. Option 1:
var globalInfo = GlobalInformation.CreateInstance();
var aString = globalInfo.AssessAString;
var anObj = globalInfo.AccessAnObject();
Option 2:
var globalInfo = new GlobalInformation();
var aString = globalInfo.AssessAString;
var anObj = globalInfo.AccessAnObject();
Option 2 would be my preferred one (I'd remove the static factory method CreateInstance()) as you could change the implementation at any time including making (some of) the fields non-static. It would appear to be a regular class while sharing data.

Categories

Resources