Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Properties 2

Status
Not open for further replies.

VeryNewbie2004

Programmer
Sep 9, 2004
16
US
I am trying to add different properties to a function I am trying to write. I am totally new to this concept so please forgive me.

To simplify the kind of thing I am trying to do is return the following values from a single function:

myfunc("sampletext").myfunclength = 10
myfunc("sampletext").myfuncleft2 = "sa"
myfunc("sampletext").myfuncright2 = "xt"

I am totally stuck and have no idea where to start - Any help gratefully appreciated.




 
What you are triing to do does not make any sense in VB. You CAN NOT add properties to a function (not in VB, at least).
But... you could create a class module in your project, lets say MyClass. Implement in this class 3 methods (not properties). As in your example:

MyClass.GetLenghtOf("sampletext") will returl len("sampletext")
MyClass.GetLeft(2,"sampletext") will return left(2,"") and so on and so on.
If you want further assistance in creating your class, tell me!

Hope I've been helpful,
Bogdan Muresan.
 
Thanks Bogdan

Are these classes standard or can you add your own defined ones? Using the example I gave can you give me of an example of a class module that will return .myfunclength, and .myfuncleft2.

My overall aim is somewhat more complicated than this but I assume the concept remains the same.

I hope this makes sense I am afraid I have never played with class modules before, but looking at it seems that it may be able to get me somewhere.
 
While I'm sure that many could provide 'sample' code for a class, examples abound in both the vast array of third party literature and to some lesser extent under the umbrella of the ubiquitous {F1} (aka HELP). Further samples may be discovered in these fora. In all (these) references, search for key-words [Class | Let } Get| Set] and various combinations thereof. I would particularly recommend one (or more) of the third party references (tutorials).






MichaelRed


 
Yes. Here we go:

Add a class module to your project (in project explorer, right click/Add/Class Module).
Change the name of your new class to something (MyClass).
In the class module:
Code:
Public Function GetLenghtOf(TheText as String) as Integer ' This is a method.
   GetLenghtOf = Len(TheText)
End Function

Public Function GetLeft(NoOfCharsToReturn as Integer, TheText as String) as String
   GetLeft = Left(TheText, NoOfCharsToReturn)
End Function

From a form module, in some procedure/function/event:
Code:
Dim TheClass as MyClass ' Where MyClass is the name of the class you made.
Dim MyLenght as Integer
MyLenght = TheClass.GetLenghtOf("Some text for testing the class")

Hope I've been helpful,
Bogdan Muresan.
 
Of corse, I didn't make any error handling to the class. You have to check if the NoOfCharsToReturn > Len(TheText), etc.

NoOfCharsToReturn is a parrameter that sets how many characters from the left the function returns.

Hope I've been helpful,
Bogdan Muresan.
 
Sorry, I ment "Dim TheClass as New MyClass".
The word "New" is essential.
Otherwise, you have to put "Set TheClass = New MyClass " before you call it's methods...

Hope I've been helpful,
Bogdan Muresan.
 
>You CAN NOT add properties to a function

Well, you can kind of - although it still involves a class. The trick is that the function returns a class that has the properties that you want
 
Heh!...
Weeeel..., yaeh..., you can do that, but I don't think that VeryNewbie2004 ment something so twisted, after all he is "VeryNewbie", as he/she tells...
But what you are sayng is true, none the less!

Hope I've been helpful,
Bogdan Muresan.
 
I am indeed very new to this, but the I think with Bogdans help I will be able to do what I want to do.

Thanks very much for you assistance - it's very much appreciated.
 
If we're going for simple, then I don't understand why BogdanMBM's solution requires a class at all
 
Because VeryNewbie2004 wants a function that does different things depending on the parameters supplied.
OK. You could write a function with (very) many parameters, almost all ot them being optional and haveing inside the function a (very) long "case" statement examening the presence of different parameters. VeryNewbie2004 wants a SINGLE function with many purposes. That's why I thought a class would be easyer to write (based on many simple methods) and a lot easyer to mantain. Think of new functionalityes: for the class perspective, just a new (simple) method; for the function perspective, some new parameters, modification of the "case"...
And it is a SIMPLE (basic) class, after all...

Hope I've been helpful,
Bogdan Muresan.
 
I would agree w/ strongm. Unless the parameterization is VERY complex, the arg list can (generally) be simplilfied to the "Return" desired and (at worst) a parameter array of (input) arg value(s). Of course, the "case" thinngy (or equivalent) needs to exist, but it hardly needs to be complicated).

I'm sure that some will object, noting that the user (caller) needs to be aware of the various options and the args necessary for each, but this is generically necessary (or at least highly useful) for use of other possabilities (class module, or -in the simplest "case" - just go for seperate routines.






MichaelRed


 
MichaelRed, kindly ask you to post a function that does what VeryNewbie2004 asked for in the first place. Then trie to add another 3 or 4 functionalities to that function and postit again. Then we can discuss about the ease on maintaining that function.
Until then, I maintain my perspective. Of corse, we can just use an ordinary module with many subs/functions to do the same thing as MyClass, but I was just trying to maintain VeryNewbie2004's view of the problem -> I transformed his ideea of a "function with properties" to a "class with methods". As simple as that.

Hope I've been helpful,
Bogdan Muresan.
 
My point is that, given we have abandoned the idea that the function might return properties, I'm trying to find out why

Dim TheClass as MyClass ' Where MyClass is the name of the class you made.
Dim MyLenght as Integer
MyLenght = TheClass.GetLenghtOf("Some text for testing the class")

is better than the somewhat more direct

Mylength = Len("Some text for testing the class")

or why is

Dim TheClass as MyClass ' Where MyClass is the name of the class you made.
Dim MyLenght as Integer
LeftText = TheClass.GetLeft(2,"Some text for testing the class")

better than

LeftText = Left("Some text for testing the class",2)
 
OK. I give up.
We went to far from the initial question, I think.
strongm, you are right. In this simple implementation, with MyClass you write more code. But VeryNewbie2004 could want in the future something more complex then that.
I'm convinced that we all know the use of a class module when it comes to port a certain functionality from one application to another. Just import the old class...

Hope I've been helpful,
Bogdan Muresan.
 
Indeed that is right I may be new to this but I am going a little beyond a couple of text functions! I just gave those as an example so I could see how I could achieve what I wanted to and I think I will be able to.
 
if the "functionallity" (?) of various approaches is the discussion, then "class" is often (generally?) thouoght to be a preferred method to encapsulate the properties of a specific object, using a (single) class for the possible properties of all vehicles for instance in NOT generally considered appropiate, since all such objects do NOT have (very many)common properties. On the otherhand a Class for "passenger cars" would have quite a few common properties and would be considered a reasonable object for a class. Since VeryNewbie2004 does not provide any substantive cointext for the generic desire to use a common pbject to return the variety of data, I (originally) advised the use of laerning resources which, hopefully, would introduse a variety of concepts. "Shoe horning" one concept into the nearest pigenhoile does not -from MY PERSPECTIVE- do either party true justice. Re the parable of the fish, why shove the fins and scales down the throat, when a bit of leading towards the takle box might easily return more positive (if not necessarily quicker) results?




MichaelRed


 
Sure. In which case the answer to the original question should be "You don't want to use a function, you want to use a class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top