Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 8th, 2007, 07:59 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Set variable

Hi,

I'm wondering how to mandatory set a variable when using a class. For example I have a message class:

Public Class Message

    'Properties
    Public MessageID As Integer
    Public Subject As String

    Public Sub New()
        MessageID = 0
        Subject = ""
    End Sub

    ''' <summary>Retrieve message content</summary>
    ''' <param name="MessageID">ID of the message</param>
    Public Sub New(ByVal MessageID As Integer)
        Dim ObjMessage As Message = MyData.GetMessage(MessageID)
        MessageID = ObjMessage.MessageID
        Subject = ObjMessage.Subject
    End Sub

    ''' <summary>Deletes a message from database</summary>
    Public Sub Delete()
        MyData.DeleteMessage(MessageID)
    End Sub

End Class

I would use this class for example to delete a message by using the following code in my page:

Dim Message As New Message
Message.MessageID = 1
Message.Delete()

But if I forget to set Message.MessageID = 1, the Delete method wouldn't work. Would there be a way to say MessageID has to be mandatory (except for the obvious if<>then check in the delete method or setting a MessageID property in the delete method)?

Kind regards,

Martin


Reply With Quote
  #2 (permalink)  
Old August 8th, 2007, 08:28 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What mechanism would you expect would tell you some internal value is mandatory? You have already listed the most practical ways of enforcing values.

This is a little confusing. If you create a new instance of your class with the default constructor, then MessageID will be 0.

What happens when you try to delete message 0? Does it break?
Could you throw an exception if you try to delete message without setting MessageID to something other than 0?
If you want to enforce the class having a message ID, then why not remove the default constructor so the caller is forced to provide one to the constructor?

-Peter
Reply With Quote
  #3 (permalink)  
Old August 8th, 2007, 08:58 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

Thanks for your help,
Indeed the example is a bit confusing, as I tried it to make it a bit to simple. I hope a better example is:

Public Class Message

    'Properties
    Public MessageID As Integer
    Public Subject As String

    Public Sub New()
        MessageID = 0
        Subject = ""
    End Sub

    ''' <summary>Retrieve message content</summary>
    ''' <param name="MessageID">ID of the message</param>
    Public Sub New(ByVal MessageID As Integer)
        Dim ObjMessage As Message = MyData.GetMessage(MessageID)
        MessageID = ObjMessage.MessageID
        Subject = ObjMessage.Subject
    End Sub

    ''' <summary>Deletes a message from database</summary>
    Public Sub Delete()
        MyData.DeleteMessage(MessageID)
        MessageListReceivedCacheDelete()
    End Sub

    Private Sub MessageListReceivedCacheDelete()
        If Not (CurrentCache(("MessageListReceived" & OwnerID.ToString())) Is Nothing) Then
            CurrentCache.Remove(("MessageListReceived" & OwnerID.ToString()))
        End If
    End Sub

End Class

And then I could call:

Dim Message As New Message
Message.MessageID = 1
Message.Delete()

Indeed like you say, when MessageID would be 0, there would be no problem, no message is removed. But when set MessageID = 1 and forget to set the OwnerID, the Cache method doesn't work properly (doesn't do what it's supposed to do). Indeed I could remove the default constructor, but then errors would only hit at runtime. My problem here is that Visual Studio doesn't warn you when you forget to set such parameters (I think). Do you know a solution to that?

Kind regards,

Martin

Reply With Quote
  #4 (permalink)  
Old August 8th, 2007, 07:21 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by dhmderuiter

Indeed I could remove the default constructor, but then errors would only hit at runtime.
What do you mean? If you have only a constructor that expects value(s) then your code must call it and pass in some value. Presumably, you would not allow your code to pass in a value that hasn't be obtained from somewhere useful. Thus you ensure a properly constructed class.
Quote:
quote:Originally posted by dhmderuiter
 My problem here is that Visual Studio doesn't warn you when you forget to set such parameters (I think). Do you know a solution to that?

Sort of: use a constructor that expects all the required values. If they are so important, why would you ever allow a default constructor that doesn't set them? (Apart from when you need to serialize a class. This requires a default, i.e. empty, constructor.)

-Peter
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object variable or With block variable not set haidee_mccaffrey Classic ASP Professional 5 March 8th, 2007 03:34 PM
Object Variable or With Block Variable Not Set Iashia06 Access 1 May 22nd, 2006 10:24 AM
Object Variable or With Block Variable not Set Parbish BOOK: Beginning VB.NET Databases 5 July 13th, 2005 04:03 AM
Object variable or With block variable not set tparrish VB Databases Basics 1 May 25th, 2005 10:25 AM
Object variable or with block variable not set spacy ASP.NET 1.x and 2.0 Application Design 0 September 21st, 2004 12:19 AM



All times are GMT -4. The time now is 06:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.