Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Could someone please post a small example of a working class?

 

I am trying to do one at the moment, but i really dont know enough about them.

 

Thanks.

Posted

This is a simple class that holds data, and has one function to show you use. The function just returns the name of the object (its a document) in uppercase. There are LOADS of excellent reasons to use classes, but just think of each class as a new box.

You stick stuff in it (the private variables normally) and then ask it for data back. You can tell the box to do something to the data, like return in upper case, or run functions based on the data.

Its also good for passing it data in one format (ie a string) and it can convert to another, or using properties, you can allow an integer to be added to the class, but only if its greater then zero.

 

If your just getting into how classes work, then keep away from inheritance, overriding, overloading and scope of things until you know how (and where) to use them.

 

Another thing to note, is that everything is really a class.

A form is a class (it inherits all its functions), a module is a class used once (singleton), controls are instances of classes.

 

Friend Class clsDocument

   'Unique ID
   Private pUniqueID As Integer
   'Name of document
   Private pName As String
   'Owner of this document
   Private pOwner As Integer

   Friend Sub New()
       'Set the defaults
       pUniqueID = -1
       pName = ""
       pOwner = -1
   End Sub

   Friend Function funcGetNameUpper() as String

     return pName.ToUpper()

   End Function

End Class

Posted

This class is still missing a lot of features that should be present though....

 

Users of a class should be able to set or get the values of its members. In this case (they are private) and the constructor sets them to empty strings and -1.

 

Anyway, I really think you should go for MSDN, they have lots of samples because Classes is a major subject to understanding Object Oriented Programming and you should learn them the right way.

 

But just for demo purposes:

 

To use this class :

'declare a new instance of the class
Dim SampleObject as New clsDocument()
'Display the name of the document in a message box (which would show empty because the member pname is empty!!)
MessageBox.Show(SampleObject.funcGetNameUpper())

 

Hope this helps,

Dream as if you'll live forever, live as if you'll die today
Posted

Brilliant.

 

Thank you.

 

Oh by the way, i like the James Dean quote - although there seems to be some confusion btw whether its 'die today' or 'die tomorrow'. I always thought it was 'die tomorrow' but a google search brings up mixed results. What a lovely car he wrote off when he did die as well. Porsche 550 Spider i believe.

 

Anyway, thanks again for your help :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...