473,657 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug with Array.CreateIns tance() when lower bound > 0

If you create an array using Array.CreateIns tance() and use a lower bound >
0, you apparently get an array of the wrong type.

int[] lenghts = new int[] {1};
int[] lowerBounds = new int[] {1};
string[] ar = (string[])Array.CreateIn stance(typeof(s tring), lengths,
lowerBounds);

Throws an InvalidCastExce ption because "string[*]" cannot be casted to
"string[]".

int[] lenghts = new int[] {1};
int[] lowerBounds = new int[] {0};
string[] ar = (string[])Array.CreateIn stance(typeof(s tring), lengths,
lowerBounds);

This works fine. The only difference in the lower bound.

int[] lenghts = new int[] {1,1};
int[] lowerBounds = new int[] {1,1};
string[,] ar = (string[,])Array.CreateIn stance(typeof(s tring), lengths,
lowerBounds);

Even this work fine. The difference is the number of dimensions. So the
problem cannot be just the lower bound 0 because in this case (and with
more dimensions too) it works well.

Everything works well in .NET 1.1. So it's apparently a bug introduced in
..NET 2.0.

Has anyone seen this before? And what is "string[*]"?

Regards,
Gianluca
Nov 18 '06 #1
2 3115
Gianluca <gi******@nospa m.comwrote:
If you create an array using Array.CreateIns tance() and use a lower bound >
0, you apparently get an array of the wrong type.
Well, sort of.

Internally, .NET has two types of array - "vectors" (single-
dimensional, 0-based) and more general arrays.

When C# uses a single-dimensional array, it *assumes* it's one of these
"vector" types and that's what it really casts it to.

If you need to use a single-dimensional array with a non-zero lower
bound in C#, you need to access it using the Array class (and IList
etc).
Everything works well in .NET 1.1. So it's apparently a bug introduced in
.NET 2.0.
Hmm... I thought I'd seen exactly the same behaviour in 1.1...
unfortunately I can't easily test it any more.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 19 '06 #2
Yep. I know about the internal difference between the vector and the array.
And you are right that it doesn't work with .NET 1.1 as well. I was testing
MD arrays and it also works if you cast it in the immediate window.

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Gianluca <gi******@nospa m.comwrote:
If you create an array using Array.CreateIns tance() and use a lower
bound >
0, you apparently get an array of the wrong type.

Well, sort of.

Internally, .NET has two types of array - "vectors" (single-
dimensional, 0-based) and more general arrays.

When C# uses a single-dimensional array, it *assumes* it's one of these
"vector" types and that's what it really casts it to.

If you need to use a single-dimensional array with a non-zero lower
bound in C#, you need to access it using the Array class (and IList
etc).
Everything works well in .NET 1.1. So it's apparently a bug introduced
in
.NET 2.0.

Hmm... I thought I'd seen exactly the same behaviour in 1.1...
unfortunately I can't easily test it any more.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 19 '06 #3

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

Similar topics

8
427
by: User | last post by:
Hi, This is very basic, It may be a repost, if so I'm sorry. The problem is that this declaration : Private strMyArray(100) As String will create an array of string with a length of 101, but the length should be only of 100 (0 to 99).
4
2257
by: Mark Gibson | last post by:
Hi, I've been playing about with array's, and found the concat operator '||' quite useful, apart from the fact that prepending an element places it in a lower subscript. Is there a way of either: a) prepending an element, but shifting existing elements up a subscript, so that the lower bound remains the same? eg: 1 ||> ARRAY -- new operator ||> shift array and prepend
8
1234
by: Paul in Toronto | last post by:
Got this assignment in my VB .NET class. The program's basically a picture viewer that lets you add your pictures to an array so you can cycle through them once the file's been opened. So you open the file, it displays, then you can select "add to list" from a menu. Anyway, I'm pretty sure the filenames are being added to the array, and I can go directly to the first and last images in the array by using the appropriate menu options, but...
4
1214
by: Ben Voigt | last post by:
I am accustomed to using a pointer to traverse an array in native C++. Here I am trying to copy a slice of a native 2-D array into a managed 2-D array to make it accessible by C#. But piData isn't incremented and no value is stored in m_aaSamples. Anyone know why? m_aaSamples = new System::UInt16; /* snip */ for( unsigned iLead = 0; iLead < NUM_LEADS; iLead++ ) { short* piData = g_data + BUFFER_SAMPLES * iLead + BLOCK_SAMPLES *...
8
1855
by: zulander | last post by:
is there a way to find out if the element exist ? dim myarray() as string dim mytxt as string mytxt ="Superman1,Superman2,Superman3,Superman4" myarray=mytxt.split(",") debug.print(myarray(4))
26
2729
by: MLH | last post by:
How would I modify the following to achieve a 2-dimensional array? Dim MyWeek, MyDay MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") ' Return values assume lower bound set to 1 (using Option Base ' statement). MyDay = MyWeek(2) ' MyDay contains "Tue". MyDay = MyWeek(4) ' MyDay contains "Thu". In other words, I want the array to hold
11
2419
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the following. I noticed that if I set: int upper = strings.GetUpperBound(0); I never match the last element in the array (i.e. "iii")
7
7085
by: yhlove | last post by:
Hi I declared an array as follows: dim a as integer() somewhere else in my program I have the following command: redim preserve a(size) I want to check the array length like that if a.length 1 then
10
3363
by: Alan Mosley | last post by:
I have a multi dimensional array, I want to copy, but when I do It shows 0's in the second dimension. Can I copy a multi dimensional array? if so how do i do it. Thanks
0
8413
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...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8513
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
8617
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
7352
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
5642
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
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1733
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.