declaring a variable dynamically

  • Thread starter Thread starter miladhatam
  • Start date Start date
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
 
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/
 
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.
 
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 )
 
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)
 
Back
Top