I would like to know how to access a public var in Program class of the console app.
class Program
{
public static string Name { get; set; }
static void Main(string[] args)
{
// Some code here
}
}
static class Settings
{
static public void DoJob()
{
// Access Name of Program ?
}
}
Sure you can do this, But the args is a string array and the property Name is a string variable, So you need to assign one value from args to the Name. Or use String.Join to get all values to Name with a delimiter.
Since the Name is static variable no instance is needed to access the variable. You will get the value through Program.Name in the static class. Now see the code:
In Main getting value from args to Name
public static string Name { get; set; }
static void Main(string[] args)
{
Name = args[0]; // taking the First value from the args array
//or use String.Join to get all elements from args
string delemitter = "";
Name = String.Join(delemitter, args);
}
In Static class assign value of Name to a local variable:
static class Settings
{
static public void DoJob()
{
string localVar = Program.Name;
}
}
Related
I cant access static method from new object and not allow create same name non-static method.I need to use same name method static and non-static.
Foo class has some default variables. I create new object and set default variables.
Sample code block
class Foo
{
public void abc()
{
//...
}
public static string xyz(string s)
{
return "bla bla";
}
}
public void btn1_click()
{
System.Windows.Forms.MessageBox.Show(Foo.xyz("value"));
//Works OK
}
public void btn1_click()
{
Foo f1=new Foo();
//f1..
f1.xyz("value");
//Cant access non static method.
}
Thanks in advance.
If the class has default values, the correct place to populate them is in the class constructor:
public class Foo
{
public Foo()
{
// set default values here.
}
}
If you still want to use these default values as static members - no problem:
public class Foo
{
public static const int DEFAULT_INT_VALUE = 5;
public Foo()
{
IntValue = DEFAULT_INT_VALUE;
}
public int IntValue {get;set;}
}
I'm playing with VS. I'm a rookie, It must be basic. I have created 2 classes and I puzzled with result. I'm using Visual Studio 2015 community edition.
I'm expect to receive at console :
myfirstClass
Class
first.
mysecondClass
Class
second.
I received :
myfirstClass
Class
_
class Program
{
public class mysecondClass
{
static public string myName ;
public mysecondClass()
{
Console.WriteLine("mysecondClass");
myName = "Class";
}
public static void Display()
{
Console.WriteLine(myName);
}
~mysecondClass()
{
Console.WriteLine("second.");
}
}
public class myfirstClass
{
public string myName;
public myfirstClass()
{
Console.WriteLine("myfirstClass");
myName = "Class";
}
public static void Display()
{
myfirstClass d = new Program.myfirstClass();
Console.WriteLine(d.myName);
}
~myfirstClass()
{
Console.WriteLine("first.");
}
}
static void Main(string[] args)
{
myfirstClass.Display();
mysecondClass.Display();
Console.ReadLine();
}
}
This is not a bug in Visual Studio. I think you have got two concepts wrongly.
Firstly, the finaliser of a class will not be called immediately after the object is out of scope. It will be called at a random time. It is quite unpredictable.
Therefore, this:
Console.WriteLine("first.");
is not printed.
The second thing is that constructors of a class is only called when you write new XXX(...) (or through reflection). Just calling a static method will not invoke the constructor.
In other words, these lines are never executed:
Console.WriteLine("mysecondClass");
myName = "Class";
You never wrote new mysecondClass().
When this line in the display method of mysecondClass executes:
Console.WriteLine(myName);
Since myName has not been assigned, it is null, and so nothing is printed.
class Program
{
public class mysecondClass
{
public string myName { get; set; }
public mysecondClass()
{
Console.WriteLine("mysecondClass");
myName = "Class";
}
public static void Display()
{
var mySecond = new mysecondClass();
Console.WriteLine(mySecond.myName);
}
~mysecondClass()
{
Console.WriteLine("second.");
}
}
public class myfirstClass
{
public string myName { get; set; }
public myfirstClass()
{
Console.WriteLine("myfirstClass");
myName = "Class";
}
public static void Display()
{
myfirstClass d = new myfirstClass();
Console.WriteLine(d.myName);
}
~myfirstClass()
{
Console.WriteLine("first.");
}
}
static void Main(string[] args)
{
myfirstClass.Display();
mysecondClass.Display();
Console.ReadLine();
}
}
try this
I have a Settings class to store the setting parameters as Properties. This class has its default Property variables and I want to set them default whenever I want. I had saw this usage in a video lesson but I can't remember clearly how he did this. Help please.
Settings.cs
class Settings
{
static Settings()
{
SaveDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Type = ImageFormat.Png;
}
public static string SaveDir
{
get;
set;
}
public static ImageFormat Type
{
get;
set;
}
}
Form.cs
Debug.WriteLine(Settings.SaveDir);
Settings.SaveDir = "path_to_another_directory";
Debug.WriteLine(Settings.SaveDir);
//In this line I expect that variables set their default
//but it stays the same
new Settings();
Debug.WriteLine(Settings.SaveDir);
This code doesn't work as I expected. How can I reassign these values calling new Settings()?
You can't call a static constructor as explained in the C# reference
A static constructor cannot be called directly
However nothing blocks you to write a method that resets the initial values
static Reset()
{
SaveDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Type = ImageFormat.Png;
}
Instead of setting the properties directly in the static constructor for Settings, try doing that in a different method, like this:
class Settings
{
static Settings()
{
Reset()
}
public static void Reset()
{
SaveDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Type = ImageFormat.Png;
}
public static string SaveDir
{
get;
set;
}
public static ImageFormat Type
{
get;
set;
}
}
If you want to reset the settings, for example in Form.cs, call Settings.Reset().
First, you can't call yourself a static constructor. Second, a static constructor can't be called twice. Here a MSDN post about it.
What about that.
class Settings
{
static Settings()
{
LoadDefault();
}
public static void LoadDefault()
{
SaveDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Type = ImageFormat.Png;
}
public static string SaveDir
{
get;
set;
}
public static ImageFormat Type
{
get;
set;
}
}
Then call Settings.LoadDefault() anywhere, anytime.
It seems that an extension method in C# cannot overwrite the original object. Why is that? Example:
using System;
namespace ExtensionTest
{
public class MyTest {
public string MyName { get; set; }
}
class Program
{
static void Main(string[] args)
{
var myTest = new MyTest() { MyName = "Arne" };
Console.WriteLine("My name is {0}", myTest.MyName);
// Will write "My name is Arne"
myTest.AlterMyTest();
Console.WriteLine("My name is {0}", myTest.MyName);
// Will write "My name is Bertil"
myTest.OverwriteMyTest();
Console.WriteLine("My name is {0}", myTest.MyName);
// Will write "My name is Bertil" (why?)
}
}
public static class ExtensionClass{
public static void AlterMyTest(this MyTest myTest)
{
myTest.MyName = "Bertil";
}
public static void OverwriteMyTest(this MyTest myTest)
{
myTest = new MyTest() { MyName = "Carl" };
}
}
}
Because as usual, reference of the class is copied while passing to the method, and you are assigning new object to the new reference.
For not-extension methods, you can pass reference by ref/out keywords
public static void Func(out MyClass b)
{
b = new MyClass();
}
...
MyClass b;
Func(out b);
Assert.IsNotNull(b);
but C# compiler doesn't allow to use ref with this(the reason is in David Arno's comment). You are free to remove this keyword, and call static method instead of extension.
I have a static member:
namespace MyLibrary
{
public static class MyClass
{
public static string MyMember;
}
}
which I want to access like this:
using MyLibrary;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
MyMember = "Some value.";
}
}
}
How do make MyMember accessible (without MyClass.) to MyApp just by adding using MyLibrary?
C# doesn't allow you to create aliases of members, only of types. So the only way to do something like that in C# would be to create a new property which is accessible from that scope:
class Program
{
static string MyMember
{
get { return MyClass.MyMember; }
set { MyClass.MyMember = value; }
}
static void Main(string[] args)
{
MyMember = "Some value.";
}
}
It's not really an alias, but it accomplishes the syntax you're looking for.
Of course, if you're only accessing / modifying a member on MyClass, and not assigning to it, this can be simplified a bit:
class Program
{
static List<string> MyList = MyClass.MyList;
static void Main(string[] args)
{
MyList.Add("Some value.");
}
}