Setting time in .net 2.0

D

david

I am trying to set the time programmatically on my PPC device and as
expected, the time zone information is not refreshed. However, when I
run the code that forced the refresh in 1.1, it is giving a null
reference error. Has anyone run into this and know how to resolve it?

The previous thread is at:
http://groups.google.com/groups?hl=...man+datetime&hl=en&lr=&ie=UTF-8&start=20&sa=N

My code looks like this:

Dim localZone As TimeZone = TimeZone.CurrentTimeZone

Dim fi As FieldInfo =
localZone.GetType().GetField("currentTimeZone", BindingFlags.NonPublic
Or BindingFlags.Static Or BindingFlags.Instance)

fi.SetValue(Nothing, Nothing)

Thanks for the help,
David
 
P

Peter Foot [MVP]

Chances are that as your code modifies a private field in the TimeZone, the
name of the field has changed and this is why your GetField is returning
null.

Peter
 
D

dariwe

Thanks. I tried to check for the correct field name but the length of
the fields collection using the code below returns 0.

Dim iLength As Integer = localZone.GetType().GetFields().Length

Is there a new way to reset the timezone information in .net CF 2.0?

Thanks
 
P

Peter Foot [MVP]

Another possible approach would be to call the static ResetTimeZone()
method, it's internal so again you'll need to use reflection, it's new in
v2.0 and will handle resetting the private field so that the time zone is
fetched again.

Peter
 
D

dariwe

Thanks Peter. I can't seem to locate the method in the timezone class
for the compact framework. Below is the code that I'm currently using,
but it returns Nothing. I also haven't found any documentation on the
ResetTimeZone. Do you know where I could find that?

Dim tz As TimeZone = TimeZone.CurrentTimeZone
Dim myMethodInfo As MethodInfo = tz.GetType().GetMethod("ResetTimeZone")

Thanks,
David
 
P

Peter Foot [MVP]

Do you have the Beta 2 release? I'm using the July CTP at the moment. It's
possible that there are differences.

Peter
 
D

dariwe

I'm using the RC released on 9/12/05.

I have looked through MSDN and searched through Google and cannot find a
reference to the ResetTimeZone() method. Do you know where there is
documentation on this?

Here is what I'm trying. Does this code look correct? I haven't done
much with reflection so I want to make sure that it's not just something
that I'm doing wrong with my syntax.

Dim mi As MethodInfo = tz.GetType().GetMethod("ResetTimeZone")
Dim obj As Object = Activator.CreateInstance(tz.GetType())
Dim param As Object() = New Object()
mi.Invoke(obj, param)

And when I run it, there is an exception on the second line that says:

System.MissingMethodException was unhandled
Message="MissingMethodException"
StackTrace:
at System.Activator.CreateInstance()
at UpdateTime.frmUpdateTime.Button1_Click()
at System.Windows.Forms.Control.OnClick()
at System.Windows.Forms.Button.OnClick()
at System.Windows.Forms.ButtonBase.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at UpdateTime.Form1.Main()

Thanks,
david
 

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