DataBinding to Custom Class breaks after New instance: how to get around this?

D

DraguVaso

Hi,

I'm having the following situation:

- A class clsFournisseur with public property's which raise a
MyPropertyChanged-event in the Set-method for each Property.
Public Event NomChanged As EventHandler
Public Property Nom() As String
Get
Return m_strNom
End Get
Set(ByVal Value As String)
m_strNom = Value
RaiseEvent NomChanged(Me, New EventArgs())
End Set
End Property
- A Form, with an instance of my clsFournisseur: "Private WithEvents
MyEntreprise As New clsFournisseur".
- Proprty's of this Myentreprise are bound to controls on the Form:
"Me.txtNom.DataBindings.Add("Text", MyEntreprise, "Nom")"

When I change the value in my txtNom.Text, the MyEntreprise.Nom-property is
changed. When I change the MyEntreprise.Nom, my txtNom.Text is changed.

So far so good. BUT:
When in my Form I want to use another clsFournisseur, there isn't anymore
databinding. I do this like this:
- MyEntreprise = New clsFournisseur(intEntrepriseID)
or
- MyEntreprise = New clsFournisseur()

I guess this is normal behaviour because, even when it still has the same
name, it isn't the same class anymore. But I still want to have this
databinding! And a weird thing: the txtNom.Databindings.Count = 1, so the
TextBox thinks there is still the databinding...

Does anybody know how I should do this? Am I doing anything wrong? Should I
Clear all the DataBindings after echt new instance of a class and put them
back?

Any help would be really appreciated.

Thanks a lot in advance,

Pieter
PS: I'm using the CTP from august of Visual Studio .NET 2005
 
C

Cor Ligthert [MVP]

Pieter,

You are using forever for an "object" the name "class".

You bind to an object. If you have renewed that, than the binding is gone.

See it as this
dim a as object 'normally you use its type name by instance ThisClass
a = new ThisClass
a = new ThisClass

Now there are in fact two objects the first a and the second a. The first
one will be released by the GC as soon as that starts doing its job because
it has not any reference anymore.

I thought that this was the base of your question.

I hope this helps,

Cor
 
D

DraguVaso

Thansk Cor, it's indeed the confirmation of what I thougt.
But isn't there any solution for the DataBinding? Is the only solution I
have to Clear all the DataBindings and than reset them back afterwarths?

Thansk anyways,

Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top