Passing local variable to a global function - c#

In my program i am passing a locally constructed variable to a global class object's member function. The member function will assign this variable to a private member and use it through out the program. Is there any drawback in this approach?
public void function()
{
int a = 0;
globalClassObject.StoreValue(a);
}

This is fine. The problem would be if you would pass a reference to this variable. In this case the value is "copied" to the variable within the function so the original a variable isn't actually used.

I don't see a problem with this. The variable is used in local scope only, so no unexpected results here.

The big drawback is if this value can be changed anywhere else in the program. If it can, your program will become a swamp for bugs to breed. If the globalClassObject stores the value immutably, there is no problem.

Related

C#: ensure static variable retains its value

Goal:
I am having some issues in C# when ensuring that a variable retains its assigned value for the duration / runtime of a program. In particular, I am attempting to:
Read a number in from some input
Save it to a variable of type double
Print the variable
Ensure that this value stays the same for the duration of the program, so that on the next iteration when a new value is read in, the previous value from step (2) is still printed.
Attempts:
I have attempted to use a global static variable such as public static double foo;, however, whenever the method that retrievs new values is called again, a new value is printed.
In C, for example, I believe the closest functionality to this would be to use the static keyword, however, I have read that this functionality has been intentionally excluded from C# and I am struggling to learn why this is the case.
void foo()
{
static int j;
int i = calculateSomeValue() + j;
j = i;
}
Please note that before deciding to post this question, I have consulted the following resources:
https://softwareengineering.stackexchange.com/questions/141871/false-friends-keyword-static-in-c-compared-to-c-c-and-java
https://learn.microsoft.com/en-us/answers/questions/240965/retain-previous-value-of-text-box-till-the-form-is.html
Retain local variable across method calls
How to retain old value when setting variable on c# class
https://social.msdn.microsoft.com/Forums/en-US/46243c7b-959e-4753-a173-34f52a7f8922/variables-retain-value-from-first-pass?forum=csharpgeneral
Q1. How can a value assigned to a variable be retained for the entire runtime of a 'C#' program?
Apologies if I am missing something extremely obvious, but it seems that the static keyword in C is used in a different manner compared to that of C#.
According to your description, the variable is re-assigned at step #2.
It is expected that step #3 (Print the variable) will output the changed variable value at each iteration.
Your understanding of "static" meaning in C# is correct, at least in the scope that you have described.

Use of temporary private variable in ASP.NET MVC controller

I'm working on doing some refactoring of functions in an existing ASP.NET MVC application, and came across the following small recursive function within a controller. I'm a little new still to C# & .NET, so please bear with me if I get some things wrong.
private int _bl;
[Session]
public void TotalMaterialSubCategories(IEnumerable<Category> materialMasterCats, int i)
{
foreach (var materialMasterCat in materialMasterCats)
{
_bl = _bl + 1;
materialMasterCat.Level = i;
if (materialMasterCat.ChildCategories.Count != 0)
{
TotalMaterialSubCategories(materialMasterCat.ChildCategories.ToList(), i + 1);
if (materialMasterCat.Parent == 0)
{
materialMasterCat.SortOrder = _bl;
_bl = 0;
}
}
}
}
The thing about it that concerned me is this private int _bl statement. Within this class, the only references to this variable are associated with this function.
I thought the line _b1 = _b1 + 1 might not be reliable because it's not manually initialized. Looking into it though, I believe that since it's an int, it cannot be null or left 'uninitialized', so it's getting a default value of 0; corroborated by the MS Docs. The way the recursion looks like it bubbles up makes me think it'll be set back to 0 at the end of this function call as well. Finally, I'm pretty sure each independent web request gets a separate instance of this controller, so it seems like the way this is written, it should function as expected.
However, I just sort of wondered why this would be written like this. Is there a reason that this isn't just a local variable to the function, initialized with 0? Can you rely on local private variables across functions in controllers? Also, are any of my assumptions / determinations incorrect?
The thing about it that concerned me is this private int _bl statement. Within this class, the only references to this variable are associated with this function.
Obviously we can't see all the code, but if I were to believe you that this Field(not variable) is only referenced by this function then I would surmise that either it's left over when other methods may have referenced it and now those methods no longer exist, used it OR it used by a method to maintain state beyond the execution of any single method (event or property).
The way the recursion looks like it bubbles up makes me think it'll be set back to 0 at the end of this function call as well.
It's set to zero before the constructor is called.
Not necessarily. It's only set back to zero after the method ends if (materialMasterCat.Parent == 0) for each instantiation of this class.
Because controllers aren't static classes, each class create has it's own private version of _bl that can only (normally) be access by the class itself (private access modifer).
However, I just sort of wondered why this would be written like this. Is there a reason that this isn't just a local variable to the function, initialized with 0?
Because then when this code ran, all of them would have a value of 1:
materialMasterCat.SortOrder = _bl;
But what's happening is that each time the function is called, it's being incremented because it's exists in the class's scope, not the functions scope.
For Example
Can you rely on local private variables across functions in MVC Controllers?
Private variables are always available until a class is disposed (generally), this time is referred to as the Lifetime of an object.
I believe that since it's an int, it cannot be null or left
'uninitialized', so it's getting a default value of 0; corroborated by
the MS Docs
Try putting the private int _bl; inside of a function as int _bl; and you will see that you get a compile error because you can't perform a _bl = _bl + 1; operation on uninitialized value. So, it doesn't get automatically initialized in scope of a function, but it does get automatically initialized when it's a property of a class instance.
You can read more on when a value gets assigned and when it doesn't here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/variables This particular case falls to:
An instance variable of a class comes
into existence when a new instance of that class is created, and
ceases to exist when there are no references to that instance and the
instance's destructor (if any) has executed. The initial value of an
instance variable of a class is the default value (Default values) of
the variable's type. For the purpose of definite assignment checking,
an instance variable of a class is considered initially assigned.

Is there a C# equivalent to Delphi's assignable const?

In Delphi you can declare a constant in a method and assign it a value in that same method. The idea is that next time you are in this method, the const will still have the same value that you last assigned it.
It looks like this in Delphi :
(I don't have Delphi here so it is untested, sorry about that, but it is enough to demonstrate what I mean)
procedure Form1.test;
const
AssignableConst: Integer = 0;
begin
ShowMessage(AssignableConst);
inc(AssignableConst);
end;
Every time you call the procedure test the messagebox will show the last value + 1
This example is completely useless I know that it is just to show how an assignable const works in Delphi.
Is there an equivalent for this in c# ?
I don't want a solution that involves private variables in the class, it has to stay inside the method.
My question is about scope. Nothing else. I don't care that the value is part of the state of the object. That is not important. What is important is that I need a variable that is only accessible from the scope of a method, nowhere else.
In Delphi this can be done by using an assignable const, so how can I do this in C#?
I don't want a solution that involves private variables in the class, it has to stay inside the method.
But the value is part of the state of the object (or type, for a static method) - so it makes sense for it to be a field declared in the object.
There's no equivalent to this in C#. The best you can do is have a private variable and document that it should only be used from a specific method. (You could write Roslyn-based tests for that if you really want.)
The closest you can get is:
class MyClass
{
static Action CreateCounter()
{
int counter = 0;
return () => {
Show(counter);
counter++;
};
}
Action showAndIncrementCounter = CreateCounter();
public ShowAndIncrementCounter()
{
showAndIncrementCounter();
}
}
But I still recommend the simple solution of using an instance field for the counter and just not accessing it from outside the method.

How ref & out works across methods with local variables?

We cannot specify access modifiers for fields in a method, The reason I suspect for this is that the fields inside a method (i.e local variables) should have scope only inside that particular method so there is no need to specify the access modifiers.
class Program
{
public static void Main()
{
int Y;
Test(out Y);
}
private static void Test(out int X)
{
X = 17;
}
}
Question:
If scope of method field is within that method then how ref and out works across methods? I know they are passed by reference but how CLR can pass that variables address(reference) when its scope is limited to that particular method?
If scope of method field is within that method then how ref and out works across methods? I know they are passed by reference but how CLR can pass that variables address(reference) when its scope is limited to that particular method?
The variable address is not bound to it's scope.When you have the address, you can access the object in that address from whereever you want.This address is used whenever you make changes in the ref or out parameter. Compiler uses that to access and manipulate the actual data.So the scope has nothing to do with it.
It makes no sense to try and declare a private variable in a method . Because by default you can't acces a variable outside a method. Variables that you declare in a method are called local variables. So it wouldn't make any sense to be allowed to give it an acces modifier
These are local variables which are accessible in the method's code block (methodName{..code-block-here..}) scope only and you can't specify any other accessibility level to them - there is no sence to access them from the outside world.
Modifiers are meant to control external class access to variables.
For example, you might not want any class other than your own to access a variable, or you just want subclasses and classes in the same package to access it. Thats the job of modifiers.
A method variable, however, is not accessible by anything outside of that method, and won't live for longer than the time it takes for the method to complete, so there really no point to having modifiers inside the method. The variable access is fixed as its scope is very limited.

What does .Net do when you declare an object without an instance?

I wonder to know how the .Net Framework handles the declared but not instantiated object situation.
For example i declare an object like
DropDownList ddl;
and do nothing about it. I know that i should do something with this variable and get a warning about it, but what i don't know is the where it will be stored.
Is there a lookup table that stores the data of all declared variables? Or is there a virtual reference for every declaration?
Edit : I just wanted to know how the memory allocated for this object declaration.
Edit2 : Whether it's a local variable or not, i'm just talking about the memory allocation structure. I wonder to know where this references stored.
If ddl is a field, then the value of ddl will be null, as it is a reference type.
Any attempt to call a member on it will result in a NullReferenceException.
If it is a local variable it will simply be unassigned.
Value types will get the default(T) of their type.
The compiler itself may remove the call completely, depending on where it was declared, but this is an implementation detail.
If you are talking about a local variable then the compiler can simply optimize it out of existence since noone can be using it (if you attempted to use it without initializing the compiler would have protested with an error). In fact the .NET 4 compiler did this for me when I tested just moments ago.
If you are talking about a field in a class then it is initialized with the default value for its type as part of the object construction.
From your description, it sounds like you're talking about a local variable. When you declare a local variable in usual implementations and without any optimizations, then space is reserved for it on the stack (most probably), with a null reference as its value.
You could look into the StackFrame class if you want to inspect further (I've never used it).
The variable is stored in your assembly. It will always have it's default value null.
In release mode (compiler is set to optimize) it's optimized and it is not stored anywhere.
If you want to know more about IL and how the compiler works, wikipedia has a good article to start.
All variables are stored into a class or method. Variables declared into a class can be listed using .NET Reflection :
class Class1 { private int i; public string s; }
typeof(Class1).GetFields(BindingFlags.Instance); // returns all instance fields
typeof(Class1).GetFields(); // returns all instance public fields
typeof(Class1).GetProperties(); // returns all instance public properties
Variables declared into a method cannot be inspected with .NET Reflection mechanisms.

Categories

Resources