strange behavior , has someone an explanation for this ??

G

Guest

i have 3 forms

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.Show(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)

MsgBox(Form2.test)


End Sub
End Class


why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

regards

Michel
 
T

tomb

M. Posseth said:
i have 3 forms

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As New Form3
frm.Show(Me)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)

MsgBox(Form2.test)


End Sub
End Class


why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

regards

Michel
Maybe because you are loading Form1 as Form1, but you are instantiating
a new Form2 as frm as a private variable in Form1.

T
 
G

Guest

Nope....

just tried it this way , and it behaves exactly the same ,

Someone else willing to shine his light on this ??


Public Class Form1
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim formB As New Form2
formB.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class

Public Class Form2
Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim formC As New Form3
formC.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class


Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)


MsgBox(Form2.test)

End Sub
End Class
 
G

Guest

Aaarghh :-(


Do i feel stupid ...... Ofcourse now i see what you meant Tom

changed this in form1 and form3

Public Class Form1

Private _test As String
Friend Property test() As String
Get
Return _test
End Get
Set(ByVal value As String)
_test = value
End Set
End Property
Friend formB As Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
formB = New Form2
formB.Show(Me)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
_test = "hallo"
End Sub
End Class

Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)


MsgBox(Form1.formB.test)

End Sub
End Class


and now it works as expected .....

i wil sit in a dark corner for a while and shame ;-|
 
C

Cor Ligthert [MVP]

I would thought that it is because Feyenoord has won the both major cups
from Holland and was the winer of the final from the playoffs from the
regular competition and was one of the 16 best teams in the Eurocompetition.

But that is another team, so you cannot make a fool of yourself by doing
something stupid as the management from Ajax did like sacking the trainer.

Have you somewhere
private form1 as form1

for sure you don't have that for form2 in my idea.

I find it strange it runs.

Just my thought,

Cor
 
C

Claes Bergefall

M. Posseth said:
Public Class Form3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
MsgBox(Form2.test)
End Sub
End Class


why gives pressing the button in form3 the value of form1 but not of form2
??

i wonder what i am missing

As tomb said in his post, the problem is likely that it creates a new
instance of Form2 while it manages to find the existing instance of Form1.
Pass an instance of Form1 and Form2 to Form3 instead. That way you can be
sure about what object you're really using.

First time I see someone using this feature (accessing forms by name instead
of using a variable). I wasn't to thrilled when I saw that MS had included
that in VB 2005. As you just showed it can lead to really weird behaviour.
VB.NET is a perfectly valid OO language, so why not use it as one instead of
trying to go back to old VB6 behaviour?

/claes
 
C

Cor Ligthert [MVP]

Sorry, still able to be manager from Ajax.

However did you try it already with the "owner" property.

Cor
 
G

Guest

Hello Claes

I have and will never run into this problem myself ,,, although i am a
VB6 progger from origin i also never used and will never use code like i
showed in the example .

Someone showed me this problem and i found it interesting enough to
investigate and threw it in the group as i could not explain this behavior to
him myself

ofcourse when ligthning strook and it was clear to me i felt pretty stupid
for not seeing it in forehand ( before the post )

however i am with Cors thought l ,,, why the $%$%% does this even compile
???
( vb makes it to easy to write S#it )


regards

Michel Posseth
 
G

Guest

Hello M. Posset,

Let's see:

MsgBox(Form1.test)
MsgBox(Form2.test)

Where are Form1 and Form2 variables instantiated?
I think you're using VB2005 so you're accesing the default instance of the forms.
Why it works for Form1 and not for Form2?
Because when your application starts, it shows a default instance of Form1; when you invoke Form2.test, the default instance of Form2 is created, but not shown, so the Load event doesn't fire.
Try this (in Form3):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Form1.test)
Form2.Show 'Fires Form2.Load event.
MsgBox(Form2.test)
End Sub

This way you can see that you're accesing the default instance of Form2, not the instance you were seeing in the screen.
Have always in mind that the default instance of a form is different from any instance that you create.

Regards.


"M. Posseth" <[email protected]> escribió en el mensaje |
| i have 3 forms
|
| Public Class Form1
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim frm As New Form2
| frm.Show(Me)
| End Sub
| Private _test As String
| Friend Property test() As String
| Get
| Return _test
| End Get
| Set(ByVal value As String)
| _test = value
| End Set
| End Property
| Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Load
| _test = "hallo"
| End Sub
| End Class
|
| Public Class Form2
| Private _test As String
| Friend Property test() As String
| Get
| Return _test
| End Get
| Set(ByVal value As String)
| _test = value
| End Set
| End Property
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim frm As New Form3
| frm.Show(Me)
| End Sub
| Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Load
| _test = "hallo"
| End Sub
| End Class
|
| Public Class Form3
|
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| MsgBox(Form1.test)
|
| MsgBox(Form2.test)
|
|
| End Sub
| End Class
|
|
| why gives pressing the button in form3 the value of form1 but not of form2
| ??
|
| i wonder what i am missing
|
| regards
|
| Michel
|
 
C

Claes Bergefall

M. Posseth said:
Hello Claes

I have and will never run into this problem myself ,,, although i am a
VB6 progger from origin i also never used and will never use code like i
showed in the example .

Good to hear :)
Neither will I (or anyone on my team)

Someone showed me this problem and i found it interesting enough to
investigate and threw it in the group as i could not explain this behavior
to
him myself

ofcourse when ligthning strook and it was clear to me i felt pretty stupid
for not seeing it in forehand ( before the post )

however i am with Cors thought l ,,, why the $%$%% does this even compile
???
( vb makes it to easy to write S#it )

Yes, it was interesting. Couldn't understand how it could compile myself
before I remembered seeing something about this feature in the release notes
for VS 2005. Personally I think MS went a step to far in their desire to
please the VB6 programmers.

/claes

/claes
 

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