Wrox Programmer Forums
|
.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 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
 
Old August 8th, 2007, 07:59 AM
Registered User
 
Join Date: Jun 2005
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


 
Old August 8th, 2007, 08:28 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old August 8th, 2007, 08:58 AM
Registered User
 
Join Date: Jun 2005
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

 
Old August 8th, 2007, 07:21 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.