Judge whether a property is created by a function via reflection - c#

Suppose I have a class Foo like below:
class Foo
{
public static int Bar()
{
return 1;
}
public static int x = Bar();
public static int y = 2;
}
I want to use reflection to know that:
x is initialized via the function Bar.
y isn't initialized via the function Bar.
Is there any way to do this?

Sorry but none of your properties are created from your function Bar anyway.Are you talking about initialization and or when/where are they assigned?
Please clarify your question.
A similar question was asked here: When do static variables get initialized in C#?
EDIT
Based on the new information you can change Access Modifiers or utilize the [Obsolete] attribute: https://learn.microsoft.com/en-us/dotnet/api/system.obsoleteattribute?view=netframework-4.7.2
to control or restrict creation.

Related

how to add values to a class variable outside of the class

It's been a long while since I have done anything with C# and I decided I wanted to give it a go again. Basically I want to create a class add some variables to it that I can reference outside of the class.
class classname (variablename)
{
function
}
classname (varaiblename = value)
I understand this is probably very much incorrect but that is the gist of wha tI would like to do.
Thank you very much for your time.
You're looking for Properties:
class Foo
{
public int Bar { get; set;}
}
Foo obj = new Foo();
obj.Bar = 5;
I think you need public static variables:
Let's say you've integer variable in class A and you want to control this variable from other classes. There's static variable term which let's you change value of static variables from other classes and it'll affect all of the instance of classes:
Here is my quick code:
public class A {
public static int myValue = 5;
}
So, when you declare your variable like this, you can use and control this variable from other classes such as:
Console.WriteLine(A.myValue); or A.myValue += 5;

Am I setting the value of the reference when changing a field of my class, or just the value of a value?

I am writing a piece of code which involve a lot of duplication of data and operations and it is important to me that I minimise this as much as possible. The MSDN support doc, to me, implies that when I set the field of my class Foo as an instance of some class Bar I am setting this as a reference to this instance, rather than the value, which is exactly what I want to do. Given how important this feature is to me however (it will affect the runtime of my program by orders) could someone wiser please confirm for me.
Code example:
public class Foo
{
public Bar ChildBar {get;set;}
public void SetChildBarValue (int value)
{
this.ChildBar.Value = value;
}
}
public class Bar
{
public int Value {get;set;}
}
var foo1 = new Foo();
var foo2 = new Foo();
var bar = new Bar();
foo1.ChildBar = bar;
foo2.ChildBar = bar;
foo1.SetChildBarValue(1);
Console.WriteLine(foo2.ChildBar.Value);
My question is, will the WriteLine return 1 or null?
Thanks in advance.
My question is, will the WriteLine return 1 or null?
As Bar is a declared as a class, hence a reference type, and you're setting both ChildBar's to point to the same instance of bar, which results in a reference assignment to that bar, your WriteLine will print 1.
As a side note - actually running this code would have told you the same thing.

What is the purpose of get : set? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Many code samples in C# contain get and set code blocks. What is the purpose of them? I copy these blocks whey they appear in sample code, but have no idea what it is used for. Can someone please explain this to me?
Getters and setters enable you to combine a pair of functions into one property, and let you use a syntax that looks like a member access expression or an assignment in place of syntax that looks like an explicit function call.
Here is a small example: instead of this
internal class Example
{
private int x;
public int GetX() => x;
public void SetX(int value) => x = value;
}
...
var e = new Example();
e.SetX(123);
Console.WriteLine($"X = {e.GetX()}");
They let you do this:
internal class Example
{
public int X { get; set; }
}
...
var e = new Example();
e.X = 123;
Console.WriteLine($"X = {e.GetX()}");
The syntax of the second code snippet is easier to read, because X looks like a variable. At the same time, the second snippet provides the same level of encapsulation, letting you hide the implementation behind the property.
Do you mean this?:
public int SomeValue { get; set; }
This is basically syntactic shorthand for this:
private int someValue;
public int SomeValue
{
get { return someValue; }
set { someValue = value; }
}
Which itself is basically shorthand for this:
private int someValue;
public int GetSomeValue() { return someValue; }
public void SetSomeValue(int value) { someValue = value; }
(Though the compiler uses different conventions for the names of things when it does this.)
As it relates to OOP, this is the encapsulation of data. The idea is that objects should hide their data and expose functionality, instead of just exposing the data directly. So you don't necessarily modify someValue directly from outside the object. You call a method on the object and supply it with a value. Internally the object handles the actual storage of its data.
public int foo { get; set; }
This defines a property. It's basically like a public field but when it comes to reflection it's different. In C#/.NET it's common to use properties for public things. You can compare it with getter/setter methods in Java.
The awesome thing now is, that you can also use custom get/set code or make set less visible than get. That allows you to have the advantages of getter/setter methods without the ugliness of method calls instead of property accesses.
public int foo {
get { return this.some_foo; }
set { this.some_foo = value; this.run_code_after_change(); }
};
In english they are setters and getters.
Hope you teach yourself encapsulation, setters and getters were rised due to encapsulation.
Again in plain english, setters and getters give you the ability to access the property you define.
get and set are kind of syntactic sugar. It is the more readable way of implementing functions getting no params where you can insert for example validation (in setter) or calculations on fields in getters. Parentheses are useless in function like that.

How to use Application.Current.Properties or some other Global variable

I have a class called RandomGen which generates random variables when called like so:
RandomGen u = new RandomGen();
int QRNOne = u.QuestionRandNo(n);
n++;
How do I then set QRNOne as a global variable to be accessed later? Also, can I do this from within the class, and have it create this variable? (I realise there is no "real" global variable in C# WPF, but you ought to understand what I'm referring to.)
Please read up on static class members.
I'm sure you know that global variables are best avoided if possible, but if you really need a static member variable then you would do something like this:
public class MyClass
{
public static int QRNOne {get; private set;}
public void GenerateRandom(){ ... ; QRNOne = ...; }
}
And then refer to that variable from elsewhere as
public void SomeOtherMethod()
{
int qr = MyClass.QRNone;
...
}
Can you use a static, Singleton style class with properties tor the values you want to use? This should then have global visibilty with sufficient structure and encapsulation.

Why and when there are things I can not do in CLASS SCOPE in C#?

well... I'm confused about what can I do and what I can't do in CLASS SCOPE.
For example
=========================
class myclass
{
int myint = 0;
myint = 5; *// this doesnt work. Intellisense doesn't letme work with myint... why?*
void method()
{
myint = 5; *//this works. but why inside a method?*
}
}
==================================
class class1
{
public int myint;
}
class class2
{
class1 Z = new class1();
Z.myint = 5; *//this doesnt work. like Z doesnt exists for intellisense*
void method()
{
Z.myint = 5; *//this Works, but why inside a method?*
}
}
Thats I make so many mistakes, I dont understand what works on class scope and what doesnt work.
I know that there are local varaibles and its life cycle. But I dont understand the so well the idea.
A class can only contain declarations, initializations, and methods. It cannot contain statements of its own.
class MyClass
{
int x; // a declaration: okay
int y = 5; // a declaration with an initialization: okay
int GetZ() { return x + y; } // a method: okay
x = y; // a statement: not okay
}
You can initialize a field, but you can't just have assignment statements.
You can't do the below:
class myclass
{
int myint = 0;
myint = 5;
Because there is nothing to do, you can only declare members and possibly set an initial value. It is nothing to do with scope. One of the reeasons you can't do it is that there is no guarantee of order, the compiler just gurantees all values will be initialised by the time the class is instantiated. That is why you also cannot do the following:
class myclass
{
int myint = 0;
int MyOtherInt = myint;
If you want to set the value when the class is instantiated, put it in the contructor.
Basically, when you create a class, you are not allowed to have code & assignments inside that class directly.
but as c# wants to help you, it allows you to define initial states for fields. and they are basically executed directly bevore your constructor code.
when you now want to define code inside the class. somewhere... the program would not know, when to execute. so they are forbidden.
A class definition defines its makeup, that is, the fields, properties, methods, events, etc. that it contains. The "work" is actually done inside methods.
What you seem to be confusing is the initialization of the fields of a class with the usage of those fields. You can initialize a field outside a method definition, but to use it, you need to be inside a method.
To clearly understand it and get answers on your questions you should read Jeffrey's Richter "CLR via C#, Third Edition" and C# 4.0 Language Spectification

Categories

Resources