I'm writing a program where I have to call a function from another class. I know you can call functions from another class using "public shared function" and this works. But when I do this the "Inherits" statement doesn't work anymore. In the function itselfs I fill parameters with variables from another class. Thats why I use the inherits statements.
I am relatively new to programming and this is my first post.
This is the function. It inherits name, pass and role from another class
Public Class AccountaddModel
Inherits AccountaddViewModel
Public Function addUser()
Dim url As String = "https://myPrivateWebsite" "&name=" (username) "&pass=" (pass) "&role=" (role)
Dim request As WebRequest = WebRequest.Create(url)
Dim response As WebResponse = request.GetResponse()
Dim result As String = New StreamReader(response.GetResponseStream()).ReadToEnd()
End Function
End Class
These are the variables the function needs:
Public Class AccountaddViewModel
Inherits AccountaddView
Public username As String = txtUsernameAdd.Text
Public pass As String = txtPasswordAdd.Password
Public role As String = txtProjectrolAdd.Text
End Class
I try to call the function from the 3rd class here and this does not work
Public Class AccountaddView
Public Sub btnAccountAdd_Click(sender As Object, e As RoutedEventArgs) Handles btnAccountAdd.Click
AccountaddModel.addUser()
Error message: reference to a non-shared member requires an object reference
CodePudding user response:
You have to create an object of that type, then call the method on the object. Look at how you're calling methods in your first code snippet. Here:
Dim request As WebRequest = WebRequest.Create(url)
you are calling the Shared
method Create
, so you call it on the class itself. Everywhere else:
Dim response As WebResponse = request.GetResponse()
Dim result As String = New StreamReader(response.GetResponseStream()).ReadToEnd()
you're calling instance methods so you have to have an instance to call them on, i.e. you have create an object first, then call the method on the object. If you expect to call that instance method addUser
then you need an instance of the AccountaddModel
class to call it on.
CodePudding user response:
I guess you want to make that function static / Shared
Public Class AccountaddModel
Inherits AccountaddViewModel
Public Shared Function addUser()
Dim url As String = "https://myPrivateWebsite" "&name=" (username) "&pass=" (pass) "&role=" (role)
Dim request As WebRequest = WebRequest.Create(url)
Dim response As WebResponse = request.GetResponse()
Dim result As String = New StreamReader(response.GetResponseStream()).ReadToEnd()
End Function
End Class
but then you have the problem where it accesses non-shared members from its base class, so you will want to make those shared too
Public Class AccountaddViewModel
Inherits AccountaddView
Public Shared username As String = txtUsernameAdd.Text
Public Shared pass As String = txtPasswordAdd.Password
Public Shared role As String = txtProjectrolAdd.Text
End Class
and you have the same issue there, where those textboxes aren't shared, and since you can't make them shared, you are at the end of the experiment.
Instead you need an instance of AccountaddModel
, like this
Dim model = New AccountaddModel()
but this is very rudimentary and judging by the context of your code, you won't be making the instance like this, rather in the manner that the other answer does it, so follow that.