Accessing form instance properties as if they are static [duplicate] - c#

This question already has answers here:
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Objects implicitly instantiated in vb.net?
(2 answers)
Closed 7 years ago.
Recently I've picked an old project in VB.NET and I keep finding things like this that annoy me:
Dim answer as String
answer = Form_Input_Text.Lbl_Answer.Text
Apparently, in VB.NET it is allowed to access stuff which should belong to an instance of a Form as if it is something static. In C# one would need to write this:
string answer = null;
using (Form_Input_Text my_form = new Form_Input_Text())
{
//stuff
answer = my_form.Lbl_Answer.Text;
}
The first bit of code is compiled with no errors, but sometimes I get NullReferenceException as expected.
How do I enforce this behaviour on VB.NET?

Related

How to check if a interface reference is a specific class [duplicate]

This question already has answers here:
Type Checking: typeof, GetType, or is?
(15 answers)
Checking if the object is of same type
(4 answers)
How can I check if an object is of a certain type at runtime in C#?
(9 answers)
Closed 3 years ago.
I was wondering if there is a way to find if my interface reference is a specific class.
For example i have DeviceInterface reference, and Playstation, PC and Mac all implement it. Is there a way to see if DeviceInterface is a PC?
I have thought about using a enum to define the type and using that, but is there a way of avoiding this and using a type check or something along those lines?
Thanks in advance.
Let's say that you have
DeviceInterface PcDevice = new PC();
In that case you can just do:
if (PcDevice is PC) { console.WriteLine("I'm a PC"); }
read more here to understand the is and as operators better

Object initialization vs property assignment [duplicate]

This question already has answers here:
Setting properties via object initialization or not : Any difference ?
(5 answers)
Is there any benefit of using an Object Initializer?
(5 answers)
Closed 3 years ago.
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
{
FirstName = GetFirstName()
}
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...

C# lambda operator for assignment [duplicate]

This question already has answers here:
What does the => operator mean in a property or method?
(7 answers)
Closed 7 years ago.
I came across this in our code-base today, and it took me awhile to see what the effect of it was, but what in the world does this actually mean??
public virtual SomeClass InstanceVariable => new SomeClass("arg1", "arg2");
I played around with this in Visual Studio's C# interactive terminal and discovered that it appears to be equivalent to:
public virtual SomeClass InstanceVariable { get { new SomeClass("arg1", "arg2"); } }
However, I couldn't find any documentation on this being any form of 'syntactic sugar' for a read-only property.
Someone want to shed some light on the scenario?
It is from new C# 6.0. You can instantiate your class by default. Check "Expression Bodied Functions and Properties" of https://msdn.microsoft.com/en-us/magazine/dn802602.aspx article

Modifying static variables from other scripts in Unity [duplicate]

This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers

Application.OpenForms - seems to be missing [duplicate]

This question already has answers here:
WPF version of Application.OpenForms
(3 answers)
Closed 8 years ago.
I have a small wpf/c# application and want to get a list of all of the open views. However, the Application.OpenForms, does not have "OpenForms" in the intellisense (and gives me an error). Could I be missing a specific reference of something?
I have tried: System.Windows.Application.OpenForms;
Based on the docs for the Application class, there is no OpenForms member.
http://msdn.microsoft.com/en-us/library/system.windows.application(v=vs.110).aspx
Try Application.Windows
var openWindows = System.Windows.Application.Current.Windows;

Categories