home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2971
Discussions: 188,378, Posts: 2,243,450, Members: 232,606
Old October 21st, 2002, 08:30 PM   Digg it!   #1 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 21,017
Method with a property?

cmd.Parameters.Add("@Servername", SqlDbType.VarChar, 30).Value = Trim(psServerName)


Ok the method sqlcommand.parameters.Add().Value...
This method takes arguments for the Add method but also at the same time can take a value for the Value property .....

How is this coded?
I mean how do you associate a property to a method?
*showing my ignorance of classes lol*

vass0922 is online now   Reply With Quote
Old October 22nd, 2002, 04:46 AM     #2 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
Nah, it's easy really.

The method returns an object, which has a Value property, which you then set.

The code is equivalent to (assuming VB?):

Dim tempObject as Object
Set tempObject = cmd.Parameters.Add("@Servername", SqlDbType.VarChar, 30)
tempObject.Value = Trim(psServerName)

replacing Object with the actual return type of cmd.Parameters.Add().

strangerstill is offline   Reply With Quote
Old October 22nd, 2002, 05:10 AM     #3 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 21,017
nope, I got that part no problem
I was wondering how to code a class so I can create a method that takes parameters AND has a property. I can set a value to the property by calling the method, but I don't know how to create a method like that.

Essentially what I'm trying to do is to create a AddDBCmd method to a class that kind of mocks that method but does all the work in the background.

how do I CREATE a method that has a value property
and WTH are you doing up!? don't you work in the day?! lol

vass0922 is online now   Reply With Quote
Old October 22nd, 2002, 06:42 AM     #4 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 21,017
Ok maybe I do understand what you're saying.. but when I make a call to the method and say I call my method AddDbCmd...

classobject.AddDbCMD("storedprocname", sql.varchar, 30) = "servername"
isn't it going to throw a fit because there's no property associated with the method?

sorry if I'm not making any sense, had a rough night at work.. and its nearly 6am ... sun will be up soon
vass0922 is online now   Reply With Quote
Old October 22nd, 2002, 09:37 AM     #5 (permalink)
Member
 
Join Date: Oct 2001
Location: Midland, NC USA
Posts: 64
Ye canno do that, lad. Really you can, but not directly.
Stranger touched on the reason why. Here's a 50,000 foot view:

You have Objects. Which expose their Properties, Methods, and Events.

An exposed (or "Public") read/write property is really a pair of Methods that Set and Get the value stored in an object variable. Object.property = value is really a call to the Object.property.set(newvalue) method. Variable = Object.property is really a call to the Object.property.get method that returns the value of the object variable.

But you gotta have a reference to the Object before you can call any of its methods.

What you're trying to do in your example is Add a (fully-populated) Parameter object to the Command object's collection of parameters. To do this, you are required to Create a Parameter object, populate it, then Append it to the Parameters collection.

Fortunately, the CreateParameter method of the Command object lets you create a fully-populated Parameter object, complete with a value in one swell foop.


Suggestion: Build a method that, as far as its interface goes, looks very much like the Command.CreateParameter method with the addition of one more, er, parameter: a reference to a Command object.
Sort of like AddParameter(CommandRef, Name, Type, Direction, Size, Value)
AddParameter would do the Set objNewParm = CommandRef.CreateParameter(Name, Type, Direction, Size, Value) and CommandRef.Parameters.Append objNewParm drill.

Your calling code would look something like:
Create and set up your Command object. We'll call it objCmd.
Then, to add a new Parameter to objCmd, call YOUR method.
AddParameter(objCmd, "@Servername", sql.varchar, 30, Trim(psServerName))
UncaDanno is offline   Reply With Quote
Old October 22nd, 2002, 09:19 PM     #6 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 21,017
The reason I asked about that specific line of code is that I use that very same line of code in an application. It's in VB.NET
cmd.Parameters.Add("@Servername", SqlDbType.VarChar, 30).Value = UCase(Trim(psServerName))
I completely understand having an object with a method AND a property in two lines of code, but I dont understand how they do it in one line?
its a method from the System.Data.SqlClient.SqlCommand class
so essentially it is
System.Data.SqlClient.SqlCommand.Parameters.Add

Now "parameters" is really a collection, that has the Add method.
I realize I can just pass a 4th parameter for the value and everythign would be all good... but don't learn anytihng new that way!
vass0922 is online now   Reply With Quote
Old October 22nd, 2002, 10:03 PM     #7 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 21,017
Ok I think I got it...
My add method

Public Function AddCmdParm(ByVal psParamName As String, ByVal psDataType As SqlDbType, ByVal piSize As Integer) As Param

Then I have another class that containers a property
Code:
  Public Class Param

    Private Parameter As String
    Public Property Value() As String
      Get
        Return Parameter
      End Get
      Set(ByVal pValue As String)
        Parameter = pValue
      End Set

    End Property 
  End Class
however, when I do a .AddCmdParm(lbahblahblah). the "Value" doesn't popup

(btw, VB.NET for clarity)

Last edited by vass0922 : October 22nd, 2002 at 10:12 PM.
vass0922 is online now   Reply With Quote
Old October 22nd, 2002, 11:12 PM     #8 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Alberta, Canada
Posts: 563
k, umm, bear with me here, it's been awhile since I've done anything serious with classes... and I'm tired... (also, all I really know of vb.net is what people have posted here!!)

first of all, what I see is a function(AddCmdParm(...)), returning a class object of Param. So... What I'm wondering is, how is the function supposed to have a .value property??

but, can you make/add/build/blah/blah your function into a class (if not already) and then have a composite class, Param, within this class...?? (whoa, composition, haven't dealt with that in a year or two!!! ) and then you should (f I remeber correctly) be able to do your -> someclass.AddParam(blah...blah...).value


hmmm, might have to pickup that ol' Deitel & Deitel C++ book and do some refreshing on classes again!!

cheers, and G'Luck!!
^hyd^ is offline   Reply With Quote
Old October 22nd, 2002, 11:22 PM     #9 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
Hmm. Vass, it looks like what you have there should work. The analagous stuff in VB is this:
=======Class1 class module=======
Private Parameter As String

Public Property Get Value() As String
Value = Parameter
End Property

Public Property Set Value(ByVal pValue As String)
Parameter = pValue
End Property

=======Module code======
Public Function doIt() As Class1
doIt = New Class1
End Function

Public Sub foo()
doit().
End Sub

as you'd expect, typing doit(). brings up the list with Value as the item in the list. So... it should (!) work in VB.NET... shouldn't it?
strangerstill is offline   Reply With Quote
Old October 23rd, 2002, 12:35 AM     #10 (permalink)
Member
 
Join Date: Oct 2001
Location: Midland, NC USA
Posts: 64
OK, let me see if I can 'splain this....without sounding like a broken record.

In this generation of compilers, working with objects is a question of calling methods to do the dirty work.

Let's say you have a class, "Parameter" that has five public properties, "Name", "DataType", "Length", "Direction", and "Value".
This class would have, at the least, twelve methods:
The constructor (or instantiator or creator)
The destructor (that cleans up when the class is destroyed)
Set Name, which takes a string (Parameter.Name = "@Whatever")
Get Name, which returns a string (strVar = Parameter.Name)
Set DataType, which takes an integer indicating the datatype (Parameter.DataType = 1 [or a literal constant that is equal to 1])
Get DataType, which returns an int (intVar = Parameter.DataType or If Parameter.DataType = 1)
Set Length, which also takes an int (Parameter.Length = 20)
Get Length, which returns an int (If Parameter.Length > 3)
Set Direction, which, like DataType, takes an int. This time indicating whether the parameter is input, output, or both (Parameter.Direction = 1 [or a literal constant that is equal to 1]
Get Direction, which returns an int (intVar = Parameter.Direction)

You also have the Setter and Getter for Parameter.Value. More on this below.

You also have the Parameters collection class. Being a collection, all it really is is an array of pointers to Parameter objects. The Parameter objects themselves aren't really in the collection. They are created and pointed to by the pointers in the collection.

The Add method of the Parameters collection does nothing more than add a pointer to a Parameter object to the array. Sort of.
This is a polymorphed method.
One version of the Add method simply takes a pointer to a Parameter as input and returns nothing. [Parameters.Add(ptrParameter)]
Another version takes the same input as the constructor for a Parameter object and returns a reference to the pointer it just added to the array. [ptrParameter = Parameters.Add("@NewParm",xxxx.Integer,4,xxxx.Inpu t)] or [ptrParameter = Parameters.Add("@AnotherParm",xxxx.String,20,xxxx. Input,strInputValue)]

By definition, an Object can have methods, properties, and events. You get at an object's public properties with the "dot-propertyname" construct which call's the object's Get or Set method for the property. Get or Set is called based on which side of the (sometimes implied) assignment operator the .propertyname is on. e.g. var = object.propertyname calls the object's Get propertyname method and object.propertyname = value calls the object's Set propertyname method.

An object's methods are called with the "dot-methodname" construct. object.methodname calls a method.

The compiler resolves things left to right. So when it sees something like "ptrParameter = new Parameter("@NewParm",xxxx.Integer,4,xxxx.Input)" it processes thusly:
I have a variable on the left of an assignment operator so I'm going to assign a value to it.
The first thing after the assignment operator is the keyword "new". This tells me that I am going to call some object's constructor and assign the address of the new object to the variable.
I'm constructing a Parameter object. The arguments being passed to the constructor pass muster, so I create the new Parameter object and put its address in ptrParameter.

Next, when the compiler sees "ptrParameter.Value = 16", things go like this:
I have a pointer to a Parameter object and I need to call a method named Value. The only thing named Value in a Parameter object is one of its properties, so I need to call either Get Value or Set Value. This call is on the left side of the assignment operator so I need to call Set Value and pass it the value on the right hand side of the assignment operator.

The compiler needs to know if the datatypes it is working with here are compatible. Sixteen is an integer. This particular instance of the Parameter object was created indicating that Value is going to be an integer. So eveything is cool.
If you would have tried ptrParameter.Value = "Some string", the compiler would have tossed an error because you would be trying to assign a string value to an integer.

To make a godawfully long story short, you have an object with a property whose datatype is dynamic. The property's datatype is not known until an instance of the object is created an you indicate what the datatype is. Further, you can't assign a value to the property unless the value's datatype is compatible to the datatype of the property.

So if you have ptrParameter = Parameters.Add("@AnotherParm",xxxx.String,20,xxxx. Input,strInputValue), everything is cool. As long as the datatype of strInputValue is String. This morph of Parameters.Add gets all the input it needs to call the constructor of Parameter and return a reference to the new Parameter object. In addition, the constructor can see that the datatype of the value it is going to assign to Value is compatible with what you are creating Value to be.

If you have ptrParameter = Parameters.Add("@AnotherParm",xxxx.String,20,xxxx. Input).Value = strInputValue, you have two basic problems.

First, the ".Value" (a property) is appended to the ".Add" a method. And methods can't have properties.

Say WHAT???? That was your question in the first place, wasn't it? Something like "why can't a method have a property?"


Simply because the compiler won't allow it. Period.

Why won't it? Because it was written that way.

Why was is written that way? Because that's the way it was designed.

Why was it designed that way? Well, now we're getting to the part of the discussion that could sound a lot like an exerpt from the Book of Job where he is talking to the whirlwind.
UncaDanno is offline   Reply With Quote
Reply


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

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2883)
The United States Debt (20)
Looks like Burris will get his Sena.. (7)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
dual monitors wont boot (0)
Folderchat Weekday thread (439)
New Build ( Finally ) (0)
MSN Hotmail Down??? (7)
Help with an Ati Radeon HD 4850.. (23)
Laptop waking up itself (0)
CPU wont boot (3)
Best digital camera for under 2.. (13)
Building first computer, will t.. (2)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:14 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28