 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

September 21st, 2006, 01:36 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Inheritance
Hello,
I'm trying to work with inheritance in classes. Below in my
namespace I'd like to have a base email class and 2 other classes
that inherit the base constructor but send different subjects and
subject bodies. I'm not sure how to do this, whether I should use
parameters for the subject and subject bodies or private variables,
maybe you know or maybe what I'm trying to doesn't sound right.
Imports Microsoft.VisualBasic
Imports System.Net.Mail
Namespace Email
Public Class SendEmail
Sub New(ByVal sendTo As String, ByVal sendFrom As String, ByVal sendSubject As String, ByVal sendBody As String)
Dim mailObj As New MailMessage(sendTo, sendFrom, sendSubject, sendBody)
mailObj.Subject = sendSubject
mailObj.Body = sendBody
mailObj.IsBodyHtml = True
Dim smtp As New SmtpClient
smtp.Send(mailObj)
End Sub
End Class
Public Class AccountCreated
Sub New(ByVal sendTo As String, ByVal sendFrom As String, ByVal sendSubject As String, ByVal sendBody As String)
Dim mailObj As New MailMessage(sendTo, sendFrom, sendSubject, sendBody)
mailObj.Subject = sendSubject
mailObj.Body = sendBody
mailObj.IsBodyHtml = True
Dim smtp As New SmtpClient
smtp.Send(mailObj)
End Sub
End Class
Public Class Checkout
Sub New(ByVal sendTo As String, ByVal sendFrom As String, ByVal sendSubject As String, ByVal sendBody As String)
Dim mailObj As New MailMessage(sendTo, sendFrom, sendSubject, sendBody)
mailObj.Subject = sendSubject
mailObj.Body = sendBody
mailObj.IsBodyHtml = True
Dim smtp As New SmtpClient
smtp.Send(mailObj)
End Sub
End Class
End Namespace
thanks,
Michael.
|

September 23rd, 2006, 05:07 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Michael,
Why are you using constructors for sending out e-mail? Personally, I think that's a flawed design. Constructors are used to instantiate new instances of objects that you work with afterwards. In your situation, it seems you're done with the object after you constructed it, right? I think you're much better off with a few smart overloads, like this:
Code:
Public Class SendMail
Public Shared Sub Send (ByVal subject As String, body As String, mailServer As String)
....
mailObj.Subject = subject
mailObj.Body = body
....
Dim smtp As New SmtpClient(mailServer)
smtp.Send(mailObj)
End Sub
Public Shared Sub Send (ByVal subject As String, body As String)
Send (subject, body, "default mail server"
End Sub
Public Shared Sub Send (ByVal subject As String)
Send (subject, "Default body", "default mail server"
End Sub
Public Shared Sub Send ()
Send ("Default subject", "Default body", "default mail server"
End Sub
End Class
This code allows you to call various flavors of the Send method, passing in detailed information as you see fit. It's just a simple example, but hopefully it puts you in the right direction. Important keywords here are Shared methods and overloads.
If this isn't what you're after, can you describe in a bit more detail what you're trying to accomplish?
Imar
(Edit: added the Shared keyword to the code)
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|

September 25th, 2006, 11:01 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
First of all, is it not good practice to use construtors for every class you define?
Secondly where are the shared methods in your class?
I understand overloading the methods, but what I am trying to create is a class that will send 3 different emails, a standard email, an email to admin, and an email when a product has been bought.
I would like to encapsulate the sendto, send subject, and send message in the classes so that they can be easily changed from one central point.
I'd like only the standard email to have a parameter for the subject so that the sendto email can be defined as a private member variable - so that any pages that use this class and method will go to the same email.
I'd like the second email method that goes to admin to use a private member variable for the email address, the email subject and email message.
And I'd like the third email method that goes to admin on purchase to use a private member variable for both the email and subject and message body.
Does this make sense?
thanks,
Michael.
|

September 25th, 2006, 11:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
On the contrary: it is good practice. If you leave out the constructor, the compile will add one for you.
However, IMO, it's odd that a constructor sends a message. I think a constructor should do nothing more than construct the object and initialize some data. Then a method should do the work.
What happens when a developer using your class does this:
Dim myObject As New MailClass()
As this point, he or she may think that all they did was create a new object, but in the background already an e-mail message have been sent. Personally, I would do something like this:
Dim myObject As New MailClass()
myObject.Send()
This creates an object, possibly defaulting some variables, fields etc. Then a developer has to call Send explicitly.
By creating classes that inherit from your base class, the model stays the same. You can do this:
Dim myObject As New SpecializedMailClass()
myObject.Send()
and things will still work.
I changed my earlier post and added the Shared keyword that I had forgotten. With the shared keyword you can do this:
SpecializedMailClass.Send(SomeValue, SomeOtherValue)
or
SpecializedMailClass.Send(SomeValue)
or even:
SpecializedMailClass.Send()
Then the shorter Send overloads define the default values for the parameters for the extensive overloaded version.
Does this help?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|

September 25th, 2006, 10:17 PM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
thanks that makes sense regarding using the constructor to initialize private member variables and the public shared methods.
I'm still having difficulty with the inheritance issues though I'll try and work out a code example myself and then maybe repost it.
thanks again.
|

September 26th, 2006, 01:40 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Good luck. You know where to find us if you need more help...
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Inheritance |
magagulad |
Visual Basic 2005 Basics |
1 |
May 12th, 2008 07:51 AM |
Inheritance |
abhi_bth |
ASP.NET 1.0 and 1.1 Basics |
0 |
September 23rd, 2006 10:03 AM |
Need help with inheritance |
filip |
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 |
1 |
August 25th, 2006 09:38 PM |
c# inheritance |
bhohman |
C# |
2 |
March 26th, 2004 01:47 PM |
|
 |