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

  • Thread starter Thread starter stand__sure
  • Start date Start date
S

stand__sure

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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top