|
 |
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |
|

January 18th, 2008, 12:17 AM
|
Authorized User
|
|
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
setting public strings dynamically
Hi if i have public string names like
public string p1,p2,p3,p4,p5,p6,p7,p8;
how could one set the values to such in a for loop? is it possible?
i've tried things like:
for (int i = 0; i <= 8; i++)
{
(p+(i)).ToString = myArr[i, 0];
}
|

January 18th, 2008, 12:21 AM
|
Friend of Wrox
|
|
Join Date: Oct 2007
Location: , , .
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
That is not possible
Instead you declare string arrary p
and access as p(0),p(1).....
like that
urt
|

January 18th, 2008, 12:30 AM
|
Authorized User
|
|
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the reason they are public string cause i need to display the values in the html page, and i know there are better way to set html values but, i cannot change the html with ease. this is the code that pertains to my first post but instead of p1,p2 i have aller_medName0,aller_medName1
Any suggestions, there must be a creative work around
for (int i = 0; i <= myAll.Length; i++)
{
switch (myAll[i, 2] as string)
{
case "MED":
aller_medName+i.ToString() = myAll[i, 0];
aller_medReact+i.ToString() = myAll[i, 1];
break;
case "FOO":
aller_fooName + i.ToString() = myAll[i, 0];
aller_fooReact + i.ToString() = myAll[i, 1];
break;
case "ENV":
aller_envName + i.ToString() = myAll[i, 0];
aller_envReact + i.ToString() = myAll[i, 1];
break;
}
}
|

January 18th, 2008, 12:30 AM
|
 |
Friend of Wrox
Points: 16,481, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
As urt suggests, this is what an array is for. And Seeing as this is a C# forum, here's the suggestion in C#:
public string[] p = new string  ; //arrays are 0 based, so 0-8 makes 9 items
for(int i=0; i<=8; i++){
p[i] = myArr[i,0];
}
That will assign the value from myArr to the item i in the array p.
The assignment "x.ToString = ..." doesn't work. ToString is a method that all objects have. It returns (typically) the object's value as string. You can not assign a value to it. If you have an array of strings into which you want to assign non-string values you would do this:
p[i] = myVariable.ToString();
In your case, the loop code would probably be this:
for(int i=0; i<=8; i++){
p[i] = myArr[i,0].ToString();
}
Long story short, you can't access multiple variables dynamically by constructing their names on at runtime, this is the purpose of arrays and collections/lists.
-Peter
|

January 18th, 2008, 12:39 AM
|
Authorized User
|
|
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the info, very informative "you can't access multiple variables dynamically by constructing their names on at runtime",
i'm use to a langure where one can
Thanks!:D
|

January 18th, 2008, 01:11 AM
|
Authorized User
|
|
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
one last quest, if yous wouldn't mind
what about looping a senerio like so:
if (aller[0].ToString() != Request.Form["aller0"].ToString()) { cmd.Parameters.Add("@aller0", SqlDbType.NVarChar).Value = Request.Form["aller0"].ToString(); }
if (aller[1].ToString() != Request.Form["aller1"].ToString()) { cmd.Parameters.Add("@aller1", SqlDbType.NVarChar).Value = Request.Form["aller1"].ToString(); }
if (aller[2].ToString() != Request.Form["aller2"].ToString()) {
....
could this be possible in a loop ?
something like?:
for (int i = 0; i <= 26; i++)
{
if (aller[i].ToString() != Request.Form["aller[i]"].ToString()) { cmd.Parameters.Add("@aller[i]", SqlDbType.NVarChar).Value = Request.Form["aller[i]"].ToString()); }
}
ALSO can using the SqlDataReader rdr be used with the public array something like,
Anam[i] = (string)rdr1["Anam[i]"];
Thanks again
|

January 18th, 2008, 03:47 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Because Request.Form takes a string parameter then yes you can do this, but not how you've just don it:
for (int i = 0; i <= 26; i++)
{
if (aller[i].ToString() != Request.Form["aller"] + i.ToString())
{
cmd.Parameters.Add("@aller" + i.ToString(), SqlDbType.NVarChar).Value = Request.Form["aller"] + i.ToString());
}
}
/- Sam Judson : Wrox Technical Editor -/
|

January 18th, 2008, 05:12 AM
|
Authorized User
|
|
Join Date: Sep 2007
Location: , , Finland.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It is possible with reflection:
Code:
class Foo {
string p1, p2, ..., p9;
}
...
Foo foo = new Foo();
Type type = foo.GetType();
for (int i = 0; i < 10; i++)
{
FieldInfo fi = type.GetField(String.Format("p{0}", i));
fi.SetValue(foo, "foobar");
}
|

January 18th, 2008, 05:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Quote:
quote:Originally posted by planoie
public string[] p = new string ; //arrays are 0 based, so 0-8 makes 9 items
|
C# uses the declaration to specify length so new string  is an array of length with elements 0 - 7.
What you wrote is true for VB and VB.NET.
--
Joe ( Microsoft MVP - XML)
|

January 18th, 2008, 10:51 AM
|
 |
Friend of Wrox
Points: 16,481, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Quote:
quote:Originally posted by havey
i'm use to a langure where one can
|
What language is that?
-Peter
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |