pass part of name as argument

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I want to access multiple arguments based on name passed.

For example I have the following asp:textboxes:

BillingAddress1
BillingAddress2
BillingCity
ShippingAddress1
ShippingAddress2
ShippingCity

I have 2 subroutines. Sub A call Sub B and passess either "Billing" or
"Shipping". I wanted to be able to access the objects
(BillingAddress1.Text, ShippingAddress1.Text etc) by a concatenated name. I
don't want to say

If name = "Billing" then
BillingAddress1.Text = "something
else
ShippingAddress2.Text = "something
end if

I want to do something like:

Sub A ()
Call B ("Billing",somevalue1, somevalue2,somevalue3)
Call B ("Shipping",somevalue4,somevalue5,somevalue6)
End Sub

Sub B (Name,value1,value2,value3)
Name + Address1 = value1 ' I know I can't do it this
way - I want to replace "Name + Address1" with something else.
Name + Address2 = value2
Name + Address3 = value3
End Sub

Thanks,

Tom
 
tshad,

Why don't you want to use normal code. Programming ends if you want to make
your own definitions. The code you show is in my idea perfect.

However maybe is a nicer alternative the Select Case which is even a little
bit nicer for this.

http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx

By the way using the + for string concatination can be in Visual Basic give
unpredictable result with option strict of, probably therefore even more
with ASPNET. The correct character for that is "&".

"1" + "1" gives "2"
"1" & "1" gives "11"

I hope this helps,

Cor
 
Cor,

Cor Ligthert said:
By the way using the + for string concatination can be in Visual Basic
give unpredictable result with option strict of, probably therefore even
more with ASPNET. The correct character for that is "&".

"1" + "1" gives "2"

No, it doesn't. '"1" + 1' gives 2 and '1 + "1"' gives 2, but '"1" + "1"'
always gives '"11"'.
 
tshad said:
I want to access multiple arguments based on name passed.

For example I have the following asp:textboxes:

BillingAddress1
BillingAddress2
BillingCity
ShippingAddress1
ShippingAddress2
ShippingCity

I have 2 subroutines. Sub A call Sub B and passess either "Billing" or
"Shipping". I wanted to be able to access the objects
(BillingAddress1.Text, ShippingAddress1.Text etc) by a concatenated name.
Sub A ()
Call B ("Billing",somevalue1, somevalue2,somevalue3)
Call B ("Shipping",somevalue4,somevalue5,somevalue6)
End Sub

Sub B (Name,value1,value2,value3)
Name + Address1 = value1
<snip>

One possible way to solve this would be to isolate the textbox
selection code in a function. And then retrieve it like this:

<aircode>
Function GetTextBox(Name As String) As TextBox
Select Case Name.ToLower
Case "billingaddress1": Return Me.BillingAddress1
Case "billingaddress2": Retuirn Me.BillingAddress2
'... I guess you got the picture
End Select
End Function

Sub B(Name As String, ...)
GetTextBox(Name & "Address1").Text = Value1
GetTextBox(Name & "Address2").Text = Value2
GetTextBox(Name & "City").Text = Value3
...
End Sub
</aircode>

Now, if you really want to go overboard (or if you want to do this
dinamically), I guess you can load a collection with your textboxes,
and index it by textbox name. Then it's just a matter of providing a
valid key to the collection. Something in the lines of:

<aircode>
Imports Cols = System.Collections.ObjectModel
Imports WinForms = System.Windows.Forms

'This collection stores textboxes selected by name
Class TextBoxCollection
Inherits Cols.KeyedCollection(Of String, WinForms.TextBox)

Sub New(ByVal ParamArray Items() As WinForms.TextBox)
MyBase.New(System.StringComparer.InvariantCultureIgnoreCase)
For Each Item As WinForms.TextBox In Items
Add(Item)
Next
End Sub

Protected Overrides Function GetKeyForItem( _
ByVal Item As WinForms.TextBox) As String
Return Item.Name
End Function

End Class

'--- Then, at page/form/whatever initialization:

Private GetTextBox As New TextBoxCollection( _
BillingAddress1, BillingAddress2, BillingCity, _
ShippingAddress1, ShippingAddress2, ShippingCity _
)

'--- Finally, in Sub B...
Sub B(Name As String, Value1 As String, _
Value2 As String, Value3 As String)

GetTextBox(Name & "Address1").Text = Value1
GetTextBox(Name & "Address2").Text = Value1
GetTextBox(Name & "City").Text = Value1

End Sub
</aircode>

HTH.

Regards,

Branco.
 
Cor Ligthert said:
tshad,

Why don't you want to use normal code. Programming ends if you want to
make your own definitions. The code you show is in my idea perfect.

However maybe is a nicer alternative the Select Case which is even a
little bit nicer for this.

http://msdn2.microsoft.com/en-us/library/cy37t14y.aspx

By the way using the + for string concatination can be in Visual Basic
give unpredictable result with option strict of, probably therefore even
more with ASPNET. The correct character for that is "&".

The "+" was just a meta code for what I was trying to achieve.

If you notice the variables are exactly the same (Address1, Address2 and
City) except they either have "Billing" or "Shipping" in front of them. I
don't want to pass each one as a parameter or use an "if" or "select"
statement. I want to somehow concatenate the type (billing or shipping) to
each variable (address1 -> billingaddress1 or shippingaddress1).

So that I can do something like:

NewName.Text = value1

Where NewName would either be ShippingAddress1 or BillingAddress1. I know
this syntax is not correct - I am just trying to convey what I am trying to
do.

Thanks,

Tom
 
Do you mean something like

CType(Page.FindControl(Name + Address2),TextBox).Text = value1
 
I find that the straightforward method works best in most situations. You
should however be able to pass the textbox you want to fill along with the
data you want to fill it with to a procedure. You send the textbox rather
than flag that indicates which textbox.

The following BTW is an "odd" (by typical development standards)
functionality. If by this you mean use the flag value to determine which of
3 textboxes gets the data in the other parameters.

Call B ("Billing",somevalue1, somevalue2,somevalue3)
Call B ("Shipping",somevalue4,somevalue5,somevalue6)

I assume that "somevalue" are variables right? Do you pass different
variables or pass them in a different order sometimes? In other words the
billing vars go with the billing textboxes and the shipping vars go with the
shipping textboxes. So unless I miss my guess (and I might have) the calls
always look the same. That means it is 3 (or 6) simple assignments right?
 
Do you mean something like

CType(Page.FindControl(Name + Address2),TextBox).Text = value1

Yes - where Name is a string passed that is prepended to all the objects
(address1,address2 and city).

Thanks,

Tom
 
Tom Leylan said:
I find that the straightforward method works best in most situations. You
should however be able to pass the textbox you want to fill along with the
data you want to fill it with to a procedure. You send the textbox rather
than flag that indicates which textbox.

I could send the textbox (this is just an example) - there could be 20 or 30
textboxes but they will all have the name prepended to name.
The following BTW is an "odd" (by typical development standards)
functionality. If by this you mean use the flag value to determine which
of 3 textboxes gets the data in the other parameters.

This is not a flag per se, but just the beginning of all the textboxes Names
(ID). I have duplicate types of textboxes that act exactly the same way and
I don't really want to test which type we are dealing with (which I would if
it were just a flag) I just want to use the variable.
Call B ("Billing",somevalue1, somevalue2,somevalue3)
Call B ("Shipping",somevalue4,somevalue5,somevalue6)

I assume that "somevalue" are variables right?
Yes.

Do you pass different variables or pass them in a different order
sometimes?

I would be passing the same number of variables and in the same order - just
putting them either in the Shipping variables or Billing variables. I could
also have other sets such as Home variables or Work variables. But they
would always be the same (ShippingAddress1, BillingAddress1, HomeAddress1
etc).
In other words the billing vars go with the billing textboxes and the
shipping vars go with the shipping textboxes.

Based on the 1st variable passed.
So unless I miss my guess (and I might have) the calls always look the
same. That means it is 3 (or 6) simple assignments right?

Yes. I would have one Sub. The first variable would be the value prepended
to each variable and each value assigned in order. I may also have it sent
to a database record.

Thanks,

Tom
 
I could send the textbox (this is just an example) - there could be 20 or
30 textboxes but they will all have the name prepended to name.
This is not a flag per se, but just the beginning of all the textboxes
Names (ID). I have duplicate types of textboxes that act exactly the same
way and I don't really want to test which type we are dealing with (which
I would if it were just a flag) I just want to use the variable.
I would be passing the same number of variables and in the same order -
just putting them either in the Shipping variables or Billing variables.
I could also have other sets such as Home variables or Work variables.
But they would always be the same (ShippingAddress1, BillingAddress1,
HomeAddress1 etc).

Consider not passing anything to anything :-) You really should avoid the
"name dependency" aspect of this. It is commonly done but not the ideal
solution.

You have textboxes in place and you have variables. If you absolutely
cannot straightforwardly assign the values then consolidate references to
the textbox "sets" and consolidate the data via an array (you can create
this on the fly so it doesn't have to formalized.) Then you can call:
SetAddresses( RefToTextBoxes, ArrayofData )

This eliminates any consideration of what the name of the textbox happens to
be and will work even if you rename them. So at some point during the
instantiation of your form simply copy each of the references to the
textboxes you want into a collection and give it a representative name
TBBillings(), TBShippings() (or whatever). Take all those separate
variables and consolidate them into an array (you can do this as you call
the function) and pass the collection and the array to use. As arrays (or
collections) the number of items can grow without having to add more
parameters to everything.

The size of a collection of textbox references isn't anything to be
concerned about and everything gets packaged neatly. Importantly the code
no longer cares about the names of these things. Any time you start to
write a function for reuse and the words "make sure you name the textbox
'Billing1'" enters the conversation consider there may be another way.

Hope this helps.
 
Back
Top