Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Trouble with invoking Classes


Reply
 
Thread Tools Display Modes
  #1  
Old 09-25-2004, 05:31 AM
jamesharte jamesharte is offline
Regular
 
Join Date: Jul 2004
Location: Yorkshire England
Posts: 83
Default Trouble with invoking Classes


I am still struggling with Classes.

In the past (Access Basic) I put some functions in a Module that would verify entries in text boxes on a Form.

I now understand that to comply with good OOP practice these validations should be preserved as a Class.

So I now have a class as follows

Code:
Public Class Validations1

     Public Shared Function  CheckLength(ByVal strString As String, _
     ByVal Length As Integer) As Boolean

          If Len(strString) > Length Then
               MsgBox("Too many characters")
               Return False
               Exit Function
          End If

          Return True

     End Function
End Class
This is called from any form in my project as follows.

Code:
Private Sub TxbHQPhone_Validating(ByVal sender As Object, _
     ByVal e As System.ComponentModel.CancelEventArgs) Handles TxbHQPhone.Validating

          If Validations1.CheckLength(TxbHQPhone.Text, 11) = False Then e.Cancel = True

     End Sub
The code works, but is this the correct way of doing this type of operation?

It’s the “Public Shared Function” bit that I am mainly concerned about

I know that I could set the “MaxLength” of the text box to what ever I wanted. This is just an example.
__________________
Jim
Reply With Quote
  #2  
Old 09-25-2004, 07:25 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

I would just do public. I dont think it needs to be shared. Shared would mean you wouldnt require an instance of that class. So you wouldnt actually instantiate it. I may be wrong. But i dont see the point of creating a class and then not instantiating it. Isnt that the point of classes? It may have some merit with some things though.Maybe there is less overhead doing it shared. Not sure.

Last edited by AFterlife; 09-25-2004 at 07:37 AM.
Reply With Quote
  #3  
Old 09-25-2004, 07:49 AM
jamesharte jamesharte is offline
Regular
 
Join Date: Jul 2004
Location: Yorkshire England
Posts: 83
Default

Thanks, Afterlife,

The example I gave was just one of about four validation functions in the Class “Validations”. I had all of them set as a “Public Shared Function”

After your post, I took out the “Shared” part on all of them.

This produced a syntax error of

Quote:
Reference to a non-shared member requires an object reference
at each location where the function was called.

I think you are correct. Leaving it as Shared means that you don’t have to instantiate it every time you need to call it – Its less code at least.
__________________
Jim

Last edited by jamesharte; 09-25-2004 at 07:57 AM. Reason: My post and Afterlife’s edit crossed in the post
Reply With Quote
  #4  
Old 09-25-2004, 08:06 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Quote:
Originally Posted by jamesharte
Thanks, Afterlife,

The example I gave was just one of about four validation functions in the Class “Validations”. I had all of them set as a “Public Shared Function”

After your post, I took out the “Shared” part on all of them.

This produced a syntax error of


at each location where the function was called.

I think you are correct. Leaving it as Shared means that you don’t have to instantiate it every time you need to call it – Its less code at least.
If its not shared. You have to create an instance.

Code:
dim myClass as new Validations1

That created an instance of your class.

Then to use it:

Code:
myClass.CheckLength(TxbHQPhone.Text, 11)

Sorry. I should of offered up the instancing way.
Reply With Quote
  #5  
Old 09-25-2004, 08:09 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

Quote:
Originally Posted by AFterlife
If its not shared. You have to create an instance.

Code:
dim myClass as new Validations1

That created an instance of your class.

Then to use it:

Code:
myClass.CheckLength(TxbHQPhone.Text, 11)

Sorry. I should of offered up the instancing way.

If its just a bunch of functions. Your probably better off creating a .dll
Then just adding a reference to your .dll
Reply With Quote
  #6  
Old 09-25-2004, 08:39 AM
jamesharte jamesharte is offline
Regular
 
Join Date: Jul 2004
Location: Yorkshire England
Posts: 83
Default

Don’t apologise AFterlife.

It was the
Quote:
“Dim myClass As New Validations1”
bit that I could not figure out.

The “shared” idea came from a textbook I have and from Paul Nettle – see “Enum and string ??” in the forum.

Thanks for the gentle nudge in the right direction I now know how to do this both ways!

I will investigate the idea of a .dll

Thanks again.
__________________
Jim
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->