[2008]how to include class.vb ?
I cant find the answer nor in the google nor in the help file & not even in the faq-s. So How I do this?
How can I make my msgbox in form1.vb output this sampletext declared in class1.vb ?
I added item class1.vb this is how it looks inside.
Code:
Public Class Class1
Dim sampletext As String = sampletext = "flowers"
End Class
in my form1.vb
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(sampletext)
End Sub
End Class
Re: [2008]how to include class.vb ?
sampletext = "flowers" is not allowed outside a method body, however, merging both lines into 'Dim sampletext As String = "flowers"' is, so I suggest that you rewrite it that way.
Re: [2008]how to include class.vb ?
Quote:
Originally Posted by obi1kenobi
sampletext = "flowers" is not allowed outside a method body, however, merging both lines into 'Dim sampletext As String = "flowers"' is, so I suggest that you rewrite it that way.
I replaced:
Dim sampletext As String
sampletext = "flowers" '
with
Dim sampletext As String = sampletext = "flowers"
& yes the error is gone, but this does not answer my question, because in form1 it still says:
MsgBox(sampletext) 'Name 'sampletext' is not declared.
Re: [2008]how to include class.vb ?
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sampletext As String = "flowers"
MsgBox(sampletext)
End Sub
Public Class Class1
Dim sampletext As String = "flowers"
End Class
End Class
try that?
Re: [2008]how to include class.vb ?
LOL, i don't think that's what he wants either. Naming a variable the same and giving it the same value will obviously give the right result but that's not what he wants.
First up, I don't know the answer but if i had to guess then i would say:
1. Declare sampleText as a Public variable rather than simply Dim
2. You will probably have to create an instance of Class1 within Form1 and then it will be easily accessible.
Re: [2008]how to include class.vb ?
Oh, wait, I didn't see that the variable is in a different class.
Try this: (in order to avoid having to make an instance)
vb.net Code:
Class Class1
Public Shared sampletext As String = "flowers"
End Class
Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(Class1.sampletext)
End Sub
End Class
Re: [2008]how to include class.vb ?
Quote:
Originally Posted by obi1kenobi
Oh, wait, I didn't see that the variable is in a different class.Try this: (in order to avoid having to make an instance)
yes that worked, but looks like I should explain why i need this, since I got loads of errors after I tried to modify this.
My code is getting bigger & bigger & its pretty hard to navigate in it so I thought ill split it by putting some functions from Form1 for example Private Sub Button2_Click(....) into other file like class1.vb & then simply use Class1.Button1_Click() under form1 button1 click functon. & theoretically by clicking on button1 it should click on button2. Well let me make a sample:
take function from form1.vb & put it in class.vb & then call that functon from form1.vb at formload
Form1.vb
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'we attempt to call a function from Class1.vb file hire
Class1.sample()
End Sub
End Class
class1.vb
Code:
Public Class Class1
Public Shared sampletext As String = "flowers"
Public Sub sample()
MsgBox(sampletext)
End Sub
End Class
Re: [2008]how to include class.vb ?
Well, IMHO that's not a very good idea, but it's your project, so...
Re: [2008]how to include class.vb ?
Quote:
Originally Posted by obi1kenobi
Well, IMHO that's not a very good idea, but it's your project, so...
Would you mind to explain why this is not good idea? I thought this is what classes are for. Well at least this is the expression i got from watching Lesson 6: Object Oriented Programming Fundamentals
Re: [2008]how to include class.vb ?
Well, the event handler is there to do something, not to call a function in another class, which is declared only to take the code which originally should have been in the event handler to another place. Simply put the code in the Form_Load event. It won't make any difference. Use a class if you need some properties/methods/both to be reusable both in multiple places in this project and in other projects.