|
A delegate is the only clean way to have communication between classes. Better that, a designated event, than passing reference to forms around and exposing text properties on labels.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
In this scenario, if Calc has only one instance thoughout and if that gets created in the form itself, then
a) would databinding the label text be enough?
b) still stick to delegates?
जय हिंद
|
|
|
|
|
Who cares... The entire point is that the non-form class shouldn't have ANY knowledge of the form using it. It should not have any reason at all to modify UI controls. What if the class needs to be used by another non-form class somewhere down thte road??
The correct way to do this would be to have the Calc class expose methods, events, or delegates, that outside consumer would subscribe, or otherwise wire-up, to receive notifications that the Calc class outputs. But, these would not be manadatory subscriptions to use the class. It should be left up to the caller to determine if it needs this notification information, not the Calc class.
|
|
|
|
|
Thanks for the response Christian.
You don't perhaps have any good links on the web referring to an example?
If not ... i do know how Google works ... just wondering.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
There are some good articles on delegates right here on code project, so search this site first, then google if you need to. I don't know off hand, but I know there's been a lot written about them.
You CAN just keep an instance of your form in your other class, but it's really ugly, delegates are the neat and clean way of doing what you want.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
|
Ever heard of Properties in C#?
जय हिंद
|
|
|
|
|
|
Sorry. I thought this is all that needs to be done.
जय हिंद
|
|
|
|
|
Class
class Class1
{
public void ChangeTextctl ( string text , System.Windows.Forms.Control ctl )
{
ctl.Text = text;
}
}
Form
Class1 cls = new Class1();
cls.ChangeTextctl("Test", button1);
Non related to the Above
P.S : if you want to change the control text from another form
then set the control modifiers as Internal . and call it from the instant
I know nothing , I know nothing ...
|
|
|
|
|
I think he wants to do it in the other direction. To do it in the direction you suggest, makes no sense at all.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Just a quick question:
When creating a new instance of the class by calling the constructor, wouldn't you re-initialize all of the controls again? InitializeComponent();
What if you don't want that to happen?
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
is the Calc class instanciated by a winForm class? If so, raise an event in your Calc instance (with a custom event args instance to pass the text), and subscribe to it in the winForm instance, and update the Label from the handler.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
Thanks for the response.
Is there any way you could give an code example, because that is more or less what is happening.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
This is a quickie example that should help to get you started. For more details on events, search this site, google... and have a look at my article Events Made Simple[^].
public partial class WinForm : Form
{
private Calc calc;
public WinForm()
{
InitializeComponent();
calc = new Calc();
calc.CalcResult += new EventHandler<CalcResultEventArgs>(calc_CalcResult);
calc.Add(3.14159265f);
}
void calc_CalcResult(object sender, CalcResultEventArgs e)
{
label1.Text = e.ResultAsString;
}
}
public class Calc
{
public event EventHandler<CalcResultEventArgs> CalcResult;
public float Value
{
get;
private set;
}
public void Add(float number)
{
Value += number;
OnCalcResult(new CalcResultEventArgs(Value.ToString()));
}
protected virtual void OnCalcResult(CalcResultEventArgs e)
{
EventHandler<CalcResultEventArgs> eh = CalcResult;
if (eh != null)
eh(this, e);
}
}
public class CalcResultEventArgs : EventArgs
{
public CalcResultEventArgs(string resultAsString)
{
ResultAsString = resultAsString;
}
public string ResultAsString
{
get;
private set;
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Excellent!! Thank you very much
Passing Data between Windows Forms[^] was very useful as well.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Just a quick point on this, setting a label on a form from a seperate class is probably not a particularly good idea. By doing that you've tightly coupled the class and form together which is bad for reuse. At the extreme you might as well put the calculation stuff in the form itself, as potentially the calc class can no longer operate without the form.
As Christian suggests, by exposing an event on your calculation class your form can get updated, but your class could be used elsewhere without that form.
Regards,
Rob Philpott.
|
|
|
|
|
Thanks for your comment Rob.
I was only trying to simplify my scenario as much as I can, the real application does nothing of this sort. I was only trying to explain in layman's terms.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Hello,
Can anyone help me to solve below problem at my pc.
Method not found:'Int32 Microsoft.VisualStudio.Shell.Interop.IVsRunningDocumentTable2.QueryCloseRunningDocument(System.String,Int32 ByRef)'.
Thanks.
Jem_Jumper.
|
|
|
|
|
1. Have you added Microsoft.VisualStudio.Shell.Interop.8.0.dll as reference?
2. Second parameter is out parameter.
जय हिंद
|
|
|
|
|
Hi
can any one tell me how to install an application developed using .NET in Linux System
Thanks in advance
|
|
|
|
|
|
.net is not directly supported on Linux by Microsoft.
Take a look at the Mono project[^]. They are attempting to implement the CLR and large parts of the framework on Linux. Currently they have a lot of good support for .net 2.0, but some of the 3.5 stuff is still catching up. It depends on what your app uses.
Also see - http://en.wikipedia.org/wiki/Mono_(software)[^]
Simon
|
|
|
|
|
We have a software developed using DotNet 2.0. The problem we are facing is there are certain areas where customers require changes according to their business needs. These change in turn makes us to change the code and cater their requirements. Lets take an example of employee entrance at their office:
Client 1:
Activity 1. Employees swipe their card and enter.
Activity 2. Have tea/coffee.
Activity 3. Start work.
Client 2:
Activity 1. Employee enter their names and time-in manually.
Activity 2. Start Work.
Client 3:
Activity 1. Employees do not perform any check-in.
Activity 2. Have Breakfast.
Activity 3. Start Work.
My question is that if there is any approach where we can handle these changing requirements. The activities involved in these requirements like the example above do change for every customer. These activities can either increase/decrease in number or change entirely.
Thanks.
|
|
|
|
|