Results 1 to 14 of 14

Thread: VB.NET: Create and Apply Basic Classes

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Create and Apply Basic Classes

    Does anyone know how to create classes and apply it without occur any error correctly...

    I find that most of my coding is inside the forms and i am not using any classes at the monent...

    thus i find it quite hard if i need to change the coding......i would search all the forms just for the word that i need to change..

    Therefore i am eager to change my style of coding and uses classes more instead..

    This is quite basic..

    Can anyone sent me some useful links, advise or tutorials about that..

    Thanks

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi

    To create a class > add class on your project. I would advise you to read the book : Visual Basic .Net Class Design Handbook from Wrock.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    I got some questions about the uses of classes...


    1)Is it a must to create both the property and methods in the

    classes....if i does not uses property often....

    can i just declare it inside the form and put all methods

    separately to the classes...

    2)Can i say that class is somehow act like the function procedure

    as it can return the values except that it could be shared to all

    forms if i put it in classes....

    3) Lastly, how to declare one or multiple classes that i need to

    the various forms... I am not sure where the Inherits

    should be place... is it

    coding:
    Code:
        Public Class Sample
        Inherits System.Windows.Forms.Form 
        Inherits Class1, Class2
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:41 AM.

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    1)Is it a must to create both the property and methods in the

    classes....if i does not uses property often....

    can i just declare it inside the form and put all methods

    separately to the classes...
    Property is just a setter/getter function. You could and could not create your own property. Sample to this functions are the ones in Java. Something that looks like this
    Code:
    function getSomething() varialbe_type
    return variable
    end function
    
    sub setSomething(something)
    me.variable=something
    end sub
    2)Can i say that class is somehow act like the function procedure

    as it can return the values except that it could be shared to all

    forms if i put it in classes....
    Class is not a function but a structure with a twist. A structure that has function and variables inside it. What's good about classes is the oop feature that can extend it's capability to another class. The one we know about inheritance.

    3) Lastly, how to declare one or multiple classes that i need to

    the various forms... I am not sure where the Inherits

    should be place... is it

    coding:
    VB Code:
    1. Public Class Sample
    2.     Inherits System.Windows.Forms.Form
    3.     Inherits Class1, Class2

    Thanks
    There is no multiple inheritance in .NET aside from C++. Dunno about any languages if they support multiple inheritance. You can do multiple inheritance by creating an interface and implements it on your class. Something like this
    Code:
    public class Sample
    inherits Class1
    implements Interface1
    
    . . . 
    end class
    
    public Class1
    . . .
    enc class
    
    public interface Interface1
    Sub DoThis()
    Function DoThis2() as variable_type
    enc interface
    From there, your class Sample have the feature from Class1 and the functionality of your interface.

    Dunno if this helps.

  5. #5

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    I heard from other forums that the current version of .NET does
    not support polymorphism. So I can only have one inherits
    statement at the class level.

    However, other objects within the class can inherit other classes...

  6. #6
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    I heard from other forums that the current version of .NET does not support polymorphism. So I can only have one inherits
    statement at the class level.
    It does support morphing like the "Aliens" morphing like Mendhak. Seriously, it does support morphing
    Code:
    function f() as something
    . . .
    end function
    
    function f(id as integer) as something
    . . .
    end function
    That's morphing. Overloading and Overriding is a feature in morphing (The Mighty Morphing Power Ranger?, ). You mean multiple inheritance?

    However, other objects within the class can inherit other classes......
    Correct.

  7. #7

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    One more question....

    is there a need to create a sub New() in all the classes that i create...

    I ask it because i came cross a book where it will create a sub New() everytime it add a new class to it...

    what is the purpose of that and what would happen if i left that out...

  8. #8
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    You don't need to create a Sub New() if it's not need. I mean, if the need of your class is something like this dim something as new toytoysclass(somethingelse), you wouldn't bother to create a sub new() but a sub new(with_parameter). But I personally create all my classes with the sub new() (note: no argument).

    BTW, it's the constructor, it gets fired if your class is instantiated. Everytime you create an instance of the class, the constructor gets called. Now with the morphing thing, you can also create another sub new with different signature as the default, that is sub new()

  9. #9

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    you means it is not so important....just create a blank sub New() inside every classes...

    Just to ask for your opinion..

    in what situation will you consider create a class best with

    1) function procedure and property
    2) sub procedure and property
    3) Just property
    4) All of the above together

  10. #10
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    you means it is not so important....just create a blank sub New() inside every classes...
    There are some usage of blank constructor. I don't know much so can't help producing much information. One usage I have with this blank constructor is, say for example, I have a GeneralCollection class that inherits the Payment class. Now, in my frmgeneralcollection (inherits from Form) there should be a reflection of the GeneralCollection class. Now I have to create an instance by default because I can add something to it. It's a scope of the frmgeneralcollection. In the events, I can add something, say... GeneralCollection.AddNatureOfCollections(...) that preserve the current instance of the GeneralCollection class. That is why in the scope of the frmgeneralcollection class I instantiated the GeneralCollection class without any arguments. Dim gncol As New GeneralCollection() (note: the blank argument), I can then recreate the class by gncol=New GeneralCollection() after the saving/updating/deleting it from the DataBase.

    in what situation will you consider create a class best with

    1) function procedure and property
    2) sub procedure and property
    3) Just property
    4) All of the above together
    My preference is the Function/Sub setting and getting member variables of the class. I found property a lot of work. Just my 0.02 but whatever floats your boat. Whatever you like.

  11. #11

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    ok...i guess i learnt something from that..

    thanks brown monkey for the explanation..

    I will continue to search for other useful links related to that in the forum...

    If you guys found any useful links.....remember to tell me


  12. #12

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    Re: VB.NET: Create and Apply Basic Classes

    Recently, i borrow a book from the author of Bradley/Millspaugh...

    I try the code on one of the chapters where it uses the class..

    I come across one HandOn program where i can't understand why the class of Payroll is not declared in the frmSummary form and it can still be use...

    but in the frmPayRoll form, it must declared a new instance of object first before it can be use...

    Can someone spare some time and look into this two forms...

    I can't figure out the reason...

    Thanks
    Attached Files Attached Files

  13. #13
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Re: VB.NET: Create and Apply Basic Classes

    lblCount.Text = Payroll.NumberProcessed.ToString()
    lblOvertime.Text = Payroll.OvertimeHours.ToString()
    lblTotalPay.Text = FormatCurrency(Payroll.TotalPay)

    Are you talking about this one? It's because those properties are Shared. A member of the class, not the object itself. Notice the Console.WriteLine()? WriteLine() is a shared member method of Console class. You need no intantiation. Instantiation is for object and we are dealing about the class member.

  14. #14
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: VB.NET: Create and Apply Basic Classes

    Hi
    The class can be used by frmSummary without creating an instance of the class because the following properties are all declared Shared.
    VB Code:
    1. Shared ReadOnly Property NumberProcessed() As Decimal
    2. Shared ReadOnly Property TotalPay() As Decimal
    3. Shared ReadOnly Property OvertimeHours() As Decimal
    Acording to MSDN The Shared keyword indicates that one or more declared programming elements are shared. Shared elements are not associated with a specific instance of a class or structure. You can access them by qualifying them either with the class or structure name, or with the variable name of a specific instance of the class or structure.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

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