473,763 Members | 6,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1451
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.Diagnost ics.Debug.Write Line("object type: " +
o.GetType().ToS tring());
if (o is string)
System.Diagnost ics.Debug.Write Line("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******** *************@g 44g2000cwa.goog legroups.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.CreateIn stance(typeof(I nt32),
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(valu es, 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***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #3
Hi trialproduct200 4,
if you want to have an array like structure where you don't want to
specify the bounds, you can use a System.Collecti ons.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
3888
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 numbers, but I do not how to involve arrays with that. program generates prime numbers according to user input, and displays them in descending order. The user enters the number of prime numbers to be generated. The output is displayed 5 numbers per...
2
1201
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 seperate file and I was able to have a module that they all shared that had global variables, like my ado connection object. In C sharp it looks like all my class can be contained into one .cs file, so my question is where would I put any...
8
2581
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 them by cross posting to 6 groups because I've already posted to 4 others with no luck. Our company has a number of custom controls that we're building for a suite of applications. We're trying to add accessibility support to the controls and...
4
1324
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 datagrid in their thread proceudre. What i want is whenever 5 threads are running as soon as one thread finish its execution i want to asssing new thread proceudre to that thread. Can some one tell me how to do it?
1
259
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 class. Is it possible. Because my function is public but i am not able to access it in nested class. can some one help me. any help will be appreciated.
12
5342
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 the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
2
1396
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 pattern since I am dealing with EAN13, UPCA12 UPC8 etc. Can someone provide a small sample on how to start. I have looked at the factory implementation online but still can't figure it out. Can someone provide a sample jsut to start with.
23
2802
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 place a link on there site so people can vote for their program, "ad a click". eg someone clicks the link on some page and it counts +1 click on my page. if some one clicks the link below it will count a click on my page.
10
2118
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 #these are the operations + = , - = , * = , 1/ = only if 0 not in .
4
2026
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 code document.getElementById('bgrp1').style.visibility='hidden'; document.getElementById('lable_rp_no1').style.display='none'; document.getElementById('rp_no1').style.display='hidden'; the code for the html part is here
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
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,...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
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
5270
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...
1
3917
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
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.