Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Help with ThisElement in a Subroutine or Function (Read 5076 times)
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Help with ThisElement in a Subroutine or Function
Aug 16th, 2004 at 5:33pm
Print Post Print Post  
I was trying to improve upon my existing code by creating a subroutine to replace code I used in every telephone number element and thought I could make use of the new command ThisElement released in version 1.0.4.

The code below was what I was using to properly format and control the data for a telephone number.
// Formats Telephone Number (not Subroutine)

var vHPhone as String    
var vNewHPhone As String 
var vhphonecount
var vhpnentered

     Vhpnentered = @num(HomePhone)
     // Writeln(Vhpnentered)
     
     vhphonecount = @num(@len(vhpnentered))
     //writeln(vhphonecount)

If vhphonecount = "10"


{
     @color(HomePhone,0,7)
     vHPhone = @Num(HomePhone)
     vNewHPhone = "(" + @Left(vHPhone, 3) + ") " + @Mid(vHPhone, 4, 3) + "-" + @Right(vHPhone, 4)
     HomePhone = vNewHPhone 
}
else
{
     @color(HomePhone,7,1)
     @msgbox("The Telephone Number needs to be Exactly 10 Digits", "The numbers you entered " +
     @num(Homephone),"is : " + Vhphonecount + " Digits Please check Home Telephone and Re-enter")
     
}   

Originally I used this code in on element change on every telephone element by copying and changing the element name.  I have since learned that the better method would be to create a subroutine or a function that will be called from each element.  So I tried creating a subroutine (see below) It does not work.


//  Formats Telephone Number with a Subroutine using ThisElement

Subroutine FormatPhone() // Name of Subroutine

// Declare Variables
var vHPhone as String    
var vNewHPhone As String 
var vhphonecount
var vhpnentered

// set Variables to empty
var vHPhone = “”   
var vNewHPhone = “” 
var vhphonecount = “”
var vhpnentered = “”

// Setting variable Vhpnentered to @num of element called from.

     Vhpnentered = @num(ThisElement)      // Writeln(Vhpnentered)

// Calculating number of digits
     
     vhphonecount = @num(@len(vhpnentered))
     //writeln(vhphonecount)

// how to handle if or if not 10 digits

If vhphonecount = "10"


{
     @color(ThisElement,0,7)
     vHPhone = @Num(ThisElement)
     vNewHPhone = "(" + @Left(vHPhone, 3) + ") " + @Mid(vHPhone, 4, 3) + "-" + @Right(vHPhone, 4)
     ThisElement = vNewHPhone 
}
else
{
     @color(ThisElement,7,1)
     @msgbox("The Telephone Number needs to be Exactly 10 Digits", "The numbers you entered " +
     @num(ThisElement),"is : " + Vhphonecount + " Digits Please check Home Telephone and Re-enter")
      
}   


End Subroutine

I placed this code in Global Code section

And in my telephone elements on element change I put FormatPhone()

I thought that this would call the subroutine named FormatPhone and pass the Element Name it was called from. I seem to be misunderstanding the correct method to create my subroutine

Can someone comfortable with Subroutines and functions please explain were I went wrong. Some of us are slow learners and need a little more help then others. (This shows the importance of the upcoming Sesame conference and Masters Seminar classes in October)

Thanks,

Robert
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #1 - Aug 16th, 2004 at 5:39pm
Print Post Print Post  
Robert,

I see a couple of issues. One is that you are not typing two of your variables. This will cause them to be Ints. However, you treat them as Strings. This can cause strange behavior.

Your description of how your code should be working is correct. In what way is it failing? Does it not run at all? Produce incorrect results?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #2 - Aug 16th, 2004 at 6:26pm
Print Post Print Post  
Quote:
Robert,

I see a couple of issues. One is that you are not typing two of your variables. This will cause them to be Ints. However, you treat them as Strings. This can cause strange behavior.


That is a Rookie Error.  Angry

I have changed these from string to integer and back a dozen times this weekend. Must of left them off by accident. I even removed the section that sets the variables to null. I will correct and see if that solves my problem

Quote:
Robert,

Your description of how your code should be working is correct. In what way is it failing? Does it not run at all? Produce incorrect results?


It runs inconsistently and does not run every time. I figured I was doing something incorrect with ThisElement because my other subroutines seem to work fine. If you do not see anything flagrantly wrong with my process other then my declarations I will correct my errors and contact Tech support if my problem continues.

Thanks for the Quick Response as always You guys and gals are the best. Cheesy

  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #3 - Aug 16th, 2004 at 6:30pm
Print Post Print Post  
ThisElement does exactly what you think it does, and you are doing just what you should be doing by making a subroutine and using it in this way.

Let me know if you need help getting it straightened out.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #4 - Aug 16th, 2004 at 7:16pm
Print Post Print Post  
Just to be official I figured I would point out that while upgrading using ThisElement it was also appropriate to replace the old @color command with the improved and more consistant RGBColor() command and the replacement of  the problematic not to be used Goto with ThrowFocus()

{
     RGBColor(ThisElement,0,0,0,255,255,255)
     vHPhone = @Num(ThisElement)
     vNewHPhone = "(" + @Left(vHPhone, 3) + ") " + @Mid(vHPhone, 4, 3) + "-" + @Right(vHPhone, 4)
     ThisElement = vNewHPhone 
}
else
{
     RGBColor(ThisElement,255,255,255,255,0,0)
     @msgbox("The Telephone Number needs to be Exactly 10 Digits", "The numbers you entered " +
     @num(ThisElement),"is : " + Vhphonecount + " Digits Please check Home Telephone and Re-enter")
       Throwfocus(ThisElement)
      
}   


End Subroutine
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #5 - Aug 16th, 2004 at 7:18pm
Print Post Print Post  
Boy, somebody was paying attention in the SBasic class!  Grin

Gold stars for Robert!!!  Grin Grin Grin
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #6 - Aug 17th, 2004 at 3:21pm
Print Post Print Post  
I figured I was on a subroutine roll so I created a few more to handle some simple tasks like first letter capitalization and things like that.

I then created one that checks the validity of a zip code by counting digits and comparing information contained in your zipcode samples database. All was going well until I tried returning values to other fields based on that lookup.

In this example I had the user enter the zip code and then on element exit I want to do a lookup into the zipcode database and return the city, state, time zone, county and +4 digits if available.

I can do this easily if I place my code in all the elements and specifically name the element I want data returned to

Xlookup("C:\Sesame\Data\Samples\ZipCode.db", homezip, "zip code", "city", Homecity)
      Xlookup("C:\Sesame\Data\Samples\ZipCode.db", homezip, "zip code", "state", Homestate)
      Xlookup("C:\Sesame\Data\Samples\ZipCode.db", homezip, "zip code",  "Full county", HomeCounty)
      Xlookup("C:\Sesame\Data\Samples\ZipCode.db", homezip, "zip code",  "Time Zone", HomeTZ)

But

If I want to do this in a subroutine I can change the first parameter to ThisElement  because that stays constant but I need to specify different elements that receive the data.

Can you point me in the direction of the methods you recommend I employ to do that?

Thanks
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #7 - Aug 17th, 2004 at 3:39pm
Print Post Print Post  
1. I recommend you use @XLookupSourceList instead of the multiple XLookups. It's much faster.  Smiley

2. You can use @ElementName(ThisElement) to get and use the name of ThisElement. That way, you can use an If...Then to behave differently based on which element is current.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #8 - Aug 17th, 2004 at 4:27pm
Print Post Print Post  
I must be missing something  ???   Because if I understand you correctly your saying I should use @ElementName(ThisElement) to identify were the subroutine is being called from and then change the parameters accordingly:

Basically create a subroutine that tests for the element I am working with and set my variables to the ones needed something sort of like this:

Var vfrom as string
Var vcity as string
Var vstate as string
Var vzip as string
Var vcounty as string
Var vtimezone as string

Vfrom = @ElementName(ThisElement)

If Vfrom = Homezip
{
     vstate = Homestate
     vcity = HomeCity
     vzip = HomeZip
     vcounty = HomeCounty
     vtimezone = HomeTz
}
Else If Vfrom = Workzip
{
     vstate = Workstate
     vcity = WorkCity
     vzip = WorkZip
     vcounty = WorkCounty
     vtimezone = WorkTz
}
Else If Vfrom = Shipzip
{
     vstate = Shipstate
     vcity = ShipCity
     vzip = ShipZip
     vcounty = ShipCounty
     vtimezone = ShipTz
}
Else If Vfrom = Otherzip
{
     vstate = Outherstate
     vcity = OutherCity
     vzip = OutherZip
     vcounty = OutherCounty
     vtimezone = OutherTz
}

@XlookupSourceList(“C:\Sesame\Data\Samples\ZipCode.db",ThisElement,”
zipcode”,”state;city;zip;county;timezone”)

and then return the values to vstate, vcity, vzip, vcounty.vtimezone.

If  I am understanding this correctly would it not be simpler and easier to put separate code in each element or is there an advantage I am missing or am I totally going down the wrong path?

Sorry if I am being dense I am just trying to get a good grasp on the concepts not just the way to accomplish the task.

Thanks
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #9 - Aug 17th, 2004 at 4:39pm
Print Post Print Post  
Using a subroutine is fine for this, but your pseudocode isn't going to work as is, for several reasons, most of which have to do with the difference between a string (element name) and an element reference. I know you like to figure things out, and I don't want to spoil your fun. Do you want me to show you a working example of how to do this, or do you want to figure it out for yourself?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #10 - Aug 17th, 2004 at 4:47pm
Print Post Print Post  
When you say the:

(difference between a string (element name) and an element reference.)

Are you reffering to element name verses field it is bound to or ?   ???

Thanks
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #11 - Aug 17th, 2004 at 4:59pm
Print Post Print Post  
Quote:
When you say the:

(difference between a string (element name) and an element reference.)

Are you reffering to element name verses field it is bound to or ?   ???

Thanks


"HomePhone" is a string. HomePhone (no quotes) is an element reference. If you use an element reference, you either get the value in the element or the element itself, depending on context. Therefore, for example,

If Vfrom = Homezip
compares the value in vFrom to the value in Homezip.

If Vfrom = "Homezip"
compares the value in vFrom to the string "Homezip".

And so on...
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #12 - Aug 17th, 2004 at 5:08pm
Print Post Print Post  
Sure,  I got that. The one in the quote is the literal the other is what is contained in the element.

Ok show me how I should be doing this subroutine. Sesame version 99999999.0.5 might be released by the time I get this on my own.

Thanks for the help.
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #13 - Aug 17th, 2004 at 5:38pm
Print Post Print Post  
Here's how I'd do it. I'd actually make three procedures.
1. A generic function that can lookup a bunch of address info based on a zip code.
2. An assignment subroutine that sorts out where the values go.
3. A subroutine to put them together to minimize the repeated code.

By separating them, I end up with a nice generic function that is usable on other Forms.

NOTE: I don't have your Form, so I'm just typing off the top of my head. There may be compile errors.  Also, the routines could probabaly use some error handling. :)
[code]
GLOBAL CODE::GLOBAL CODE

FUNCTION GetLocationInfo(vZip as String) As String
var vRet as String

    vRet = ""
    vRet = @XlookupSourceList("C:\Sesame\Data\Samples\ZipCode.db", vZip, "zipcode", "state;city;zip;county;timezone")
    Return(vRet)

END FUNCTION

SUBROUTINE AssignLocationInfo(vName as String, vVals as String)
Var vstate as string 
Var vcity as string
Var vzip as string
Var vcounty as string
Var vtimezone as string

  vstate = @AccessStringArray(vVals, 1)
  vcity = @AccessStringArray(vVals, 2)
  vzip = @AccessStringArray(vVals, 3)
  vcounty = @AccessStringArray(vVals, 4)
  vtimezone = @AccessStringArray(vVals, 5)

If vName = "Homezip"
{
Homestate = vstate
HomeCity = vcity
HomeZip = vzip
HomeCounty = vcounty
HomeTz = vtimezone
}
Else If vName = "Workzip"
{
Workstate = vstate
WorkCity = vcity
WorkZip = vzip
WorkCounty = vcounty
WorkTz = vtimezone
}
Else If vName = "Shipzip"
{
Shipstate = vstate
ShipCity = vcity
ShipZip = vzip
ShipCounty = vcounty
ShipTz = vtimezone
}
Else If vName = "Otherzip"
{
Otherstate = vstate
OtherCity = vcity
OtherZip = vzip
OtherCounty = vcounty
OtherTz = vtimezone
}

END SUBROUTINE

SUBROUTINE DoAssignLocationInfo()
var vName as String
var vZip as String
var vLVals as String

    vName = @ElementName(ThisElement)
    vZip = ThisElement
    vLVals = GetLocationInfo(vZip)
    If @Len(vLVals) > 0
    {
       AssignLocationInfo(vName, vLVals)
    }
   
END SUBROUTINE
[/code]

Then, in each element event where needed:
[code]DoAssignLocationInfo()[/code]
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Help with ThisElement in a Subroutine or Funct
Reply #14 - Aug 17th, 2004 at 8:01pm
Print Post Print Post  
Erika,

Thanks these examples are really helpful.  Smiley

This is good learning material for reverse engineering how things are done. It seems I need to confront Arrays head on. I have not really confronted them in detail yet.

Is Arrays still a topic being covered in the classes in October?

I also need to get a better understanding of  function versus subroutine. I am re-reading that section once again. Eventually it will sink in.

Finally is the reason you would make this a routine instead of a quick cut, paste and edit for each element you  would call this from mainly for better speed or is it just good programming practices to routine and function as much as possible.

Thanks again
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print