Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Read the code comments ...........................

What I have tried:

//external class
public class DCPS
{
    public string pin1_Sign = "+";
    public string pin2_Sign = "-";
    public double pin1_Volts = 3;
    public double pin2_Volts = 0;

    public void Pin1Contact(string pinXsign, double pinXvolts)
    {
        pinXsign = pin1_Sign; pinXvolts = pin1_Volts;
    }
}



public UserControl1()
{
    InitializeComponent();
    //this is NOT working
    //led.pin1_Sign is NOT changing to "+" and
    //led.pin1_Volts is NOT changing to 3
  dcps.Pin1Contact(led.pin1_Sign,led.pin1_Volts); //?????
    led.pin1_Sign = dcps.pin1_Sign; //(test) this is working fine
}
DCPowerSupply1.DCPS dcps = new DCPS();
Posted
Updated 30-Mar-19 0:50am
v4

No. It won't.
That's because all parameters passed in C# unless otherwise specified are passed by value, not by reference.

To explain, think about this:
C#
public void TimesTwo(int x)
   {
   x = x + x;
   }
...
   int value = 666;
   TimesTwo(value);
   Console.WriteLine(value);
If value was passed by reference (i.e. the x inside the method was the same variable as value outside the method) then the console would print "1332".

But ... what if I called it like this:
C#
TimesTwo(666);
Console.WriteLine(666);
What would you expect the console to show? "1332" or "666"?
Fairly obviously, we want the console to print "666" because otherwise constants are no longer constant, and the rest of our code is going to become impossible to follow!

So C# passes by value, not reference: a copy of the variable content is made and is passed to the method. Because TimesTwo is changing the copy, it doesn't affect the outside world, and constants remain the same.

The same thing happens with strings: a copy of the string is passed to the method, and your code "modifies" that copy, and the copy is discarded when the method exits.
Except ... with strings it's even worse, because strings are immutable: once you create a string in C#, it can't be changed at all - every time you try, you create a new string, and they both then exist side by side.

If you want to pass a value back, you can .. but you have to specify that when you both create and use the method:
C#
public void Pin1Contact(out string pinXsign, out double pinXvolts)
    {
        pinXsign = pin1_Sign; pinXvolts = pin1_Volts;
    }

C#
dcps.Pin1Contact(out led.pin1_Sign, out led.pin1_Volts);

This tells the system to return parameters by reference, rather than by value.
YOu can also use the ref keyword instead, and the system will let you access the current value in the method as well:
C#
public void Pin1Contact(ref string pinXsign, ref double pinXvolts)
    {
        pinXsign += pin1_Sign; pinXvolts += pin1_Volts;
    }

C#
dcps.Pin1Contact(ref led.pin1_Sign, ref led.pin1_Volts);
 
Share this answer
 
Comments
_Q12_ 30-Mar-19 6:55am    
WAAAAAW !!! Thank you so much !
I was aware the existence of out and ref, but i never Knew when to use them!
Now, because of you, i know. VERY GOOD EXPLANATION !!!!!!!!
Additional question: and if i want specifically to pass by value what keyword i should use?
OriginalGriff 30-Mar-19 7:02am    
There isn't one: it's the default, so if you don't specify out or ref, it's by value.
_Q12_ 30-Mar-19 7:07am    
Thank you,
I understand now.
So good explanation ! Ohoa! 10+++++++++
Live long and prosper !
OriginalGriff 30-Mar-19 7:11am    
You're welcome!
You cannot changing the values of led in that way. You are merely changing the values of the local parameter variables. Your class design and usage is wrong. The Pin1Contact method should be used to change the values of the object's variables, not some external ones. It should be like:
C#
public class DCPS
{
    public string pin1_Sign = "+";
    public string pin2_Sign = "-";
    public double pin1_Volts = 3;
    public double pin2_Volts = 0;

    public void Pin1Contact(string pinXsign, double pinXvolts)
    {
        pin1_Sign = pinXsign;
        pin1_Volts = pinXvolts
    }
}

led.Pin1Contact("-", 5.0);
 
Share this answer
 
Comments
_Q12_ 30-Mar-19 7:50am    
Thank you. You are also right!
Richard MacCutchan 30-Mar-19 8:55am    
Happy to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900