I've read the other threads on the subject but I'm having no joy.
I'm trying to update the content of my wpf TextBlock from inside a static method UpdateTextBlock, this method needs to be static as I'm calling it from inside a Timer and it'll only work for some reason if it is static.
public static void UpdateTextBlock()
{
foreach (String s in GetWhoList())
{
TextBlock1.Inlines.Add(s);
}
}
Basically need to find a way to reference TextBlock1 to the object that it appears in I think. Struggling to get my head around the methodology of it. I understand that it doesnt know what to reference because it's static, but not sure the commands to tie it to the TextBlock in my Windows App.
I'm generating error code CS0120
An object reference is required for the non-static field, method, or property 'MainWindow.TextBlock1'
Create a static property that contains TextBlock1's control. TextBlock1's window can initialize it, for example, in the "loaded" event or anywhere else you think is right.
Consider removing static modifier from the method that works with non-static objects instead.
Related
I want to call a method from 'Check.cs' in MainWindow.
I've tried to set instance.
Check ch = new Check();
class Check
{
public static string IsOpen(string text)
{
// My logic
}
}
I expected to be able to set instance, but whenever I want to I can't find it, also I'm not able to set it.
First of all, make your class public. Now you should get errors trying to create instance of that class.
Then, you need to add reference to library (.dll) that this is compiled to, unless this is in the same project.
Also, you need using directive to point to a namespace, where Check class is located.
Finally, as already said, your method is static, so you don't need to create instance to call this method. Although it can be called on an instance as well. So both ways are valid:
var ch = new Check();
ch.IsOpen();
Check.IsOpen();
is it possible to initialize a static class on app start up "automatically"?
By automatically I mean without the need of referencing a property.
The reason I want to be able to do this for is that I'd like to automatically theme an app on start up.
Here's a short snippet:
static class Settings{
private static Theme _defaultTheme;
public static Theme DefaultTheme{
get{
return _defaultTheme;
}
private set{
_defaultTheme = value;
ThemeManager.SetTheme(value);
}
}
static Settings(){
DefaultTheme = Themes.SomeTheme;
}
}
I know I can ( and that's how it is at the moment ) go with original getter/setter and call
ThemeManager.SetTheme( Settings.DefaultTheme );
in constructor of App ( it's WPF project ) and it'll do the job, however, at least from my point of view ( correct me if I'm mistaken please ) it'd make more sense for the default theme to apply without the need of explicitly stating it.
is it possible to initialize a static class on app start up "automatically"? By automatically I mean without the need of referencing a property.
The only way to guarantee that the static constructor will execute is to use the type in some form. It does not necessary need to be referencing a property (it could be constructing an instance, using a method, etc), but you do need to use the type. It is possible for the static constructor to never run otherwise.
Your current option, or a variation of it, seems like the best solution. You could change this to having a single call such as:
Settings.InstallDefaultTheme();
If you prefer, since the reference of Settings would force the static constructor to execute.
Is there a way to access wpf ui elements from a static method? I'm trying to access a textbox string from a static method to no avail
This would be impossible. Your static method can run while the textbox doesn't exist because there is no instance of your form.
Static methods are designed to be able to run without any instance.
Quote from wikipedia:
Therefore, a static method cannot
refer to a specific instance of the
class (i.e. it cannot refer to this,
self, Me, etc.)
Need more code to give a better answer.
e.g. if you pass in a Window parameter to the static method or if you pass in the ViewModel object that has the property to which your textbox binds to, I'd say you would be able to affect the WPF UI from your static method.
I want change the text of a label on one form to the text of a button on another form when I press the button.
To do this I have created this on the form where the label is
public static void changeText(string text)
{
L1.text = text;
}
this code is on the form with the button
mainForm.changeText(this.Text);
It then gives the error : An object reference is required for the non-static field, method, or property...
This may seem like a stupid question but I am still new to C# so please help me.
About static and non-static members
There are two kinds of type members: non-static and static. Non-static members are also called instance members, because they appear in the object instances of the type. Static members are bound to the type itself, and not its object instances, so you can use them without actually instantiating the type.
Consider the following:
class MyClass
{
// static member: can NOT reference 'this', as it is not in the context of an object instance of the type
public static bool IsTrue()
{
return true;
}
// constructor: this runs whenever the type is instantiated
public MyClass()
{
}
// instance member: can access to 'this', which references the context object instance of the type
public int GetNumber()
{
return 42;
}
}
You can use the above type as following:
if(MyClass.IsTrue()) // static call
{
var myObject = new MyClass(); // constructor call
int result = myObject.GetNumber(); // instance member call
Console.WriteLine(result);
}
On to your specific problem...
If you're perfectly sure that you need that logic inside a static method, you'll need to get an object instance of the form you wish to change.
Unfortunately singletons don't work very well, because the VS designer needs to create an object instance of your Form, which obviously violates the singleton pattern.
What you can still use, is (in case of a Windows Forms application): Application.OpenForms. This returns a read-only collection that contains all currently open forms of the application. You can use this to find the object instance of the form you want to change, and then perform that change.
Be advised that if this is a multi-threaded situation (i.e. the static method runs in a different thread than the GUI thread), you'll have to use some sort of synchronization mechanism, such as InvokeRequired and Invoke().
L1 is not static, so you cant have a static function interacting with it. Having a static let you able to write something like MainForm.changeText(...), but in this case what is L1 ?
I think we can say:
You dont need a function to change the label text, the property Text is already written
If there is some logic needed to mangle the tet before you can:
Consider if the function you need is so general that can apply to many labels across your app, in this case an extension method would be good. In other case if you want a function in the main form to set the text somewhere, and this place can change, or the text need some mangling, a member function would be good, and probably a DataBinding would be better.
You don't want to use a static method for this since L1 is a member of the mainForm class.
The error means your static function is accessing a non-static variable (control L1).
Static functions can only access to static variables. You can change L1 to static variable to make it work.
Hey! I've made a little boiler system that's controlled entirely by a form. The form components, however, call functions in a class for the boiler, radiators and so on.
I've got a little main class to that instantiates all of the classes but I'm struggling to figure out how to pass the form object to those classes so that they can access the form's components.
I guess I should be using mutator methods in each class to store the form object? How would I do this that's syntactically correct?
Thank you! ;o)
Just pass the form to each class. Store it in a private variable so the class can use it later. It is passed by reference by default.
class Boiler {
private Form parentForm;
public Boiler(Form f) {
parentForm = f;
}
}
When you pass a reference type to a method, C# (by default) will pass a copy of the reference to the method. This means that if pass the reference you have to your classes you are giving the method a copy of that reference and since both copies reference the same object both the call site and the method will have access to the same instance.
For example:
class Example
{
static void Main()
{
string s = "hello, world";
// Here we are passing a copy of the reference
// stored in "s" to "Print"
Print(s);
}
static void Print(string str)
{
// By default, "str" will be assigned the copy of the
// reference passed to this method.
Console.WriteLine(s);
}
}
I would be careful building an application in which your domain objects (in your case, Boiler, Radiator, etc.) know about the UI layer that consumes them. If you find that you need to pass a Form to one of these domain models you are probably doing something wrong. If you show us a small example of what you are trying to accomplish we might be able to help you come up with a more maintainable solution.