Results 1 to 21 of 21

Thread: Classes?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Wink

    Hi,
    I was just reading some of your posts and I noticed that some of you use classes. Can anyone tell me what they are and whats the advantage of using them? I'm relatively new to VB, but I always like to expand my VB knowledge and I've never used/written a class.

    Thanks
    Tomexx (VB6 Pro)

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    As this has been a quite frequent topic, let me point you here:
    http://forums.vb-world.net/showthrea...threadid=20492
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Guest

    Classes - in a business environment

    Ok we use classes to define Table properties and methods.

    For example we have a class called Customer. This is built with a property for each field in the Table, and a method for add, modify, search, delete, etc.

    Now in the class we define the business rules related to Customers. A good example is all Customer records must have something in the Name field. Therefore if a Customer record is added or modified and nothing is in the Name field an error is generated and the Table is not updated.

    Ok now for the really good stuff. The Customer table is defined once as a class, and can be re-used throughout a project or any number of projects. If a change occurs to the business rules, we only have to make that change in one place, not in multiple places.

    Ok that was a simple example of classes as used in business, they form the basis of object orientated programming.

  4. #4
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    what i think

    i think classes r a way to make ur program communicate with other programs ...

  5. #5
    Guest

    Thumbs down Not quite theman32x

    Sure you could create classes to talk to other programs...but this is not the fundamental purpose of classes, which is to represent objects, and expose their properties and methods.

  6. #6
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I wouldn't say Represent objects, you can make classes for things other than tables and chairs, They're used to combine data and code in one unit, The Simplest useful Example I can Think of is a clsAngle as follows

    Code:
    'clsAngle
    
    Option Explicit
    
    Private Const DegreesPerRadian = 57.2957795130823 ' number  of degrees in a radian =180/Pi
    
    Dim m_Value As Single 'Value of angle in radians
    
    Public Property Let Degrees(new_Value As Single)
    m_Value = new_Value / DegreesPerRadian
    End Property
    
    Public Property Get Degrees() As Single
    Degrees = m_Value * DegreesPerRadian
    End Property
    
    
    Public Property Let Radians(new_Value As Single)
    m_Value = new_Value
    End Property
    
    Public Property Get Radians() As Single
    Radians = m_Value
    End Property
    
    
    Public Property Get Tan_Angle() As Single
    Tan_Angle = Tan(m_Value)
    End Property
    
    Public Property Let Tan_Angle(new_Value As Single)
    m_Value = Atn(new_Value)
    End Property
    
    
    Public Property Get Sin_Angle() As Single
    Sin_Angle = Sin(m_Value)
    End Property
    
    Public Property Let Sin_Angle(new_Value As Single)
    Select Case Abs(new_Value)
    
        Case Is > 1 'if |new_Value| > 1 we cant evaluate the inverse sine, so rais invalid property value error
            Err.Raise 380
            
        Case 1 'if sin x = 1 then x is 90 degrees, but tan x is infinite
            Degrees = 90 * new_Value 'the * is so we get the sign right
    
    
        Case Else 'vb has no inverse sine so we use this identity to set Tan
            Tan_Angle = new_Value / Sqr(1 - (new_Value * new_Value))
    
    End Select
    
    End Property
    
    Public Property Get Cos_Angle() As Single
    Cos_Angle = Cos(m_Value)
    End Property
    
    Public Property Let Cos_Angle(new_Value As Single)
    Select Case new_Value
    
        Case Is > 1, Is < 0 'if 0 > new_Value  or new_Value > 1 we cant evaluate the inverse cosine, so rais invalid property value error
            Err.Raise 380
            
        Case 0 'if cos x = 0 then x is 90 degrees, but tan x is infinite
            Degrees = 90
    
        Case Else 'vb has no inverse cosine so we use this identity to set Tan
            Tan_Angle = Sqr(1 - (new_Value * new_Value)) / new_Value
    
    End Select
    
    End Property
    so now we can

    Dim X as New clsAngle

    and we have all our Trig and conversion Functions and their inverses wrapped up with the variable and we neverhave to worry about wheather out angle is in degrees or radians again


    It's quite long winded to code but whonce you've done it you can compile it and add it as a reference (probably alog with some other maths stuff too)

    That's essentially what class modules are for, it's not usually worth writing one this small but this one could save a lot of debugging is you're doing some triganmometry


  7. #7
    Guest
    Class modules are also called Object Modules. Some examples of diffeent classes are CButton, CString, CWnd etc.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Don't Mix up Objects with Classes, they sound pretty much like the sam thing but there's one big difference: Objects are instances of Classes, while classes themselves are the structure.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    Hello
    This is how it was explained to me.

    A Class is a Blueprint of an Object.
    An Object is the Instance of a Class

    So the Blueprint of a House is the class (House Class)
    The House is the Instance of the Blueprint (House Class)
    Many Houses may be built from one Blueprint (House Class)
    The House may be any color or have a different type of
    roof. These would be properties of the BluePrint (House Class)

    Bottom Line, CODE REUSE. If you need to build 20 houses,
    you don't need 20 code modules. Just one!

    It is confusing and easy at the same time.

    Happy Coding
    Will


    [Edited by William on 07-03-2000 at 04:13 PM]

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That was my point! But well you made a much better explanation of it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Guest

    kedaman interesting interpretation

    If you create a class called Angle, then automatically the class Angle becomes an object with properties and methods. As some one stated above, an object is just an interation of a class. True OOP demands that a class must be re-usable and must be able to generate multiple instances. Therefore even an application like Excel becomes an object for vb with properties and methods.

    I still have problems understanding sub-classing and inheritance.

  12. #12
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    Hey
    forget about inheritance for now. You have to understand
    that just about everything is an object. The Class is a
    code Object to the Project, Just as a textbox is an Object to a form. But once you make an Instance of the Class,
    that Instance becomes an Object created from that Class.
    The Class is still an Code Object to the Project. You just used it make an Object.

    Take the textbox again. you can make as many of them as you
    want. Microsoft didn't make 3000 textboxes in an OS in the
    hope that you would never ask for more than 3000 of them.
    They wrote the code once in a class, gave it Properties
    and methods to make if functional then create every textbox
    from this one peice of code.

    There is a good tutorial in the MSDN for this,
    it evens talks about inheritance.(is it really inheritance?)

    Hope this helps
    William

  13. #13
    Guest

    Thumbs up William your absolutely right on this

    All programs, apps, whatever in the windows environment are objects. However version 7 of vb is expected to be fully OOP therefore am trying to get as much info on sub-classing and inheritance as possible, to see what advantages effects this will have on existing apps, and design requirements for new apps. Classes should have shown even the most ardent anit-OOP programmers the advantages of this approach, therefore sub-classing and inheritance are likely to hold even better benefits. Of course MS might just rethink what this stuff should mean, and set their own view on it.

  14. #14
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    More

    I have been told (by people on the VC++ development team
    "ver 7") that inheritance is the number one cause of bugs.
    And that alot of C++ programmers wish it never existed.
    VB version of inheritance is easy to understand(yea-right)
    but I don't think that I have ever used it.

    Point? It might evolve into something else?

    William

    subNote:
    not all Objects are instances of a Class

    [Edited by William on 07-03-2000 at 09:17 PM]

  15. #15
    Guest

    Thumbs up Megatron & Kedaman

    Guru status....well down!!!!!!!!Are these the first Aussie and Finnish guys to get this rating?????????

    William,

    VB doesn't exactly have inheritance as l understand it, but coming in version 7. So what if these C++ clowns claim that this is a major area of bugs, that's a part of development fixing bugs.(Remember most C++ guys think vb is a joke as a language and generally point to it not having this facility). Associates who use inheritance claim that while it may take a while to get the hang of it, for major development projects it slashes development time and costs. Therefore bring it on!!!!!!!

    Currently working on a project with over a hundred bas routines, 50 or so classes, and a couple of guys attacking COMs at a rapid rate. If inheritance could reduce the complex nature of this project would be of a major advantage.

  16. #16
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    From what I've seen of inheritence in C++ VB is actually better, you just have to write a bit more code and it's a tiny bit slower. An example of inheritence, based on the angles class, could be a clsIsoscelesTriangle

    Quick Recap in case you don't remember what one is

    An Isosceles Triangle is a Triangle with 2 sides the same length, this means you can define one simply with the length of the 2 equal sides and the angle between them

    So if we wanted to make a clsIsoscelesTriangle our clsAngle would be quite useful.

    So we buy VB7 and use Inherietence

    Code:
    'clsIsoscelesTriangle
    
    Option Explicit
    
    Inherits clsAngle  'I think this is the VB7 syntax to inherit a class
    
    'Now we have clsAngle inherited, all it's public methods are now public in our class, and we can access all its private methods and variables
    
    
    Dim m_SideLength As Single
    
    Public Property Get SideLength() As Single 
    
        SideLength = m_SideLength
    
    End Property
    
    Public Property Let SideLength (new_SideLength As Single)
    
    If new_SideLength < 0 Then
    
        Err.Raise 380
    
    Else
    
        m_SideLength = new_SideLength
    
    End If
    
    End Property
    
    Public Property Get Base() As Single 'We Use the Third Side as The Base
    
        Base = 2 * m_SideLength * Cos(m_Value / 2)
    End Property
    
    Public Property Get Height() As Single 'The Perpendicular Distance from the Vertex To the Base
    
        Height= m_SideLength * Cos(m_Value / 2)
    
    End Property
    
    
    Public Property Get Area() as Single
    
        Area = 0.5 * Base * Height
    
    End Property
    What's Usefull is that you don't have to Retype all the Code For the Sin, Cos and Tan Properties, ALthough you could just Dim m_Angle as New clsAngle and simply Have things Like
    Code:
    Public Property Get Radians As Single 
    
        Radians = m_Angle.Radians
    
    End Property
    The Great thing About inheritence is that you can get at the Private Variables and Functions (There was only one variable here but I used it anyway) and as you can see it made the code a lot shorter than it would normally be to have all the stuff in it.

    But, unless you have the code of the base class to hand you are unlikley to know how it works and what the private properties and methods are called. I'm a Grat believer in the Idea that once a Class is Written it shouldn't be looked at again, this way If there are bugs in a class you can just re-write it, and keep the interface the same, If you rewrite a base class for an Inherited class then unless you are very carefull to keep everything exactly the same (In which case what's the point of a rewrite) Then all he inherited classes won't work.

    Plu In this case clsIsoscelesTriangle also has the Sin_Angle, Cos_Angle and Tan_Angle Properties, which are pretty Meaningless in the Context of an Isosceles Triangle, Keep Inheriting And We Have all sorts of Properties that We Don't Need. (I think you can Get Rid of them by Overloading them as Private but I'm Not sure.

    The other Disadvantage (And probably the main reason it's so prone to bugs) Is that If I used m_Value for my SideLength Variable, not knoing that the m_Value variable was already used (I could use the Radians property instead in the Base and Height) Then Bang my project explodes (It would in fact be overloaded, meaning the same Name is used for 2 different variables, which is a one way ticket to bug city.



    Obviously in Major Projects Where the Code is all in house and pre planned then inheritence could be great, and I don't doubt that there will be lots of situations where it will come in very usefull, but I Think that in VB as it is you can Easily get around it with a little extra interface code and good use of Freind Variables.

    Congrats to Kedman and Megatron BTW, Definatley well deserved.

  17. #17
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    All Right guys

    Guru status....well down!!!!!!!!Are these the first Aussie and Finnish guys to get this rating?????????

    What is This?

    I don't get it, I'am no Guru, and I am from Sac, CA. and I
    will never live long enough to learn everything I want to
    know. Unless someone creates a Brain DownLoad Port (from
    a Class?) that won't hurt to much.

    William

  18. #18
    Guest

    Wink Actually William...............

    ........the guru stuff was aimed at Megatron and kedaman.


  19. #19
    Member
    Join Date
    Apr 1999
    Location
    Reno, NV
    Posts
    57
    Cool
    I thought I might need nap?

    William

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Jethros interesting interpretation

    Well Jethro I hope you know what youre doing... 100 modules and 50 classes in a project? Well I would like to know what happens if you have all those modules and classes loaded at the same time in vb, my resources would go down to 0 with 30 classes.

    And for anyone that didn't know, batman is back and so is the security of this site.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  21. #21
    Guest

    Cool kedaman...oh yeah it's resource hungry

    Ok the app runs on the back of a four pentium chip server with gigs of ram. The client side, which l am almost finished, only has a couple of bas, three classes, and approx 10 or so windows.

    What the server side does is extract multiple record layouts from a number of different database types, (SQL, informix, Universe,etc etc etc), dependant on client requirements. This is then added to Country specific information which then generates a specific Access file for a Sales guy to download to his/her portable.

    Way heavy stuff.............

    Without classes we would be totally rooted, trying to tie in all the db layouts throughout the project.

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