Fred Mastropasqua's Facebook profile

Want to Pass In a UserControl into your Class? Yes it’s possible.

by Fred Mastro 18. September 2009 17:23

I had a situation where I had a Payment UserControl that had say, 20 properties on it. I also had a Class file (App_Code) that I wanted to use to handle some of the business logic when the payment was complete. Say create tables in the database.

I could have A) Created a method on the class and sent in 20 arguments. B) Created 20 properties on that class and populate them all before I call the method. But that was waste of code to me, when I already had the information. I just wanted to do something with it.

So I thought, what if I just send in my UserControl into my class method by reference, then I can just use the properties directly to retrieve the value.  So I tried… no luck. Class can’t see my UserControl class.

Problem/Broken Version:

My UserControl’s class is Application_Controls_Payment

Partial Class Application_Controls_Payment
Inherits System.Web.UI.UserControl
'...
End Class

In my class file I tried to use it as an argument on a function that the page would send me. However this fails on the compiler.

Public Shared Function CreateTransactionRecordInDB(ByRef PaymentUserControl As Application_Controls_Payment) As Integer
'This Errors, as Application_Controls_Payment class from the UserControl can not be seen from this level
End Function

Solution:

So I created an Interface for all the properties, and Implemented it on my UserControl. Then Sent the UserControl as the Interface and low and behold, it works great!

Step 1: Create Interface with all the properties.

Public Interface PaymentControl
Property CreditCardNumber() As String
Property CCVCode() As String
Property ExpirationMonth() As String
Property ExpirationYear() As String
Property TransactionAmount() As Double
Property TransactionAmountAdditionalInfo() As String
Property TransactionDescription() As String
Property InvoiceNumber() As String
Property BillingFirstName() As String
Property BillingLastName() As String
Property BillingEmail() As String
Property BillingCompany() As String
Property BillingAddress() As String
Property BillingState() As String
Property BillingCity() As String
Property BillingZipCode() As String
Property CustomerID() As String
Property CustomerIPAddress() As String
Property EmailCustomerReceipt() As Boolean
Property EmailReceiptHeader() As String
Property EmailReceiptFooter() As String
Property EmailOfMerchant() As String
Property TestMode() As Boolean
Property ValidationGroup() As String
End Interface

Step 2: Implement this Interface To your UserControl

Partial Class Application_Controls_Payment
Inherits System.Web.UI.UserControl
Implements CheckOut.PaymentControl

Public Property CreditCardNumber() As String Implements CheckOut.PaymentControl.CreditCardNumber
Get
Return txtCCNumber.Text
End Get
Set(ByVal Value As String)
txtCCNumber.Text = Value
End Set
End Property
Public Property CCVCode() As String Implements CheckOut.PaymentControl.CCVCode
Get
Return txtCCVCode.Text
End Get
Set(ByVal Value As String)
txtCCVCode.Text = Value
End Set
End Property
'... Rest of Properites
End Class

Step 3: Change your Method Constructor to want the Interface class vs trying to call the UserControl’s class.
Public Shared Function CreateTransactionRecordInDB(ByRef PaymentUserControl As PaymentControl) As Integer
'Now you can reference the properties from the usercontrol
Dim oPaymentTransactions As New PaymentTransaction
oPaymentTransactions.BillingAddress = PaymentUserControl.BillingAddress
oPaymentTransactions.CCVCode = PaymentUserControl.CCVCode
oPaymentTransactions.CCExpirationMonth = PaymentUserControl.ExpirationMonth
'Rest of code here
End Function

And there you go.

Tags: ,

VB.NET | Programming

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

A Certified MCSE (NT4 & 2k), MCDBA (2k), A+, CCA, with over 10 years of experience with Windows Networking and Development. Developing mainly in ASP.NET, VB.NET and T-SQL. Also develops in Objective-C (iPhone), XAML (SilverLight & WPF), C#, "Classic" ASP 3.0, ADSI,  VBscript, WScript.

Non-technical hobbies include other areas such as Movie watching (action, epic, comedy, some romantic comedy, well everything), Reading (Science Fiction, Fantasy, Detective and Programming categories), Film Editing, Directing with Special effects (using Adobe Premiere and Adobe After Effects), Dungeons & Dragons (D&D 4th Edition), Auto-Cross Racing & Cars (BMW M3, MazdaSpeed's), Motorcycles (Honda CBR 600), TV Shows (Flight of the Conchords, Lie To Me, DollhouseBattleStar Galatica, Smallville, Alias), Music (Akon, Billy Joel, Micheal Bublé, Bid Daddy Weave, T-Pain, Barlow Girl, Notorious B.I.G and more, love all types of music), and Religion (Christianity, debating and prophecies).


Web Tools - QuickLinks

Web tools I use more then others. Some of these are on my Link Collections page, but this made it easier for me to go to my site and click a tool.

  1. Telerik Code Converter (C# to VB/VB to C#)
  2. Lorem Ipsum - Dummy Text for Prototype Apps
  3. Web Color Values
  4. Open Source Icons

 

Highlights

  • Some websites I've worked on. This is a small collection of sites I've developed or added to awhile back.

  • Revenge Movie Trailer. Trailer I made with Adobe Premiere and After Effects. Jason Christman is the main star and I'm the director behind the camera.

  • Essential Software For your Mac. - I'm a Microsoft geek, but I've switched over to Mac. There was a lot of stuff I needed to get installed that I missed on my Windows machine. Also I had no idea how to do it :p Here's some help.

  • Speed Football. I wanted to make a special effect like the Smallville or Superman running fast. All the other ones I've seen, the person in the frame was the only moving object while everything else was blurred. I wanted to create the effect but interact with another normal moving object.
  •    
  • Code Snippets and Quickies. Sometims I find something or develop something that I think is useful and it can be copied and pasted anywhere for someone to use. Here's a collection of things I've posted on.
  • Books I've Read or Reading