Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

Required fields, pointers and VB.net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick Stansbury - 05 Jan 2005 15:34 GMT
Hi,

   Given a set of objects like the following I have two questions - firstly
is the technique used below for initialising what are effectively constants
in Sub New() correct? Is there a "better" way of doing this? Secondly, I
know that the code below for Validate() , requiredProperties etc. will never
work - you can't put a reference to nothing into a collection. How could I
effect this - or similar functionality? What I really want to be able to do
is to put a bunch of pointers into an array and check in Validate() whether
they point to an instantiated object...

(The idea here being that I can abstract the logic for validating common
objects like these into a base class.)

Thanks,

Nick

Class Animal
   Public MaxAge as int
   Public TypeDescription as string
   Public RequiredProperties as Collection
   Public Sub Validate()
       for each Obj as Object in RequiredProperties
           if Obj is nothing then : throw new exception("Required property
not set :" & obj.tostring()
       next Obj
   End Sub
End Class
Class Dog : inherits Animal
   Public sub new()
       TypeDescription = "Dog's are a man's best friend"
       maxage = "14"
    end sub
   Public CollarSize as integer
End Class
Class Cat : inherits Animal
   Public Sub New()
       TypeDescription = "Nasty animals - not even good for eating"
       MaxAge = "18"
       RequiredProperties.add(HasStupidBellAroundNeck)
   end sub
   Public HasStupidBellAroundNeck as boolean
End Class
Ben Strackany - 05 Jan 2005 16:52 GMT
First off, constants values should not change. E.g. I notice in your code
that MaxAge is sometimes 18 and sometimes 14. That's not a constant --
that's a variable. So what you're doing is correct.

For actual constants, you should instead use

Public Const MaxAge as Integer = 18

Second, you're setting MaxAge (which is an integer) to "18". If there are
quotes around a value, that's a string. It'll work in VB, but only because
VB is so forgiving. You should read up on data types to make sure you don't
run into issues.

Lastly, you're adding boolean variables into your RequiredProperties
collection, not objects. So I don't know why you're checking for objects in
there. Maybe you should instead use a Dictionary object to store your
properties, then you can check by key whether something is added or not.

Signature

Ben Strackany
www.developmentnow.com

> Hi,
>
[quoted text clipped - 40 lines]
>     Public HasStupidBellAroundNeck as boolean
> End Class
Nick Stansbury - 06 Jan 2005 14:48 GMT
Dear Ben,
   Thank you for your response.

> First off, constants values should not change. E.g. I notice in your code
> that MaxAge is sometimes 18 and sometimes 14. That's not a constant --
> that's a variable. So what you're doing is correct.
> For actual constants, you should instead use

   Thank you for the clarification. I am clear on the difference - however
the example here appears to fall into the middle. It is a constant in that
across the class in question it is effectively constant - the effect I want
is to define a constant in the base class and set it's value in the
inherited classes.

> Second, you're setting MaxAge (which is an integer) to "18". If there are
> quotes around a value, that's a string. It'll work in VB, but only because
> VB is so forgiving. You should read up on data types to make sure you don't
> run into issues.

   It's a silly error - the code given was supposed to just illustrate a
point - I wrote it in OE... I didn't however know that VB was that
forgiving - how distinctly unhelpful of it.

> Lastly, you're adding boolean variables into your RequiredProperties
> collection, not objects. So I don't know why you're checking for objects in
> there. Maybe you should instead use a Dictionary object to store your
> properties, then you can check by key whether something is added or not.

   Again I think I mis-communicated. RequiredProperties could include
references to anything - not just booleans. But it is a valid point - I can
add a pointer to a string into the collection but not a pointer to an
underlying data type - how irratating.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.