how to pass values between methods from external class

时间:2016-02-12 21:49:30

标签: c#

I've been looking for an answer but couldnt find one that matched my problem and/or couldnt understand those which don't match my example. Here is an example of my problem:

In Form.cs,

rich

In my program.cs I have:

namespace Test
public partial class Form
`{
    public void firstmethod()
     {
     enregistre enr = new enregistre();
     enr.date = dateTimePicker1.Value.ToShortDateString();
     }

    public void secondmethod()
    {
    enregistre enr = new enregistre();
    textBox1.Text = enr.date;
    }

My problem is : inside the same method I can call enr.date as often as I want and it keeps the values set or returned, but when I call it from the next method, it returns " 01/01/0001 " ... How can I keep values between methods while stored on another class? I understand I could have just use " public DateTime date" right under "public partial class {" but I wrote more than 1000 lines now using this other class.. any way to fix this without rewriting everything? If one could write a solution using this example code that would be very useful. Thank you.

4 个答案:

答案 0 :(得分:1)

Your first method and the second one use different instances of the same class. For being able to share information between them you should use the same instance.

// boot.ts
import 'rxjs/Observable'
import 'rxjs/add/operator/debounceTime'
...

答案 1 :(得分:1)

Declare it static so that it retains its values between instances.

switch (someFlagYouSet) {
  case 'a':
    require.ensure('./resources/A.json', function() {...})
    break;
  case 'b':
    require.ensure('./resources/B.json', function() {...})
    break;
  ...
}

Now when you call it you use enregistre.date = DateTimePicker1.Value.ToShortDateString();

When you use instance variables, (no static descriptor) the variables are reinitialized for every instance, meaning every time you call namespace Test public static class enregistre { publist static DateTime date {get;set;} } . When you want to keep values between invocations, use static.

答案 2 :(得分:0)

When you call it from the second method, you're getting a different value back because you're calling public partial class Form { public enregistre Enr {get; private set;} public void firstmethod() { Enr = new enregistre(); Enr.date = dateTimePicker1.Value.ToShortDateString(); } public void secondmethod() { textBox1.Text = Enr .date; } } on a different date object. If you change the enregistre property to be static, then there will be only one date which applies to the entire date class, not to each instance of that class. An alternative solution is to create the enregistre in the caller of the two methods in your enregistre, then pass it into each of those methods as a parameter.

答案 3 :(得分:0)

Can you not modify your enregistre class as follows to preserve the date value:

${document}.ready(function() {

$("h1").hide();

$("td").click(function() {
    $(this).css("background-color","red");

    alert("you clicked!");
});

Since in your method, you are creating a new instance of enregistre(), between methods within a class, it will be difficult to preserve the property value unless you make some changes to code as you already indicated.