473,466 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

declaring a variable dynamically

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
Sep 12 '08 #1
5 1180
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
nj********@hotmail.com
http://www.nathansokalski.com/

<mi********@gmail.comwrote in message
news:27**********************************@z66g2000 hsc.googlegroups.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

Sep 12 '08 #2
<mi********@gmail.comwrote in message
news:27**********************************@z66g2000 hsc.googlegroups.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] = i;
}

Response.Write(j[5]);

this would send 5 to the browser.

--
Anthony Jones - MVP ASP/ASP.NET

Sep 12 '08 #3
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[i].ID= i.ToString();
obj[i].Text = i.ToString();
obj[i].NavigateUrl = i.ToString() + ".aspx";
obj[i].BorderStyle = BorderStyle.Ridge;
Controls.Add(obj[i]);
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 )
Sep 12 '08 #4
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)
"mi********@gmail.com" wrote:
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[i].ID= i.ToString();
obj[i].Text = i.ToString();
obj[i].NavigateUrl = i.ToString() + ".aspx";
obj[i].BorderStyle = BorderStyle.Ridge;
Controls.Add(obj[i]);
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 )
Sep 12 '08 #5
hi
thanks for your good help
i 'll test last one
Sep 13 '08 #6

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

Similar topics

2
by: Jim Hudon | last post by:
i need to create an array of a size determined by a non-const variable: int char sampleArray; why does the following not work, and what can i do: const int constArraySize = arraySize; int...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
4
by: Lucy | last post by:
In the Declarations section of a form's code module, what is the difference between the following: Dim Flag As Boolean Public Flag As Boolean Private Flag As Boolean Thanks!
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
1
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en...
3
by: mamun | last post by:
Hi all, I am trying to create variables dynamically. This is needed because the user interface can have ten different textboxes with name as txt1, txt2 and so on. I would like to get values of...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.