Class wth Thread

  • Thread starter Thread starter nondos
  • Start date Start date
N

nondos

Hello

I have a problem with thread i have a class defntion i've create but every
time i call it from thread it's null any idea why?

code:

dim test as class1

test.a=1
test.b=2
test.c =3
Dim t As Thread

t = New Thread(AddressOf Me.DoIt)

t.Start()

sub doit()

dim a = test.a ' i get null

end sub

Public Class Class1

Public a As Integer

Public b As Integer

Public c As Integer

End Class
 
nondos said:
Hello

I have a problem with thread i have a class defntion i've create but
every time i call it from thread it's null any idea why?


You did not assign an object to variable test. Use "new" to do that:

code:

dim test as class1


test = new class1

test.a=1
test.b=2
test.c =3
Dim t As Thread

t = New Thread(AddressOf Me.DoIt)

t.Start()

sub doit()

dim a = test.a ' i get null

end sub

Public Class Class1

Public a As Integer

Public b As Integer

Public c As Integer

End Class


Armin
 
nondos said:
Hello

I have a problem with thread i have a class defntion i've create but every
time i call it from thread it's null any idea why?

code:

dim test as class1

test.a=1
test.b=2
test.c =3
Dim t As Thread

t = New Thread(AddressOf Me.DoIt)

t.Start()

sub doit()

dim a = test.a ' i get null

end sub
Public Shared Class Class1

Public Shared a As Integer

Public Shared b As Integer

Public Shared c As Integer

End Class


Then you can use Class1.a = test.a

Use Goggle or go to the VS Help and look-up the *Shared* keyword and find
out what it does.

Either you have to instantiate class1 with Dim class1 = new class1 at some
point or you use the Shared keyword.

It should have not compiled or blew up IMHO. I don't know how you got away
with it. You must have Option Strict turned off for the project.
 
Hi mr Arnold,

Can you tell us why option strict should be off. At least (I am sure of)
both Armin and I are very interested in that.

Cor
 
Cor Ligthert said:
Hi mr Arnold,

Can you tell us why option strict should be off. At least (I am sure of)
both Armin and I are very interested in that.

I don't know ask the OP. The OP is the one that has the problem. :)
 
I don't know ask the OP. The OP is the one that has the problem. :)

Lol :-)

Well i guess the OP has a special sort of compiler
 
Cor said:
Hi mr Arnold,

Can you tell us why option strict should be off. At least (I am sure of)
both Armin and I are very interested in that.

Cor

I must have been off, otherwise there would have been a compiling error.
That doesn't mean that it should be off.
 
Cor,


With option strict ON

the provided code would raise 2 errors

option strict requires all variabels to have an as clause
name Test is not declared ( as it is method scoped )

so it wouldn`t even compile

if run with option strict off it would also not compile because of some
coding errors



However this could run ( although not my prog style ) in this wayImports System.Threading
Public Class Form1
Private test As New Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
test.a = 1
test.b = 2
test.c = 3
Dim t As Thread

t = New Thread(AddressOf Me.doit)

t.Start()
End Sub
Sub doit()

Dim a As Integer = test.a ' now you get 1 :-)

End Sub

End Class
Public Class Class1
Public a As Integer
Public b As Integer
Public c As Integer
End Class
 
Michel,

I have had big fights with Armin about Option Strict, I found it very easy
to set it off, luckily it was as far as I remember me the only real
discussion with Armin. Now I think it is almost possible to cast to the
right object. (Even if that is casting over a cast).

Armin is one of the oldest members of this newsgroup, however it is normal
that he is doing nothing for almost a year and than begins again very
intensive.

Cor
 
Back
Top