473,397 Members | 2,084 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,397 software developers and data experts.

Help regarding class in c#

Hi all
i am having application in c#.
I want to create array of class object at runtime.
I don't want to set array bound at design time.
How to set array bound at runtime and how to access array element?
Can some one help me.
Thanks in advance.

Nov 17 '05 #1
3 1428
Hello

// creating array
int arraySize = 5;
object[] objectsArray = new object[arraySize];

// instantiation of each element
objectsArray[0] = "Hello";
objectsArray[1] = 10;
objectsArray[2] = 40.0;
objectsArray[3] = 15.0f;
objectsArray[4] = new Random( );

// working with elements
foreach (object o in objectsArray)
{
System.Diagnostics.Debug.WriteLine("object type: " +
o.GetType().ToString());
if (o is string)
System.Diagnostics.Debug.WriteLine("Hey, it's string: \"" + o.ToString() +
"\"");
}

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
<tr**************@yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi all
i am having application in c#.
I want to create array of class object at runtime.
I don't want to set array bound at design time.
How to set array bound at runtime and how to access array element?
Can some one help me.
Thanks in advance.

Nov 17 '05 #2
tr**************@yahoo.com wrote:
Hi all
i am having application in c#.
I want to create array of class object at runtime.
I don't want to set array bound at design time.
How to set array bound at runtime and how to access array element?
Can some one help me.
Thanks in advance.


I will assume I understand your questions. If not, please give us more
information and repost.

To construct an array:

Type <varname> = new Type[<length>];

example:

Int32[] values = new Int32[10];
// values now has indices 0 through 9 (10 elements)

To construct an array that doesn't start at index 0, you need to use a
method of the Array object:

Int32[] values = (Int32[])Array.CreateInstance(typeof(Int32),
new Int32[] { 10 }, new Int32[] { 1 });
// values now has indices 1 through 10

To store a value into the array:

values[5] = 23;

to retrieve a value from the array, just use values[x] wherever you need
to read the value at index x:

Int32 someValue = values[9];

If you want to resize the array, if that's what your question is, then
you need to allocate a new array of the new, correct, size, then copy
the elements from the old array to the new and replace the reference in
the variable with the new array, something like this (using the first,
0-based, example as a start):

Int32[] values = new Int32[10];
....
// "resize" to 20 elements
Int32[] newValues = new Int32[20];
Array.Copy(values, newValues, values.Length);
values = newValues;

Also, note that arrays containing objects (like class instances,
strings, etc.) as opposed to value types (Int32, DateTime, Boolean,
etc.) are defaulted to null, which means there are no objects stored in
the array to begin with. This means that the following will crash with a
null-reference exception:

String[] names = new String[10];
Int32 len = names[0].Length; // crash here, names[0] = null

Hope this answers some of your questions.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #3
Hi trialproduct2004,
if you want to have an array like structure where you don't want to
specify the bounds, you can use a System.Collections.ArrayList object. It
takes objects as input and returns objects, you just have to cast to your
desired type i.e.

string name = "mark";
Label tag = new Label();

ArrayList list = new Arraylist();
list.Add(name);
list.Add(tag);

string returnedName = (string)list[0];
Label returnedTag = (Label)list[1];
the Arraylist will grow as you keep adding more items. If you do not
specify an inital size it start with 16 elements, then once it needs more
space it will double in size to 32 then 64 etc to give you more space.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"tr**************@yahoo.com" wrote:
Hi all
i am having application in c#.
I want to create array of class object at runtime.
I don't want to set array bound at design time.
How to set array bound at runtime and how to access array element?
Can some one help me.
Thanks in advance.

Nov 17 '05 #4

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

Similar topics

1
by: Roy J | last post by:
Hello everyone:) My name is Roy, I am new to Java and I have problem regarding to arrays. if you have time and like to help, please help. I am trying to make a program to deal with prime...
2
by: Frank | last post by:
Hi everyone, I have a general class design question. I come from a VB5-6 background where I made applications that used an active x dll to access the database. In the VB world each class was in a...
8
by: Pete Davis | last post by:
First of all, I apologize for cross-posting to so many groups, but clearly there are only 2 people on the planet that understand MS Accessibility in ..NET and I'm hoping I might reach just one of...
4
by: trialproduct2004 | last post by:
Hi all i am having application in C#. here what i want it to update one datagrid depending on particular value. I want to start minimum of 5 threads at a time and all these threads are updating...
1
by: trialproduct2004 | last post by:
Hi all i am having problem in c# class. I have two classes one nexted inside another. What i want is to have one command function to be defined in outerlevel class and to be accessiable in nested...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
2
by: Chris | last post by:
Hi, I have been stuck trying to come up with a design for days. I am working on a small project regarding barcode and I want to implement a factory design. I am now confused. I decided factory...
23
by: casper christensen | last post by:
Hi I run a directory, where programs are listed based on the number of clicks they have recieved. The program with most clicks are placed on top and so on. Now I would like people to be apple to...
10
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
4
omerbutt
by: omerbutt | last post by:
hi there i am amking an inventory application with php,ajax,javascript,html,dhtml,sql the problem is that i have hidden the input fields regarding the replace no's of the parts with this line of...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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,...

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.