Accessing an object created by another class
-
I'm trying to reword the question I asked below in more general terms. From a class
classSecond
how do you check if an objectObj_classFirst
ofclassFirst
has been created? Also, how do you access and modify its value? -
I'm trying to reword the question I asked below in more general terms. From a class
classSecond
how do you check if an objectObj_classFirst
ofclassFirst
has been created? Also, how do you access and modify its value?NietzscheDisciple wrote: From a class classSecond how do you check if an object Obj_classFirst of classFirst has been created? I guess if it has not been created, it will hold it's default value, which should be set to null. The real problem with this question is that it cannot be answered without knowing the relationship between the two classes. NietzscheDisciple wrote: Also, how do you access and modify its value? If there is no interface so that one is a member of the other, or some other mechanism is provided through methods that both can see, the answer is that they can't. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
NietzscheDisciple wrote: From a class classSecond how do you check if an object Obj_classFirst of classFirst has been created? I guess if it has not been created, it will hold it's default value, which should be set to null. The real problem with this question is that it cannot be answered without knowing the relationship between the two classes. NietzscheDisciple wrote: Also, how do you access and modify its value? If there is no interface so that one is a member of the other, or some other mechanism is provided through methods that both can see, the answer is that they can't. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
Christian, thanks for the reply. In my main form's class, I have an object of the DirectSound
SecondaryBuffer
class. It is declared as follows:public SecondaryBuffer SecBuff = null;
and is initialized later in the code as follows:
SecBuff = new SecondaryBuffer(FileName, ApplicationDevice);
The above code belongs in the class "MainForm" that is declared as
public class MainForm : System.Windows.Forms.Form
Now, I have a separate Control,
public class ctlVolume: System.Windows.Forms.UserControl
All I have in this Control is a trackbar, tbVol. I want the volume of
SecBuff
to change when the trackbar value changes. This is accomplished by calling theVolume
method on this object. How do I access the objectSecBuff
which is in the class MainForm? How do I use interfaces to do it? Thanks! -
Christian, thanks for the reply. In my main form's class, I have an object of the DirectSound
SecondaryBuffer
class. It is declared as follows:public SecondaryBuffer SecBuff = null;
and is initialized later in the code as follows:
SecBuff = new SecondaryBuffer(FileName, ApplicationDevice);
The above code belongs in the class "MainForm" that is declared as
public class MainForm : System.Windows.Forms.Form
Now, I have a separate Control,
public class ctlVolume: System.Windows.Forms.UserControl
All I have in this Control is a trackbar, tbVol. I want the volume of
SecBuff
to change when the trackbar value changes. This is accomplished by calling theVolume
method on this object. How do I access the objectSecBuff
which is in the class MainForm? How do I use interfaces to do it? Thanks!Maybe I'm missing something here, but it seems that you're trying to obtain an event from your user control that contains the trackbar. I don't really know why you need the separate UserControl, since you could just place the trackbar on the form, but if you have to do that, you'll need to declare a public event on the UserControl, then create an event handler in your form. In the user control, you would handle the value changed event (whatever that is) from the track bar, and raise the event. The .NET documentation indicates a pretty straightforward design pattern for this. Assuming your event is ValueChanged you typically declare a method: private void OnValueChanged() { if( ValueChanged != null ) ValueChanged(this, EventArgs.Empty); } In the event handler for the trackbar event, call this method. You'll also need to expose a public property that tells you where the progress bar is, so that in your form, when you respond to the event you can read the value and pass it to your SecBuff. All that said, really, it would be much easier just to site the track bar on the form and respond to its value changed event by passing the current track bar position into the SecBuff. Another approach you could take would be to pass the SecBuff object into your user control as a property, then respond to the value changed event on the track bar by setting the corresponding value on the SecBuff object. Tom Clement Apptero, Inc. www.apptero.com articles[^]
-
Maybe I'm missing something here, but it seems that you're trying to obtain an event from your user control that contains the trackbar. I don't really know why you need the separate UserControl, since you could just place the trackbar on the form, but if you have to do that, you'll need to declare a public event on the UserControl, then create an event handler in your form. In the user control, you would handle the value changed event (whatever that is) from the track bar, and raise the event. The .NET documentation indicates a pretty straightforward design pattern for this. Assuming your event is ValueChanged you typically declare a method: private void OnValueChanged() { if( ValueChanged != null ) ValueChanged(this, EventArgs.Empty); } In the event handler for the trackbar event, call this method. You'll also need to expose a public property that tells you where the progress bar is, so that in your form, when you respond to the event you can read the value and pass it to your SecBuff. All that said, really, it would be much easier just to site the track bar on the form and respond to its value changed event by passing the current track bar position into the SecBuff. Another approach you could take would be to pass the SecBuff object into your user control as a property, then respond to the value changed event on the track bar by setting the corresponding value on the SecBuff object. Tom Clement Apptero, Inc. www.apptero.com articles[^]
Tom, thanks for the advice. The volume control is just an example. I actually need to perform some DSP functions on the audio data. I have five controls for five DSP functions. I'm trying to first establish communication between these two classes. I'll try what you suggested and see if I can get it to work. Thanks!
-
Maybe I'm missing something here, but it seems that you're trying to obtain an event from your user control that contains the trackbar. I don't really know why you need the separate UserControl, since you could just place the trackbar on the form, but if you have to do that, you'll need to declare a public event on the UserControl, then create an event handler in your form. In the user control, you would handle the value changed event (whatever that is) from the track bar, and raise the event. The .NET documentation indicates a pretty straightforward design pattern for this. Assuming your event is ValueChanged you typically declare a method: private void OnValueChanged() { if( ValueChanged != null ) ValueChanged(this, EventArgs.Empty); } In the event handler for the trackbar event, call this method. You'll also need to expose a public property that tells you where the progress bar is, so that in your form, when you respond to the event you can read the value and pass it to your SecBuff. All that said, really, it would be much easier just to site the track bar on the form and respond to its value changed event by passing the current track bar position into the SecBuff. Another approach you could take would be to pass the SecBuff object into your user control as a property, then respond to the value changed event on the track bar by setting the corresponding value on the SecBuff object. Tom Clement Apptero, Inc. www.apptero.com articles[^]
Tom, could you please clarify which method goes on the main form and which goes in the Control? Thanks!
-
Tom, could you please clarify which method goes on the main form and which goes in the Control? Thanks!
Hmmmm.... first, you need a plan. Here's the situation as I see it: --- OPTION 1 --- 1. You have a UserControl that sits on your form. 2. You want to be notified when something changes on your UserControl. 3. You want to be able to read a property on the user control that tells you the slider bar position. So, first you declare the event on the UserControl, like this: public event EventHandler SomethingChanged; Then you write the standard private method for raising the event like this: private void OnSomethingChanged() { if( SomethingChanged != null ) SomethingChanged(this, EventArgs.Empty); } Then you call OnSomethingChanged() when something changes, like this: private void tbVol_Changed(object sender, System.EventArgs e) { OnSomethingChanged(); } You also want to expose the current value of the tbVol control as a property, so you probably do something like this: public int TrackValue { get { return tbVol.Value; } } --------- Now, in your form, you want to respond to this event on your user control. So you want to create an event handler for the SomethingChangedEvent. If you're using the IDE, you can go the the properties window, events tab, and double click in the value area for SomethingChanged. That will generate the event handler which you can use to set the value of your SecBuff object. private void ctlVolume_SomethingChanged(object sender, System.EventArgs e) { SecBuff.Volume = ctlVolume.TrackValue; } ---------------------------------------------------- ---- OPTION 2 ----- If that doesn't work for you, you could add the following code to your ctlVolume control: private SecondaryBuffer m_buff = null; public void SetSecondaryBuffer(SecondaryBuffer buff) { m_buff = buff; } You could call this right after: SecBuff = new SecondaryBuffer(FileName, ApplicationDevice); like this: ctlVolumme.SetSecondaryBuffer(SecBuff); Then you could write the event handler for tbVol Changed event like this: private void tbVol_Changed(object sender, System.EventArgs e) { if( m_buff != null ) m_buff.Volume = tbVol.Value; } I hope this gets you started. Truthfully, it's pretty much beginner stuff. Tom Clement Apptero, Inc. www.apptero.com articles[