PC Review


Reply
Thread Tools Rate Thread

Declare Function for global use then call on change event

 
 
gwoodby@gmail.com
Guest
Posts: n/a
 
      16th Nov 2007
Say i wanted to have a numbersonly function

I would Declare this in a new module, but when i call it

Function MyFunction(NewText as string)
On Error GoTo ErrorHandler
If IsNumeric(Chr(strTempAscii)) Or Chr(strTempAscii) = "-" Or _
Chr(strTempAscii) = "/" Or Chr(strTempAscii) = "\" Then

Else
strTempAscii = ""
TxtDtoAtto2.Text = Left(TxtDtoAtto2.Text, Len(TxtDtoAtto2.Text) -
1)
End If

ErrorHandler:
End Function


When i call it Like


Private Sub TxtDtoAtto2_Change()
MyFunction( TxtDtoAtto2)
End sub
Private Sub TxtDtoAtto2_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
strTempAscii = ""
strTempAscii = KeyAscii
End Sub

it doesnt work, Is there something im not doing?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      16th Nov 2007
Functions are intended to return values. Sum is a function that returns the
total value of all of the numbers. Your function is not returning a value.
You are passing in a value by reference and modifying it that way. In general
functions don't modify the inputs (not always true but generally a good
practice in VB). For example sum does not change any of the numbers that are
passed in. Take a look at this...

Function MyFunction(byval NewText as string) as string
On Error GoTo ErrorHandler
MyFunction = ""
If IsNumeric(Chr(strTempAscii)) Or Chr(strTempAscii) = "-" Or _
Chr(strTempAscii) = "/" Or Chr(strTempAscii) = "\" Then

Else
strTempAscii = ""
MyFunction = Left(NewText , Len(NewText ) -1)
End If

ErrorHandler:
End Function


'your function is still changing strTempAscii which is generally considered
to be
'a side effect. You normally want to avoid side effects in functions.
Private Sub TxtDtoAtto2_Change()
TxtDtoAtto2 = MyFunction( TxtDtoAtto2.text)
End sub
Private Sub TxtDtoAtto2_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
strTempAscii = ""
strTempAscii = KeyAscii
End Sub

--
HTH...

Jim Thomlinson


"(E-Mail Removed)" wrote:

> Say i wanted to have a numbersonly function
>
> I would Declare this in a new module, but when i call it
>
> Function MyFunction(NewText as string)
> On Error GoTo ErrorHandler
> If IsNumeric(Chr(strTempAscii)) Or Chr(strTempAscii) = "-" Or _
> Chr(strTempAscii) = "/" Or Chr(strTempAscii) = "\" Then
>
> Else
> strTempAscii = ""
> TxtDtoAtto2.Text = Left(TxtDtoAtto2.Text, Len(TxtDtoAtto2.Text) -
> 1)
> End If
>
> ErrorHandler:
> End Function
>
>
> When i call it Like
>
>
> Private Sub TxtDtoAtto2_Change()
> MyFunction( TxtDtoAtto2)
> End sub
> Private Sub TxtDtoAtto2_KeyPress(ByVal KeyAscii As
> MSForms.ReturnInteger)
> strTempAscii = ""
> strTempAscii = KeyAscii
> End Sub
>
> it doesnt work, Is there something im not doing?
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
declare a global variable =?Utf-8?B?cmFmaWs=?= Microsoft Access Macros 1 5th Aug 2005 02:22 AM
Call javascript function without using any event call cschang Microsoft ADO .NET 3 1st Feb 2005 03:04 AM
function call from Global.asax.cs Steve Microsoft C# .NET 1 16th Nov 2004 03:32 PM
Changing the value of Session.Timeout triggers a call to Global.asax.Session_End function at end of request Samuel Stanojevic Microsoft ASP .NET 1 15th Sep 2004 04:35 AM
Re: How to declare a 'global' class J.Marsch Microsoft C# .NET 0 17th Mar 2004 06:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:33 PM.