Get public variable from another class - c#

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;

Related

C# Self Initated class - how to use in project

I have created a class and self initialized it in the file - is this the best way to use it? I have another Constant class that I can use but I am unable to use this for some reason in my project, do they need to be consts?
File: test.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace DH.Models
{
public class test
{
public string testSourceCollection { get; set; }
public string testSourceKey { get; set; }
public string testSourceDatabase { get; set; }
public string testSourceCluster { get; set; }
public string testSourceTimestamp { get; set; }
public test[] testDetails = {
new test{
testSourceCollection = "SourceCollection",
testSourceDatabase = "SourceDatabase ",
testSourceKey = "SourceKey ",
testSourceTimestamp = "SourceTimestamp "
},
new ProviderRecon
{
testSourceCollection = "testSourceCollection2",
testSourceDatabase = "testSourceDatabase2",
testSourceKey = "testSourceKey2",
testSourceTimestamp = "testSourceTimestam2",
testSourceCluster = "testSourceCluster2"
}
};
}
}
I would like to use in my Worker.cs file as such
public class Worker : BackgroundService
{
var test = test.testDetails;
Console.WriteLine("2nd test: " + test.testSourceCluster )
//Prints "2nd test: testSourceCluster2"
}
You probably want testDetails to be static. You can't access (non-static) members using a type. Change public test[] testDetails = ... to public static test[] testDetails =...
However, you have a lot of non-standard namings there, which make this code confusing to read. The class test should be called Test instead. The line var test = test.testDetails; is hard to read otherwise (and probably won't compile). Same is true for your Worker class. That piece of code won't compile.
Every time when you create an instance of your class, testDetails property is initialized with new array. If you create 1000 instances for each instance testDetails is initialized with new array.
You can use static field/property to avoid create multiple instances with the same array. static keyword
Or use static readonly property/field to avoid create multiple instances with the same array. static readonly properties/fields can be initialized with static constructors. Static constructors

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;

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.

Static fields question

im trying to understand the get and set properties for fields, and run in to this issue, can somone explaine to me why i had to make the int X field Static to make this work?
using System;
namespace ConsoleApplication1
{
class Program
{
public static int X = 30;
public static void Main()
{
var cX = new testme();
cX.intX = 12;
Console.WriteLine(cX.intX);
cX.intX = X;
Console.WriteLine(cX.intX);
Console.ReadKey();
}
}
class testme
{
public int intX
{
get;
set;
}
}
}
Because you were using the field in a static context, in this case the method publicstaticvoid Main. Since your Program class just runs statically there is no instance and therefore you can't access any instance members.
because it is used in a static method
Since Main is static, you cannot access non-static instances from outside of it.

Static Constants in C#

I have this code;
using System;
namespace Rapido
{
class Constants
{
public static const string FrameworkName = "Rapido Framework";
}
}
Visual Studio tells me: The constant 'Rapido.Constants.FrameworkName' cannot be marked static
How can I make this constant available from other classes without having to create a new instance of it? (ie. directly accessing it via Rapido.Constants.FrameworkName)
public static class Constants
{
public const string FrameworkName = "Rapido Framework";
}
A const is already static as it cannot change between instances.
You don't need to declare it as static - public const string is enough.

Categories

Resources