Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to assign array values C#

Status
Not open for further replies.

theogary

Programmer
Mar 3, 2003
99
US
I am defining my array incorrectly what is wrong with this code.I want to assign the array conditionally?

string[] FieldsToCheck;

switch (TableToUSe)
{
case "PROGRAM":
FieldsToCheck = {"street_address" , "street_number"};
//{"name,street_number","street_address","zip,inf_rate","inf_rate_pt","tod_rate","tod_rate_pt","prs_rate","prs_rate_pt","sch_rate","sch_rate_pt"}
break;
case "CHILD":
FieldsToCheck[] = { "enroll_startdt1","enroll_term_code1","enroll_enddt1","enroll_startdt0",
"enroll_term_code0","enroll_enddt0","enroll_term_code2","enroll_enddt2",
"enroll_dropdt2","birthdt","reas_care_code1","enroll_dropdt1","enroll_dropdt0"};
break;
case "CASEREC":
FieldsToCheck[] = {"pa_number","pa_suffix","pa_line","ssn_mother",
"ssn_father","reas_care_code1","claim_codesdt","status_code","recertmmyy",
"last_name","first_name","caseclose_code","comments1","comments2","comments3"};
break;
case "HOME":
FieldsToCheck[] = {"ssn_fid","prev2_pgm","last_name","first_name","street_number",
"street,apt","zip,inf_rate","inf_rate_pt","tod_rate","tod_rate_pt","prs_rate",
"prs_rate_pt","sch_rate","sch_rate_pt","license_type","program_num3"};
break;
default:
return;
break;
}
 
Based on the named of the variables FieldsToCheck and TableToUSe, I'm assuming that you are then going to execute a dynamic sql statement against the database. If this is correct, I'd think twice about using this method and instead pass just one variable to your stored procedure which dictates which sql statement to run (with the "if" logic inside the sp).


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
The is no SQL involved. I just wan to assign fieldstocheck conditionally
 
I solved it I must define number of elements in array

switch (TableToUSe)
{
case "PROGRAM":
ls_value1 = NewVal["program_number"].ToString();
ls_value2 = "NULL";
ls_value3 = "NULL";
ls_value4 = "NULL";
FieldsToCheck = new string[2] {"street_address" , "street_number"};
//{"name,street_number","street_address","zip,inf_rate","inf_rate_pt","tod_rate","tod_rate_pt","prs_rate","prs_rate_pt","sch_rate","sch_rate_pt"}
break;
case "CHILD":
ls_value1 = NewVal["case_number"].ToString();
ls_value2 = NewVal["child_num"].ToString();
ls_value3 = "NULL";
ls_value4 = "NULL";
FieldsToCheck = new string[13] { "enroll_startdt1","enroll_term_code1","enroll_enddt1","enroll_startdt0",
"enroll_term_code0","enroll_enddt0","enroll_term_code2","enroll_enddt2",
"enroll_dropdt2","birthdt","reas_care_code1","enroll_dropdt1","enroll_dropdt0"};
break;
case "CASEREC":
ls_value1 = NewVal["case_number"].ToString();
ls_value2 = "NULL";
ls_value3 = "NULL";
ls_value4 = "NULL";
FieldsToCheck = new string[15]{"pa_number","pa_suffix","pa_line","ssn_mother",
"ssn_father","reas_care_code1","claim_codesdt","status_code","recertmmyy",
"last_name","first_name","caseclose_code","comments1","comments2","comments3"};
break;
case "HOME":
ls_value1 = NewVal["home_id"].ToString();
ls_value2 = "NULL";
ls_value3 = "NULL";
ls_value4 = "NULL";
FieldsToCheck = new string[16]{"ssn_fid","prev2_pgm","last_name","first_name","street_number",
"street,apt","zip,inf_rate","inf_rate_pt","tod_rate","tod_rate_pt","prs_rate",
"prs_rate_pt","sch_rate","sch_rate_pt","license_type","program_num3"};
break;
default:
return;
break;
}
 
when defining an array you need to supply the number of elements either as the indexer or in a list of options.
Code:
string[] array = new string[] {}; //zero length
string[] array = new string[0]; //zero length

string[] array = new string[] { null }; //1 element
string[] array = new string[1]; //1 element
your orginal code was missing new string[]

just a thought, but you may want to consider using a stragety here instead of a swich statment. it would make the code easier to read.
1. create an object to hold values 1 - 4 and the string array.
2. populate 5 new objects. one for each case and an empty dto for object for the default.
3. load these objects into a dictionary using the string as a key. then simply return the corrosponding dto from the list. example
Code:
public class DTO
{
   private string value1;
   private string[] array;

   public DTO(string value1, string[] array)
   {
      this.value1 = value1;
      this.array = array;
   }

   public string Value1 { get { return value1; } }

   public string[] Array { get { return array; } }
}
Code:
IDictionary<string, DTO> strategies = new Dictionary<string, DTO>();
public void LoadStrageties()
{
   strategies["A"] = new DTO("Foo", new string[] { "a" });
   strategies["b"] = new DTO("Bar", new string[] { "a", "b" });
}

public DTO GetDTOBy(string key)
{
   if (strategies.Keys.Contains[key]) return strategies [key];
   return new DTO(string.Empty, new string[0]);
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top