Results 1 to 10 of 10

Thread: [2008]how to include class.vb ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    [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
    Last edited by goldenix; Apr 6th, 2008 at 01:51 PM.

    M.V.B. 2008 Express Edition

  2. #2
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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.

    M.V.B. 2008 Express Edition

  4. #4
    Lively Member
    Join Date
    Apr 2008
    Posts
    82

    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?

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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:
    1. Class Class1
    2. Public Shared sampletext As String = "flowers"
    3. End Class
    4.  
    5. Class Form1
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         MsgBox(Class1.sampletext)
    8.     End Sub
    9. End Class
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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

    M.V.B. 2008 Express Edition

  8. #8
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008]how to include class.vb ?

    Well, IMHO that's not a very good idea, but it's your project, so...
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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

    M.V.B. 2008 Express Edition

  10. #10
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width