Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2008, 12:17 AM
Authorized User
 
Join Date: Nov 2007
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];
}
 
Old January 18th, 2008, 12:21 AM
Friend of Wrox
 
Join Date: Oct 2007
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
 
Old January 18th, 2008, 12:30 AM
Authorized User
 
Join Date: Nov 2007
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;
        }
}
 
Old January 18th, 2008, 12:30 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old January 18th, 2008, 12:39 AM
Authorized User
 
Join Date: Nov 2007
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
 
Old January 18th, 2008, 01:11 AM
Authorized User
 
Join Date: Nov 2007
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
 
Old January 18th, 2008, 03:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old January 18th, 2008, 05:12 AM
Authorized User
 
Join Date: Sep 2007
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");
}
 
Old January 18th, 2008, 05:14 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old January 18th, 2008, 10:51 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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





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 [email protected] 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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.