Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Checking for initialised variables (VB .NET 2003)

    I am creating class modules to allow manipulation of data tables in our Oracle database. I don't like the way that the IDE creates strongly typed dataset objects so I've written a program to create classes of my own.

    One of the things I want to do is to check if a property corresponding to a dataset field has been initialised, especially in my AddNewRow method. I thought about checking if the variable within the class that holds the value for the field is set to Nothing but this won't work as Nothing corresponds to the default value for the variable. i.e For strings, Nothing equates to an empty string and for numbers Nothing equates to zero.

    The method I've used is to initialise, in my New method, the variables to their MinValue property. This works but isn't perfect as there is a possibility, however remote, of the variable actually being set to that value.

    Can anybody think of an elegant and foolproof way of testing if a property has been set?

    Regards,
    Kevin Bell

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for initialised variables (VB .NET 2003)

    Hi Kevin,

    Can you provide more details about the class that's holding your data? Is it inheriting from another class, like the DataSet?

    When using a DataSet object (created in code - not in the designer), the default unassigned value is DbNull.Value rather than Nothing ("" or 0, depending on type).

    If that's not what you're looking for, you may consider creating a property for each field to indicate that a value has been assigned. You could then fire an event that sets the property when each field is initialized.

    Hope this helps

  3. #3
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Checking for initialised variables (VB .NET 2003)

    The classes don't inherit from anything Mark. Basically I want to create classes that expose the fields of a table as properties so my programmers and me can reap the benefits of Intelisense during programming.

    I've attached the class module for a table called TRX. s you can see, the internal variables holding the property values, all begining with "m_", correspond to fields in the table.

    The methods DoSelect, NextRow and PreviousRow populate these variables and hence the properties. My problem comes with the AddNewRow method where I want to check that all the variables/properties have been set before executing the Insert Into statement.

    In a nutshell, I want to be able to know is a property has been initialised. I could do this by creating a Boolean variable for each property and setting it to True in the Set statement of the property. Waht I was looking for is a method of using 'Nothing' for a variable in the same way that 'Nothing' is used for Objects.

    I hope this explains what I wanted to do. This project is still in the development stage but seems to be useful. My programmers are happy with the classes and say it makes it easier and quicker to program as opposed to using the 'Item("ItemName")' construct to reference table items.

    Regards,
    Kevin Bell

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for initialised variables (VB .NET 2003)

    Thanks for posting the sample - that helped me understand the situation.

    Rather than using the .NET data types (String, Integer, Double, Date) you can use the Oracle data types (OracleString, OracleNumber, OracleDateTime). Rather than initializing to String.Empty, Integer/Double.MinValue or Date.MinValue, you can initialize to OracleString.Null, OracleNumber.Null and OracleDateTime.Null. Since there is also an Empty and MinValue (or Zero) value for the Oracle data types, you should be able to easily detect whether an assignment has been made or not.

    Comparing against String.Empty and Integer.MinValue is dangerous because in some cases those could be legitimate values (although, perhaps your situation is different).

    Hope this helps

  5. #5
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for initialised variables (VB .NET 2003)

    We've created our own data types for this purpos. It gives us a little more flexibility for some situations.

    I have heard that the next version of .Net will provide Nullable datatypes as a part of the languiage.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  6. #6
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking for initialised variables (VB .NET 2003)

    Ahh - the fond memories of using IsNull()

    <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  7. #7
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Checking for initialised variables (VB .NET 2003)

    Thanks for the post Mark. I'll have to think about using the Oracle data types. I wonder if the casting between them and regular .NET data types will cause any problems or performance issues. I don't want to force the programmers to change the data types they use in their programs in case it creates another dimension of possible errors.

    I think I'll use the method I suggested before and include local boolean variables cortresponding to the properties. It may not be as elegant as using the Oracle data types but I think the programmers will appreciate not having to change the programs and after all, it will be transparant to them. It's also a trivial thing to add to my class generation program.

    I too wish for the days of IsNull.

    Regards,
    Kevin Bell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •