public int a;
public void currentvalue(int a)
{
if (a == 5)
{
a = 10;
Console.WriteLine("a" + a);
}
}
how can I change the value of a into 10
When you have both a global variable (field) and a local variable (parameter/local) with the same name in the same scope, the compiler will automatically choose the locally declared variable.
When dealing with a non-static (instance referenced) field, you can still access the field by using the this keyword. Example:
public class MyClass
{
public int number = 2;
public void Calc(int number) //when number: 4
{
int result1 = number * 3; //result1: 12
int result2 = this.number * 3; //result2: 6
}
}
If your globally declared variable is static, you can't use this (which is only usable on instance references). In that case, use a type reference instead:
public class MyClass
{
public static int number = 2;
public void Calc(int number) //when number: 4
{
int result1 = number * 3; //result1: 12
int result2 = MyClass.number * 3; //result2: 6
}
}
You may want to read up on the this keyword here.
You can use ref keyword and pass the reference into function :
public void currentvalue(ref int a)
{
if (a == 5)
{
a = 10;
Console.WriteLine("a" + a);
}
}
Related
class sample
{
public int i; //global variable
public int[] arr = new int[10];
public void fun(int i, int val)
{
Console.WriteLine(this.i); //I got Output is 11
arr[i] = val;
Console.WriteLine(arr[i]);
}
}
class Program
{
static void Main(string[] args)
{
sample s = new sample();
s.i = 1;
s.fun(1, 7);
Console.ReadLine();
}
}
How to get the global variable i inside the fun() method
Here the function variable name and global variable name are the same.
To access the field i within the fun() method, you can use the this keyword. For example:
class sample
{
public int i; //global variable
public int[] arr = new int[10];
public void fun(int i, int val)
{
this.i = i; // assign the value of the function parameter to the field
arr[i] = val;
Console.WriteLine(arr[i]);
}
}
Alternatively, you can also use a different name for the function parameter to avoid the naming conflict. For example:
class sample
{
public int i; // field
public int[] arr = new int[10];
public void fun(int j, int val)
{
i = j; // assign the value of the function parameter to the field
arr[j] = val;
Console.WriteLine(arr[j]);
}
}
I have a code for extension method which looks something line
parent class
{
int i = 9; // the value was i was 9;
i = i.Add(2); // here 9 + 2
Console.WriteLine(i); // it is printing 11
}
extension class
{
public static int Add(this int firstInteger, int secondInteger)
{
return (firstInteger + secondInteger);
}
}
This is the extension class I have but what I need is
parent class
{
int i = 9; // the value was i was 9;
i.Add(2); // here 9 + 2
Console.WriteLine(i); // it has to print 11
}
I'm not finding a way to do this please recommend a solution.
Firstly, I'd strongly advise you not to do this. It's very counterintuitive.
But as of C# 7.2, this is possible - only for value types - using ref extension methods. Just change the first parameter to have the ref modifier, and assign to it:
using System;
public static class Int32Extensions
{
public static void Add(ref this int x, int y)
{
x = x + y;
}
}
class Test
{
static void Main()
{
int i = 9;
i.Add(2);
Console.WriteLine(i); // 11
}
}
The i.Add(2) call is implicitly:
Int32Extensions.Add(ref i, 2);
It will fail if you try to call it on something that isn't a variable.
But this will be really surprising behavior for many C# developers, as the ref is implicit. It also isn't valid for reference types.
My Code:
public class A
{
public virtual void displayDetailInfo()
{
}
}
public class B : A
{
public String _a;
public int _n;
public B() { }
public B(String _a, int _n)
{
this._a = _a;
this._n = _n;
}
public String A
{
get { return _a; }
set { this._a = value; }
}
public int N
{
get { return _n; }
set { this._n = value; }
}
public override void displayDetailInfo()
{
Console.WriteLine(A);//To obtain value entered in Main(i.e. f.A)
Console.WriteLine(N);//To obtain value entered in Main(i.e. f.N)
}
}
public class Program
{
public static void Main(String[] args)
{
A v = new A();
A v1 = new B();
B f = new B();
f.A = Console.ReadLine(); //Value to be accessed
f.N = Convert.ToInt32(Console.ReadLine()); //Value to be accessed
v1.displayDetailInfo();
}
}
How can I get the value(f.A and f.N) I entered in Main accessed from the overrided method in class B(i.e. displayDetailInfo()). The code I wrote doesn't obtains any value(i.e. Console.WriteLine(A) gives no value of f.A). So how can I get the value of f.A and f.N from overrided displayDetailInfo()?
Whenever you use new to create a new object, you are creating a new, independent object that has its own state.
Here, you are creating 3 separate objects - v, v1, f.
A v = new Vehicle();
A v1 = new B();
B f = new B();
Changing a property of one of these objects does not affect the properties of the other two objects whatsoever.
Here you change the properties of f, but the properties of v1 is not affected.
f.A = Console.ReadLine();
f.N = Convert.ToInt32(Console.ReadLine());
This is why when you call v1.displayDetailInfo(), it prints null and 0. null and 0 are the default values of string and int respectively. v1's properties have not been set yet, so they hold the default values.
To fix this, just call f.displayDetailInfo() instead.
You can't do this because you v1 is a different instance of B than the one you want to get the values from (f)
Calling f.displayDetailInfo() should give you the result you want
You are setting A of a different object than what you are calling displayDetailInfo on.
I think you meant to do this:
public static void Main(String[] args)
{
A v = new Vehicle();
B v1 = new B();
v1.A = Console.ReadLine(); //Value to be accessed
v1.N = Convert.ToInt32(Console.ReadLine()); //Value to be accessed
v1.displayDetailInfo();
}
It's not possible for v1 to have data you entered in f. The class is just a blueprint, v1 and f are different instances that exist in different parts of the heap.
v1.A is not the same as f.A and v1.N is not the same as f.N
To see the values you entered, you better call:
f.displayDetailInfo()
Also, you're using properties wrong. If you want to use backing fields, the ones with the underscores (_n and _a), you better make them private. And unless you want to have additional logic to the getters or setters you are better off not using backing fields altogether and use auto implemented properties:
public string A { get; set; }
public string N { get; set; }
I want to ask you how to declare a variable that can be used by every method?
I tried making the method's access type public but that didn't let me used its variable across other methods
Moreover, I basically want to accumulate that variable with different values across different methods that's why I am asking this.
NOTE: I want to avoid making any static classes.
EDIT:
For example, I did
public decimal MiscMethod()
{
decimal value1 += 23m;
}
public decimal AutoMethod()
{
decimal value 1 += 34;
}
do you mean somethinge like this ?
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
myClass.Print(); //Output: Hello
myClass.SetVariable();
myClass.Print(); //Output: Test
}
}
class MyClass
{
string MyGlobaleVariable = "Hello"; //my global variable
public void SetVariable()
{
MyGlobaleVariable = "Test";
}
public void Print()
{
Console.WriteLine(MyGlobaleVariable);
}
}
with your example:
decimal value1 = 0;
public decimal MiscMethod()
{
value1 += 23m;
}
public decimal AutoMethod()
{
value1 += 34;
}
Use it like a global variable,but make sure after using it in every method you need to nullify the value,as that will not make the value contradictory to other methods.
decimal value1;
public decimal MiscMethod()
{
value1 += 23m;
//Complete your code using value1
value1 = 0;
}
public decimal AutoMethod()
{
value 1 += 34;
//Complete your code using value1
value1 = 0;
}
I have a .Net library. say with 2 public functions. say one is Summmator which takes 2 arguments and does A+B. Another function simpleMultiplicator which takes 2 arguments: a number to multiplicate and count of times it should be multiplicated. A simpleMultiplicator function when called uses Summmator function. So it is my library. It is compiled into dll for .net4. In my programm I want to extend or modify Summator function so that when I call simpleMultiplicator it would use my modification of original Summator function. Is it possible in .net4, how to do it?
(C#, visual-C++)
It depends on how you design your classes. You state that your library exports two public functions, but they need to be defined on a class either as static or instance method, so you can make use of object-orientated principles like inheritance or polymorphism to achieve what you want.
Here is an example using inheritance:
namespace MyLibrary
{
public class MyMath
{
// Be aware of the virtual keyword which enables overriding the method
public virtual int Summmator(int a, int b)
{
return a + b;
}
public int SimpleMultiplicator(int a, int b)
{
int result = 0;
for (int i = 0; i < b; i++)
{
result = Summmator(result, a);
}
}
}
}
namespace MyProgram
{
using MyLibrary;
public class MyExtendedMath : MyMath
{
public override int Summmator(int a, int b)
{
return a + 2 * b;
}
}
public static class Program
{
public static void Main()
{
MyMath math = new MyExtendedMath();
int result = math.SimpleMultiplicator(2, 3);
Console.WriteLine(result);
}
}
}
Another way is to use polymorphism:
namespace MyLibrary
{
public interface ISummmator
{
int Summmator(int a, int b);
}
public class Summmator : ISummator
{
public int Summmator(int a, int b)
{
return a + b;
}
}
public static class MyMath
{
public static int SimpleMultiplicator(int a, int b, ISummmator summmator)
{
int result = 0;
for (int i = 0; i < b; i++)
{
result = summmator.Summmator(result, a);
}
}
}
}
namespace MyProgram
{
using MyLibrary;
public class MySummmator : ISummmator
{
public int Summmator(int a, int b)
{
return a + 2 * b;
}
}
public static class Program
{
public static void Main()
{
int result = MyMath.SimpleMultiplicator(2, 3, new MySummmator());
Console.WriteLine(result);
}
}
}
Best Regards,
Oliver Hanappi
First, I'd objectify these things. So you have a Summator which provides a sum function, and a SimpleMultiplicator which provides a multiply function. Then, you add the Summator to the SimpleMultiplicator in the default case. For example,
class SimpleMultiplicator
{
public SimpleMultiplicator()
{
this.summator = new Summator();
}
public int Mult(int a, int b)
{
// ...
this.summator.sum(something, somethingelse);
// etc
}
Then you create another constructor where you can override the default Summator:
public SimpleMultiplicator(Summator summator)
{
this.summator = summator;
}
So if you want to change the summation function, you create a new Summator-based class, override its sum() method and pass it on to the SimpleMultiplicator constructur.