By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,558 Members | 1,673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,558 IT Pros & Developers. It's quick & easy.

Is there a graceful way to set the upperbound on an array without first raing an OutOfMemory exception

P: n/a
I recently had need to write a function that could (potentially) create
a very large array of integers. Since .NET assumes that memory is
infinite, I was wondering if there was a way to set a safe maximum
upper bound in code for use in the class constructor. Also, in c++ one
is able to assign all the memebrs of an array to a value without
listing the full array (i.e. with a simple =0); is such a thing do-able
in VB.NET?

here is the function (maximum is a private integer member,
internalArray is a private integer array)

Public Sub New(ByVal UpperLimit As Integer)
If ((UpperLimit > Integer.MaxValue) OrElse (UpperLimit < 2)) Then
Throw New IndexOutOfRangeException("Invalid value passed to
constructor " & Me.GetType().FullName())
Exit Sub
End If

maximum = UpperLimit
ReDim internalarray(maximum)
For nloop As Integer = minimum To maximum
internalarray(nloop) = nloop 'array is 0-based
Next
End Sub

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Since .NET assumes that memory is
infinite, I was wondering if there was a way to set a safe maximum
upper bound in code for use in the class constructor.
Not sure what you mean by safe, but the answer is most likely no.

Also, in c++ one
is able to assign all the memebrs of an array to a value without
listing the full array (i.e. with a simple =0); is such a thing do-able
in VB.NET?
No, you'll have to use a loop.

here is the function (maximum is a private integer member,
internalArray is a private integer array)

Public Sub New(ByVal UpperLimit As Integer)
If ((UpperLimit > Integer.MaxValue)


That's a meaningless check - an integer can never have a value that's
larger than the maximum value.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
thanks for the reply

the last line that you highlight is a place-holder for a better
check-value...

I'm still digging around for a way to dynamically set this... if I
find one, I'll post it here.

cheers

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.