Confused by classes (Nightmare!)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C CORDON

    Confused by classes (Nightmare!)

    I am verry confused about classes. I understand that classes can encapsulate
    properties, methods, events (don't know hoy to add events to a class), etc.

    I am confused with this: if you can encapsulate all this in a class, how do
    you decide or when is it necessary to Dim x as new MyClass or when to use
    Imports MyClass. Eather way you have access to all that is inside it.

    If I make a class calld User and I want it to handle everything related to
    the user: LoginUser, IsLogged, Store the user info. and make that info last
    through all the time the app is running how do I get this functionality.

    This are my main questions.

    If there is a good and understanable document about this please point me to
    it.

    TIA!


  • Cor Ligthert

    #2
    Re: Confused by classes (Nightmare!)


    "C CORDON"
    [color=blue]
    >I am verry confused about classes. I understand that classes can
    >encapsulate properties, methods, events (don't know hoy to add events to a
    >class), etc.
    >[/color]
    Public/Friend Class MyClassA
    Friend/Public Event myevent(byval mes as string)
    Private sub/function whatever
    raise myevent("Hello" )
    End sub/function
    [color=blue]
    > I am confused with this: if you can encapsulate all this in a class, how
    > do you decide or when is it necessary to Dim x as new MyClass or when to
    > use Imports MyClass. Eather way you have access to all that is inside it.[/color]

    These two you have not much to do with each other.

    Dim x as new MyClassa (myclass is reserverd a reserved word) is for a class
    that looks like this beneath (very ugly written in this as all samples not
    nice however written as short as possible)

    Public Class MyClassa
    Public A as integer
    End Class

    Than you should first create an object from it what is
    dim x as new myClassa
    and use it as
    x.a = 1
    You can make from this as much objects as you want and they are deleted, as
    soon as they go out of scoop or/and that there are no references anymore
    to/from it, by the Garbage Collector

    While you can use a so called shared class that looks like this
    Public Class MyClassa
    Public shared a as integer
    End class

    This you can use direct as
    MyClassa.x
    And this will not be deleted by the Garbage Collector however destroyed with
    the program at the end and therefore would be used with care. You would not
    make objects from it, this way of using is named "shared class" in VBNet

    You can import the last as
    import yourprojectname .myclassa
    And than you can write
    a=1
    And become probably everywhere in conflict when you would use "a" :-)
    [color=blue]
    > If I make a class calld User and I want it to handle everything related to
    > the user: LoginUser, IsLogged, Store the user info. and make that info
    > last through all the time the app is running how do I get this
    > functionality.
    >
    > This are my main questions.
    >[/color]
    I hope that I answered your first question with the "shared class".

    I hope this helps something?

    Cor


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Confused by classes (Nightmare!)

      "C CORDON" <tcordonb@hotma il.com> schrieb:[color=blue]
      > events (don't know hoy to add events to a class)[/color]

      Visual Basic Language Concepts -- Events and Delegates
      <URL:http://msdn.microsoft. com/library/en-us/vbcn7/html/vaconEventsDele gatesInheritanc e.asp>


      ..NET Framework Developer's Guide -- Handling and Raising Events
      <http://msdn.microsoft. com/library/en-us/cpguide/html/cpconevents.asp >
      [color=blue]
      > I am confused with this: if you can encapsulate all this in a class, how
      > do you decide or when is it necessary to Dim x as new MyClass or when to
      > use Imports MyClass. Eather way you have access to all that is inside it.[/color]

      By importing a class, you will only get unqualified access to /shared/
      members of a class, but not to its instance members.
      [color=blue]
      > If I make a class calld User and I want it to handle everything related to
      > the user: LoginUser, IsLogged, Store the user info. and make that info
      > last through all the time the app is running how do I get this
      > functionality.[/color]

      \\\
      Public Module Program
      Private m_User As New User()

      Public Sub Main()
      m_User = New User()
      ...
      Application.Run (New MainForm())
      End Sub

      Public ReadOnly Property User() As User
      Get
      Return m_User
      End Get
      End Property
      End Module
      ///

      Access to the user: 'Program.User'.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • C CORDON

        #4
        Re: Confused by classes (Nightmare!)

        Thnak you verry much for pointing me in the right direction.

        Have a little doubt about when shoul it be used buy ill look up some more
        information on the web.

        Thanks again.


        "C CORDON" <tcordonb@hotma il.com> wrote in message
        news:ezXvLhACFH A.2804@TK2MSFTN GP15.phx.gbl...[color=blue]
        >I am verry confused about classes. I understand that classes can
        >encapsulate properties, methods, events (don't know hoy to add events to a
        >class), etc.
        >
        > I am confused with this: if you can encapsulate all this in a class, how
        > do you decide or when is it necessary to Dim x as new MyClass or when to
        > use Imports MyClass. Eather way you have access to all that is inside it.
        >
        > If I make a class calld User and I want it to handle everything related to
        > the user: LoginUser, IsLogged, Store the user info. and make that info
        > last through all the time the app is running how do I get this
        > functionality.
        >
        > This are my main questions.
        >
        > If there is a good and understanable document about this please point me
        > to it.
        >
        > TIA!
        >
        >[/color]


        Comment

        Working...