accessing data from classes in vb.net

shortcircuit

New member
Joined
Dec 20, 2004
Messages
3
Programming Experience
10+
Hi all,

i'm stuck with a class problem, i want to create a class that, say for example gets the system time, and can be displayed in label or textbox but can be accessed many times....

ok i'll start with a pseudo example.

VB.NET
pseudo code....

public function test(variable)
do something....anything
eg get system date,time or anything
'**********************
label1.text=result 'This works here, but not in a class?
'**********************
end function

private sub form load
timer1.interval=500
timer1.enabled=true
end sub

private sub timer1_tick()
dim x as integer
test(x)
end sub

'This works fine but i want to put this in class so, it can be accessed many times.

class myclass
'maybe withevents of inherits timer1, not sure
public function test(variable)
do something....anything

'****problem here******

label1.text=result 'this will not work but to put in its place?

'*******************
end function
private sub timer1_tick()
dim x as integer
test(x)
end sub
end class

i cant figure out how to bring the data out of the class, for example if the data in displayed in a label, and use it to have say tens or thousands of labels that display this data, how do i extract it from the class.

:)

thanx Andy
 
think i have cracked it


Private Class Test

Inherits Timer

Public var As String

Public Sub enable()

Me.Interval = 500

Me.Enabled = True

End Sub

Private Sub disable()

Me.Enabled = False

End Sub

Public Function test()

test = Now.Second

End Function

Private Sub Test_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Tick

test()

End Sub

End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim var As New test

var.enable()

Label1.Text = var.var

End Sub

End
Class

 
Slow down!

It would be much easier to just create the function that returns the value inside a module. You can do this the following way:
In the explorer-window of VS.Net > Right click on your project name > Add Module > Type in a module name ie Module 1.

A new module will appear before you. Put your function inside the module and remember to declare it as friend... ie (Pseudocode):


Friend Function Ticker (byVal sec as Integer) as Integer
bla bla bla...
return sec
end Function


Now you can access the function from anywhere in your project, ie:

label1.Text = Ticker(2)

VOILA!

This also counts for "Global Variables"

:)
 
Last edited:
Back
Top