473,320 Members | 1,879 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,320 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 1426
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.