-1

I'm using Visual Studio 2013. My issue is that I can't get any value of my form1 from any class.

This is my code

Public MyHour as DateTime 
Public SomeValue as string
Public MyClass as SomeClass

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MyHour = datetime.now()
    Somevalue = "asjdasd"
    MyClass = new Someclass()
End Sub

Here is the button1 click event code in form1:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Myclass.mysub()
End sub

When I call a sub on my class I can't access to anything of my form1:

public sub mysub()
    Dim datetovalidate as datetime= form1.actualhour
    Dim stringtovalidate as string = form1.somevalue
    messagebox.show(datetovalidate.tostring)
    messagebox.show(stringtovalidate)
end sub

From my class:

  • Datetovalidate shows me: 01/01/0001 00:00:00
  • Stringtovalidate shows me : ""

Even If I try to access to a property of a control in my form1 from my class nothing happens, and there is no exception being fired and no errors show me up for this situation. What can I do?

Jose David
  • 23
  • 4
  • Where does the `form1` reference in `mysub()` come from? What if you wanted to have more than one instance of `Form1`; how would the code know which instance it was using? – Joel Coehoorn May 03 '18 at 16:47
  • You should do some reading on default instances of forms in VB.NET. It seems like you're trying to use the default instance to get data when it's not actually the default instance that you're looking at on screen. – jmcilhinney May 03 '18 at 16:52
  • Another one that bites the dust. Search for [VB.NET Default Form Instance](https://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c) – Steve May 03 '18 at 16:52
  • Is the same, I can name the form1 as MyForm.. I'm using the Visual Studio UI, myform.datetovalidate is still 01/01/0001 00:00:00 – Jose David May 03 '18 at 17:18

1 Answers1

0

I recommend you pass the parameters you need into the subroutine:

Public Sub MySub(ByVal myHour as DateTime, ByVal someValue as String)

    messagebox.show(myHour.tostring)
    messagebox.show(someValue)
End Sub

Then, you'll call it from Form1

Myclass.MySub(MyHour, SomeValue)

You could instead pass the whole Form1 in there, but that's less recommended in case you want to call the method from a different form, or from something that isn't a form at all.

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40