Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 18th, 2008, 12:17 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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];
}
Reply With Quote
  #2 (permalink)  
Old January 18th, 2008, 12:21 AM
Friend of Wrox
 
Join Date: Oct 2007
Location: , , .
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

That is not possible
Instead you declare string arrary p
and access as p(0),p(1).....
like that

urt
Reply With Quote
  #3 (permalink)  
Old January 18th, 2008, 12:30 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
        }
}
Reply With Quote
  #4 (permalink)  
Old January 18th, 2008, 12:30 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old January 18th, 2008, 12:39 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #6 (permalink)  
Old January 18th, 2008, 01:11 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old January 18th, 2008, 03:47 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
Reply With Quote
  #8 (permalink)  
Old January 18th, 2008, 05:12 AM
Authorized User
 
Join Date: Sep 2007
Location: , , Finland.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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");
}
Reply With Quote
  #9 (permalink)  
Old January 18th, 2008, 05:14 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
Reply With Quote
  #10 (permalink)  
Old January 18th, 2008, 10:51 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by havey
 i'm use to a langure where one can
What language is that?

-Peter
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting onChange Event w/Dynamically Created Form rvanandel Javascript How-To 4 June 28th, 2007 08:10 AM
Setting Gridview Dynamically copelanda BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 February 21st, 2007 01:28 PM
Dynamically Setting X, Y Coordiantes of Element hugh@kmcnetwork.com Javascript 4 August 21st, 2005 05:01 AM
Subform - Dynamically Setting mnoon Access 14 March 13th, 2005 07:33 PM
Setting stylesheet for dynamically created object tgopal Javascript 2 September 6th, 2004 11:47 PM



All times are GMT -4. The time now is 09:16 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.