473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
2 1128
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jacob H | last post by:
Hello all, I'm nearing the completion of my first graphical console game and my thoughts have turned to the subject of gracefully handling runtime errors. During development I like to not handle...
20
by: MickeyBob | last post by:
How does one detect the EOF gracefully? Assuming I have a pickle file containing an unknown number of objects, how can I read (i.e., pickle.load()) until the EOF is encountered without generating...
3
by: Mike Schilling | last post by:
Instances of SystemOutOfMemoryException do not contain a stack trace. Easy test to verify this: class OOM { public static void Main() { try { Object arr = new Object; } catch...
0
by: Ramin Dalkouhi | last post by:
Hello, We are currently working on a serialization task which imports data into memory streams. At first, we had problems with a very high amount of data as the memory management of such memory...
4
by: Annoyed Programmer | last post by:
Maybe this question has been addressed before, but I just stumbled with the issue so I'll post it here. Since I can remember array declaration in any language I've worked(Basic/VB/VB.NET/C/C++)...
2
by: Ricky Chan | last post by:
Windows Server 2003 with 2G Ram ,IIS6 -> I have enabled /3gb switch in boot.ini ! -> Machine.config set to 80% memory Limit However, It still throw outofmemory exception when loading large...
1
by: Ricky Chan | last post by:
In the production environment, it always occurs and the worker process did not recycle automatically. Therefore, it make the system service break to client. In development environment, we write...
8
by: Mantorok | last post by:
Hi all I'm populating a DataTable with around 1,500,000 rows, the table contains 4 columns, 3 string columns and a decimal column. However, I keep getting OutOfMemory exceptions when my app...
3
by: Robert Bevington | last post by:
Hi all, I ran into memory problems while tying to search and replace a very large text file. To solve this I break the file up into chunks and run the search and replace on each chunk. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.