declaring a variable dynamically

M

miladhatam

how can i declare a varible dynamically
like this :

for (i=1 ; i<10;i++){

int ("j" + i) = i ;
//declaring j1 - j9
}

ofcourse this code is wrong and it is an algorithm
 
N

Nathan Sokalski

I don't believe you can declare variables dynamically, but it sounds like
either an array or some type of collection should be able to satisfy your
needs. The only thing you need to remember is that you must declare the
array or collection outside of the loop, but you are not required to specify
the dimensions or length in the declaration. Hopefully this helps.

Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/
 
A

Anthony Jones

how can i declare a varible dynamically
like this :

for (i=1 ; i<10;i++){

int ("j" + i) = i ;
//declaring j1 - j9
}

ofcourse this code is wrong and it is an algorithm

You're in need of an array. First though arrays start at 0 not 1:-

int[] j = new int[9];

for (i=0; i <9; i++)
{
j = i;
}

Response.Write(j[5]);

this would send 5 to the browser.
 
M

miladhatam

hi
yes i put my variable in a array
ofcourse it differ with my thinking
my way was like java script or action script in flash :)
i need something like this
protected void Page_Load(object sender, EventArgs e)
{
* HyperLink[] obj = { new HyperLink(), new HyperLink(), new
HyperLink(), new HyperLink() };*
for (int i = 0; i < obj.Length; i++)
{
obj.ID= i.ToString();
obj.Text = i.ToString();
obj.NavigateUrl = i.ToString() + ".aspx";
obj.BorderStyle = BorderStyle.Ridge;
Controls.Add(obj);
Response.Write("<br />");
}
}
ofcourse i have a problem again , in the line with " * "
how can i change the size of array dynamically ( i have error )
 
B

bruce barker

in .net Array are fixed size, you can not change them, only create a new one.
you want a List in c#. Ther is no associative array support like javascript,
but if you need a key, data use a dictionary or hashtable.


HyperLink[] obj = new HyperLink[] {
new HyperLink(), new HyperLink(),
new HyperLink(), new HyperLink()
};

or

HyperLink[] obj = {
new HyperLink(), new HyperLink(),
new HyperLink(), new HyperLink()
};

or

var obj = new HyperLink[] {
new HyperLink(), new HyperLink(),
new HyperLink(), new HyperLink()
};

but this is probably what you are typing to do:

var listSize = 4;
for (var i = 0; i < listSize; i++)
{
var link = new HyperLink();
link.ID= "Link" + i.ToString(); // numbers not valid in html
link.Text = i.ToString();
link.NavigateUrl = i.ToString() + ".aspx";
link.BorderStyle = BorderStyle.Ridge;
Controls.Add(link);
Controls.Add(new HtmlGenericControl("br"));
}


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top