473,382 Members | 1,622 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,382 software developers and data experts.

array conversions

Why does the following code throw an exception?

Dim o1() As Object = New Object() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

Following is a snippet from VB.NET language specification on MSDN.
Nov 21 '05 #1
4 1215
Sankar,

Is the sample not this
Dim s() As String
Dim o1() As Object = _
New String() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

In my opinion are you trying to cast some arrays containing different
objects.
Not one object to a given type.

I hope this helps?

Cor
Nov 21 '05 #2
Sankar,

Is the sample not this
Dim s() As String
Dim o1() As Object = _
New String() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

In my opinion are you trying to cast some arrays containing different
objects.
Not one object to a given type.

I hope this helps?

Cor
Nov 21 '05 #3
"Sankar Nemani" <sn*****@nospamlumedx.com> schrieb:
Why does the following code throw an exception?

Dim o1() As Object = New Object() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

This won't work. You'll have to cast each item in the array individually.

\\\
Dim o1() As Object = {"Sankar", "Sankar2"}
Dim s1(o1.Length - 1) As String
For i As Integer = 0 To o1.Length - 1
s1(i) = DirectCast(o1(i), String)
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
"Sankar Nemani" <sn*****@nospamlumedx.com> schrieb:
Why does the following code throw an exception?

Dim o1() As Object = New Object() {"Sankar", "Sankar2"}
s = DirectCast(o1, String())

This won't work. You'll have to cast each item in the array individually.

\\\
Dim o1() As Object = {"Sankar", "Sankar2"}
Dim s1(o1.Length - 1) As String
For i As Integer = 0 To o1.Length - 1
s1(i) = DirectCast(o1(i), String)
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
14
by: blue | last post by:
Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap);" where N is the size of...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
17
by: eBob.com | last post by:
I know it wouldn't be hard to write, but I have been unable to find a method which will take elements of a character array and concatenate them into a string. I've looked at both String members...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...

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.