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

Class and array properties

Is there a way to dynamically resize an array along with maintaining
its contents similar to VB's REDIM PRESERVE statement in C#? My
problem is, the user won't be sure how big the string array (sTest) is
initially. As stated in the code below, when the class is
instantiated, the string array is initialized to null. Is it possible
to have the user "set" a value in the array and the object would resize
the array automatically? For instance:

using System;

namespace Class1
{
public class Class1
{
private string[] sTest;

public string[] Test
{
get { return this.sTest; }
}

public UserProxy()
{
this.sTest = null;
}
}
}

Class1 tmp = new Class1();

tmp.Test[0] = "TEST";

I want the tmp object to be able to automatically create/resize its
sTest array and/or overwrite the value at the current location
specified, without destroying any other value in the rest of the array?
In the set accessor, how can I "see" what index the user specified?
Is what I ask even possible or is there an easier way?

Any help would be greatly appreciated.

Thanks,
Exitus

Aug 23 '05 #1
4 1726
I've always been a fan of creating a custom class that inherits from
System.Collections.CollectionBase

You can implement whichever sets of functionality you want, and type
your collections items however you want such as: Add(), Remove(),
RemoveAt(), IndexOf(), etc...

I try to only use regular arrays for quick processing/parsing. For more
complex data structures I usually stick with the CollectionBase base
class.

Aug 23 '05 #2
<ex****@military.com> wrote:
Is there a way to dynamically resize an array along with maintaining
its contents similar to VB's REDIM PRESERVE statement in C#?
No. Even VB.NET's REDIM PRESERVE doesn't really resize the array - it
creates a new array of the bigger size, and copies everything into it,
just as you can do in C#. Note that any other variables which referred
to the original array still do.

However, it's easier to use an IList (eg ArrayList) in most cases. In
1.1 that's not a strongly-typed solution, but in 2.0 you can use
List<Foo> for a list of references to objects of type Foo.

<snip>
I want the tmp object to be able to automatically create/resize its
sTest array and/or overwrite the value at the current location
specified, without destroying any other value in the rest of the array?
In the set accessor, how can I "see" what index the user specified?
Is what I ask even possible or is there an easier way?


No, what's you're asking isn't possible - there's no set accessor
involved, it's just array access.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 23 '05 #3

Use an ArrayList and add Class1 objects to it. You can then add, search
and sort.

ex****@military.com wrote:
Is there a way to dynamically resize an array along with maintaining
its contents similar to VB's REDIM PRESERVE statement in C#? My
problem is, the user won't be sure how big the string array (sTest) is
initially. As stated in the code below, when the class is
instantiated, the string array is initialized to null. Is it possible
to have the user "set" a value in the array and the object would resize
the array automatically? For instance:

using System;

namespace Class1
{
public class Class1
{
private string[] sTest;

public string[] Test
{
get { return this.sTest; }
}

public UserProxy()
{
this.sTest = null;
}
}
}

Class1 tmp = new Class1();

tmp.Test[0] = "TEST";

I want the tmp object to be able to automatically create/resize its
sTest array and/or overwrite the value at the current location
specified, without destroying any other value in the rest of the array?
In the set accessor, how can I "see" what index the user specified?
Is what I ask even possible or is there an easier way?

Any help would be greatly appreciated.

Thanks,
Exitus

Aug 23 '05 #4
If you do want the speed benefits of an array, and actually feel like
writing all the manual code, I have a code example at
http://www.geekswithblogs.net/shriop.../19/39987.aspx .
Also, someone commented on there that there is a static method in 2.0
that will just do all the work for you called Resize, and that code is
there.

Bruce Dunwiddie
http://www.csvreader.com

Aug 24 '05 #5

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

Similar topics

24
by: downwitch | last post by:
Hi, I know this has been covered here and in the .public groups, but it seems like it's been a while, especially around here, so I just thought I'd ask again to see if anyone has figured out a...
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
8
by: Joe | last post by:
I have several classes which I need serialized. Here is their class definitions: public class TopContainer { private LevelTwoType m_levelTwo; public LevelTwoType LevelTwo { get {
2
by: David | last post by:
Hi all, I am fairly new to C#. so go easy on me :-) Anyhow, I have a class file that I have set up properties and a method. I am calling this class file directly from and aspx.cs file. So...
13
by: thomasp | last post by:
I have written my first Class and am posting it to this newsgroup with hopes that I can get some feedback on what is right and what is wrong with it. All comments are welcome, but more interested...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
3
by: Dan | last post by:
Is it possible to dynamically create and populate properties? Below is my class. I would like to define properties for each of the items in my two arrays without actually defining each one. (There...
3
by: Miro | last post by:
First off...thanks in advance for getting me this far. Sorry for all these class posts but im having a heck of a time here trying to get something to work, and have finally got it to work (...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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:
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...
0
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,...
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...

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.