5 Replies - 1018 Views - Last Post: 27 December 2012 - 08:31 AM Rate Topic: -----

#1 miesman81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-December 12

timer 'is not declared. It may be inaccessible due to its protecti

Posted 27 December 2012 - 05:17 AM

Hi,

I'm working with an embedded vb editor in Proficy Workflow (tool of GE).
Basically it's the same as MS VB editor, (which version i don't know).

I'm getting the error, when starting my timer which I declared earlier.
I posted my question just to make sure my code is ok,

I've put the following code:

Public class Timer1
		
	public shared sub main
		Dim aTimer as New System.Timers.Timer()
	AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        ' Set the Interval to 5 seconds.
        aTimer.Interval = 5000
        aTimer.Enabled = True
	end sub
	' todo
	Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Messagebox.show("Hello World!")
    End Sub


end class



Starting the timer:

 
Private Sub but_start_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
aTimer1.start()
End Sub



Where did I go wrong?

Thanks!!!

Is This A Good Question/Topic? 0
  • +

Replies To: timer 'is not declared. It may be inaccessible due to its protecti

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,467
  • Joined: 07-April 08

Re: timer 'is not declared. It may be inaccessible due to its protecti

Posted 27 December 2012 - 07:51 AM

you declare the timer in sub main. It is only available within that sub and nowhere else. To make it accessible within other methods you would need to declare it at the class level. The scope of a variable is very important as it defines where a variable can and cannot be accessed.
Was This Post Helpful? 0
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: timer 'is not declared. It may be inaccessible due to its protecti

Posted 27 December 2012 - 08:15 AM

Does your main method ever execute? Put a breakpoint there, and see if you ever enter it. It should start timer, and show messagebox after 5 seconds, because you start the timer in line 8. On the other hand, as rgfirefly24 mentioned, the button click can't access aTimer (you have aTimer, and then you want to access aTimer1 ???).
Was This Post Helpful? 0
  • +
  • -

#4 miesman81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-December 12

Re: timer 'is not declared. It may be inaccessible due to its protecti

Posted 27 December 2012 - 08:17 AM

I cannot even compile it, due to the error that it's giving.

Putting the timer in the class gives me the following error:

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Was This Post Helpful? 0
  • +
  • -

#5 miesman81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-December 12

Re: timer 'is not declared. It may be inaccessible due to its protecti

Posted 27 December 2012 - 08:24 AM

sorry you are right, I made a typo

aTimer1.start()



should be

aTimer.start()



sorry you are right, I made a typo

aTimer1.start()



should be

aTimer.start()


Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: timer 'is not declared. It may be inaccessible due to its protecti

Posted 27 December 2012 - 08:31 AM

Now all you need to do is declare aTimer outside main, and instantiate it inside the main method. It will be then available to button click handling method. And get rid of shared methods.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1