Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
What's best practice for preventing delegates from causing memory leaks? For event handlers I would just call -= for each registered handler. I have my delegate assigned with a '=' (equals sign). Shall I just assign NULL at dispose time?
public delegate int MyDelegate(string message);
public class MyManager MyDelegate
{
public MyDelegate ManagerDelegate;
..
public class Transaction
{
public int DoSomething(string message)
{
//do something
}
public void init()
{
var manager = new MyManager();
manager.ManagerDelegate = this.DoSomething("abc");
An instance delegate references a class instance (in its Target property), so if you store that delegate somewhere, whether an event or a simple delegate typed property, that will reference your original class. In this respect, it doesn't matter if it's an event or not. So if you want to have your original class garbage collected while the other class stays alive, you have to clean up. Remove your event handlers and also any other delegates. If the other class dies first, your original class can die, too, so it depends on your specific case.
Update: proof: http://pastebin.com/XcTz76dY
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Suppose that we have two classes, A and B, and two events OnPress and OnPressed. When I invoke OnPress both classes are a must-have, so I have an EventArgs that have a ref to both classes, but in OnPressed context only makes sense to have the ref to the class B.
My doubt is, is better to have separated event args as:
public class PressEventArgs : EventArgs
{
public A a;
public B b;
}
public class PressedEventArgs : EventArgs
{
public B b;
}
And pass the right context to each event OR pass the PressEventArgs to both events only leaving the A class ref null?
If different events have different arguments, then you should have different classes representing them.
Using the same class makes no sense. It violates several principles of software development.
If you had to pass a single number, you would never contemplate if passing a 2d coordinate and leaving the y-value zero would be a good fit. It isn't. You pass what you need to pass, not more, not less.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am developing an ASP/c# webform where I am using JQuery as well. I came into a scenario where I need to call. C# function from JQuery. In order to that, I found that function in c# has to be a static method (web method).
The problem is that I need to access all variables, arrays, etc which I used to populat some data and these are not stated c variables. Also, from the web method I need to re-use some the functions which are not static. I ended up gradually just changing all methods and variables to static.
I would like to know if the approach I am taking is correct, and whether there is any pitfall of using static variables/methods and what in simple words makes a difference between static/none-static.
Static variables can be called directly by using class names such as
public class IhaveStatic
{
public static string Hello = "Hello I am A";
}
When you use static this means this will be in memory for life time of your process.
now consider another class as
public class IhaveNoStatic
{
public string Hello = "Hello I am B"
}
public class C
{
Console.WriteLine(IhaveStatic.Hello); // Correct
IhaveNoStatic obj = new IhaveNoStatic();
Console.WriteLine(obj); // Correct
Console.WriteLine(IhaveNoStatic.Hello); // Compile time error
}
as you can see that you need to create object of that class "IhaveNoStatic" to access non-static variable. So, this will be in memory until there is an instance of that class exist.
So, basically it's on your requirement but it is good to use less static variable in your programs.
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 7 years ago.
Improve this question
I want to recall a method from the called method, can i use action for that, or any other solution.
I see some ambiguity in your question use case..
Let us take a simple use case: You want to call some one and they in turn call you back. Is there any use to it and when do you want to stop this and go ahead to get some work done?!
For example following code like what you are expecting will go in infinite recursion and result in StackOverflowException
class MyClass
{
public void TargetMethod(Action callback)
{
Console.WriteLine("Inside TargetMethod");
callback(); //This call the SourceMethod() and which inturn call TargetMethod() again in infinite recursion.
}
public void SourceMethod()
{
Console.WriteLine("Inside SourceMethod");
TargetMethod(SourceMethod);
}
}
//Calling code
MyClass objMyClass = new MyClass();
objMyClass.SourceMethod();
Instead you can use Callback mechanism, so that once you done within the target method, notify another handler method that in turn can do some stuff like logging or updating UI etc., case specific stuff.
class MyClass
{
public void TargetMethod(Action callback)
{
Console.WriteLine("Inside TargetMethod");
callback(); //This calls/notify the handler to do some stuff.
}
public void SourceMethod()
{
Console.WriteLine("Inside SourceMethod");
TargetMethod(CallbackHandler); //Notice here we are calling to a handler which can do some stuff for us.
}
public void CallbackHandler()
{
Console.WriteLine("Inside CallbackHandler");
}
}
This code is just for quick demonstration purpose and you can enhance design further in your implementation.
Hope this provide you some heads-up on why you need to revisit your design..
You should not do this in normal situations, your design is probably wrong!
You can use the CallerInfoAttribute to get the caller and use reflection to invoke it.
But... that is a lot of work and I think you are not very skilled. Luckily, there is another way!
You can add a parameter of type MethodInfo. And let's say A() calls B(MethodInfo). Then A needs to get its reflected self and pass it into B. Then in B you can use MethodInfo.Invoke to invoke the method.
Well... it turns out that the alternative requires reflection too... So if you are not familiar with reflection, learn more about it first!
If you want to know more about MethodInfo, go to here: https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo(v=vs.110).aspx
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I've been having to use c# lately, which I don't have much experience in. a conundrum I keep finding myself in is, when building a class, having state be dependent on the state initialized before it
class foo{
public bar_ {get;}
public dum_ {get;}
public foo (){
bar_ = BuildBar();
dum_ = BuildDum(bar_);
}
}
its a bit redundant for BuildDum to carry a parameter if it's just going to use something already accessable from a member. on the other hand I like explicitly pointing out dependencies a function relies on
I guess I am asking: what is the best way to handle the situation?
Both ways are fine. The current version of BuildDum could be made static, in which case it's perfectly fine for the method to not access any member variables, because it cannot do it anyway:
private static Dum BuildDum(Bar b) {
...
}
If you make BuildDum that accesses bar_ directly, you should also make it access _dum, i.e. it should be a non-static void:
private void BuildDum() {
...
_dum = ...
}
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
I have a problem with a example of my book. From what i have read non static methods cant be used without instance a object of the class. So is this ok ?
public partial class TempAgencyForm : Form
{
public TempAgencyForm()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
...
setVisibility(false);
}
private void setVisibility(bool visibilityValue)
{
...
}
}
Yes, it is fine. One non-static method can call another non-static method.
The call:
setVisibility(false);
can also be written:
this.setVisibility(false);
but the this qualifier is redundant.
However, if you had tried to call a non-static method without instance qualification from inside a static member, that would have been a problem (compile-time error).
I assume you are talking about calling setVisibility(false);. Yes it is fine, neither it or the method calling it are static.
This will all happen within an instance of TempAgencyForm
Yes this is okay, because it's called from within another member.
You're correct, since setVisibility() is not static, it always has to be called in the context of some object of the parent class (TempAgencyForm in this example).
However, btnCalculate_Click() is another member of TempAgencyForm, as such you're able to access the current/local object using the this keyword (this.setVisibility()), which is optional if there's no disambiguity.
static or non static doesnt matter here, you are calling a member function declared within the same object. so short answer is "fine"
how you call TempAgencyForm members may be what you are referring to
in this case (as you have defined) instantiations is required
TempAgencyForm taf = new TempAgencyForm()
taf.setVisibility(false);
however if you have your class definition itself as static, i.e.
public static partial class TempAgencyForm
then,
TempAgencyForm.setVisibility(false);
is suffice (without instantiating the object) as the object is already loaded on stack at the time of application start