Re: Java Null VS .NET Null

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Java only allows you to use null if you are using the wrapper classes (i.e.
treating primitive values as objects) - just like .NET Java primitive types
('int' etc) are stored on the stack and can't be set to null. So this is
really your option 2.

If you can wait for C# 2.0, it has nullable types, that do exactly what you
want (a value type that supports a HasValue option). However the database
api's may take a while to catch up.

Unless you planning to handle lots of in-memory data, I would still give
DataSets a look. They effectively use your option 2 (since all values are
objects). However if you use the tools to generate a strongly typed wrapper,
this will provide an interface like your option 3. If you try and access a
null property an exception will be thrown, so failures to test for null
fail-fast. Given the basic cost of storing all values as objects (which you
may have to do anyway), I don't think the DataSet memory structures are too
bad or too expensive.

In any case, my preference would always be for option 3, at least in terms
of the interface. That way the storage can be implemented in different ways
without the caller knowing. You can make the behaviour of the property when
reading nulls vary according to requirements (throw an excpeiton or return a
default value).

My 2 cents, anyway.

------
Nigel Norris


"David Jessee" <DavidJessee@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F862ABA5-62F8-45B8-AC49-761A7406B393@xxxxxxxxxxxxxxxx
> In Java, creating a class that represents a tuple in a database is
> relatively
> simple because if you have a field for, lets say, "Quantity" that is an
> integer, you can set it to null. Since .NET uses a stack for these Value
> Types, the integer (Int32) has an initial value of 0 and you can't set it
> to
> null.
>
> So, what's the best way to approach this without having to jump through a
> bunch of programmatic hoops? These are a couple of ways I've though of.
>
> 1) Create a constant that represents a null Int32, et and use that value
> (problem: you'd have to constantly check that value if you're doing
> calculations)
> 2) Make all of your properties be objects. That way you could set the
> property to an integer, or a null (or DBNull.Value). (problem:
> performance
> hit from boxing and potential errors when doing calculations)
> 3) Have a property for the field and then have an ancillary property names
> "FieldXIsNull" (problem: classes get bulky and you end up having to check
> the IsNull constantly)
>
> So, what are the best ways to approach this?
>
> Side Note, I'd like to stay away from using datasets because they're big
> memory hogs. If I can have a class that I can instantiate that keeps
> track
> of real values, I'll get better memory usage. As far as the overhead of
> coding all of these classes, I can use/create code generators for that.
>


.



Relevant Pages

  • Re: If Macs have no spyware....
    ... >had made a complete code review of its operating system and removed all ... and writing new data into those memory locations would ... >but when the data exists on the stack, it can cause very large problems. ... >location that needs to be written in place of the correct execution ...
    (comp.sys.mac.advocacy)
  • Re: If Macs have no spyware....
    ... First you yammer about being a Mac advocate, then bad mouth me for dumping XP in favor of a Mac. ... Supposedly Microsoft had made a complete code review of its operating system and removed all the buffers which could overflow. ... the fundamental problem is that the basic architecture of Windows has two fatal flaws in its memory management and while these remain in the software the ad hoc patches will never be enough to make Windows a secure operating system. ... These problems are bad enough when dealing with data in the one routine but when the data exists on the stack, it can cause very large problems. ...
    (comp.sys.mac.advocacy)
  • Re: Maybe we should stop "Paging Beth Stone" already...
    ... I'll want to work on my OS while running my OS, so the assembler that it's written with has to run under it. ... You have to swap CR3 if you want seperate memory spaces. ... The alternate stacks aren't used by the processor unless the task calls a different protection level, so they're not part of the TSS swap. ... This lets any application use up to a gigabyte of stack before Linux is forced to tell it that it's gone too far. ...
    (alt.lang.asm)
  • Re: When is "volatile" used instead of "lock" ?
    ... to get the address of a stack variable to a background thread. ... I'm suggesting that the memory model ... lock pattern works without making the instance member volatile; ... fields shared amongst more than one thread despite following the locking ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Resolving internal links
    ... You have to pass all workspace in via ... Which highlights another issue - you could not use Fortify (or similar code which uses these functions to replace the memory allocators) within the main application code unless you also used it within the module. ... At best you'll reference memory which does not exist and cause an abort. ... will only be able to use 256 bytes of stack space IIRC. ...
    (comp.sys.acorn.programmer)