473,799 Members | 3,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Exception when using reference-passed int as array index

Hi all,
maybe you'll see the trick and could help me. I am passing int by
reference and get an exception when using it as an index to passed
array. (Yes I know I can workaround with object encapsulation, but
wish to understand this). Thanks, Jan

[code]

using System;

public class TestPassByRef {
public void printArray(stri ng[] a) {
p = new int(0);
printArray(a, ref p);
}
private void printArray(stri ng[] a, ref int p) {
if (p < 0 || p >= a.Length) return;
Console.Write(" {0}:", p);
Console.Write(" ({1}) ", a[p]); // here it breaks .. see output
below
p++;
printArray(a, ref p);
}
}

public class Test {
static void Main() {
string[] a;
TestPassByRef t = new TestPassByRef() ;
a = new string[] {"A","B","C","D ","E"};
t.printArray(a) ;
}
}

[output]

0:
Unhandled Exception: System.FormatEx ception: Index (zero based) must
be greater than or equal to zero and less than the size of the
argument list.
at System.Text.Str ingBuilder.Appe ndFormat(IForma tProvider provider,
String format, Object[] args)
at System.String.F ormat(IFormatPr ovider provider, String format,
Object[] args)
at System.IO.TextW riter.Write(Str ing format, Object arg0)
at System.IO.SyncT extWriter.Write (String format, Object arg0)
at TestPassByRef.p rintArray(Strin g[] a, Int32& p)
at TestPassByRef.p rintArray(Strin g[] a)
at Test.Main()
Nov 16 '05 #1
1 1748
Am 12 Sep 2004 09:26:16 -0700 schrieb Jan Killian:
Console.Write(" ({1}) ", a[p]); // here it breaks ..

^
Console.Write(" ({0}) ", a[p]);

Till Meyer
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
9598
by: Michael Winter | last post by:
In an attempt to answer another question in this group, I've had to resort to calling the DOM method, Node.removeChild(), using a reference to it (long story...). That is, passing Node.removeChild. In Opera (7.23/Win), the call appears to do nothing - the node remains - but no errors are shown. In Netscape (7.0/Win), an exception results. On IE (6.0/Win), the node is removed. Strangly, if I pass another function reference, say...
11
3278
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two "resources" I use are memory and file I/O and whenever there is a memory allocation failure or file I/O failure I just simply call a custom assert-type function to check, print a error message and abort. This seems to be OK for now (for the...
44
4233
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
3
1798
by: Chris Baldwin | last post by:
Hello, I'm having a problem with a mixed managed/unmanaged DLL. When I attempt to reference "Exception" in my VB.NET code, it conflicts with some other "exception" that gets somehow gets into the assembly. This code reproduces the issue: #using <System.dll> // #include <vector> is in stdafx.h
0
290
by: Matt | last post by:
Hi all, I'm having a very strange problem. The third line in the following code gives me a null reference exception when used in an ASP.Net web page. The code is called on the click of a button. Public Overridable Property SDSVarsSetup() As Boolean Get If Not IsNothing(Session("SDSVarsSetup")) Then ' <-- **Exception occurs here**
7
2707
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote server returned an error: (500) Internal Server Error." I found a post that suggested to catch the WebException to retrieve the actual HttpWebResponse object for more information. The response returned is shown below. At first I thought this was a...
3
9087
by: James | last post by:
Hi guys Do you ever get the exception in Managed C++? How can I know which file or dll is missed?
2
2892
by: Chris Stiefeling | last post by:
Hi, I am experiencing a strange problem. I am reading and writing xml files via XmlDocument and XmlTextWriter. In the debugger everything works fine but outside the debugger (debug or release) I receive the following error: "The type initializer for "System.Xml.Schema.Validator" threw an exception." I wrote a small console app that contains the problem -- I've just attached the default class which gets run. Output outside the...
5
1385
by: Sridhar | last post by:
Hi, I have created a web application. I have a data access dll to interact with database. I added this dll as a reference to the web application. Now let's say if I am calling a function in data access and if it throws exception (like cannot find stored procedure), how do I retrieve that exception message back in the web form? In the client, I am able to catch the exception, but the message I am getting is "Object reference not set to an...
1
2322
by: Tim_Mac | last post by:
hi, i am encountering a sporadic exception using crystal reports that is causing me grief, I can't 'handle' it... my code is loading a typed report, and loading a DataSet to it. unfortunately crystal reports customer support is very poor and their 'case management' tool refuses to function. i'm posting here in the hope that someone may have seen it before and worked around it. first of all, here is the exception:
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10243
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.