I have this piece of code that i would like to simplify because I know that i will be adding more checkboxes in the future. Can any one help me to simplify this? Also, I already declared the string strSubscription in above code.
//Assign database name to strings
if (chbAll1t60.Checked == true)
{
strSubscription += "xXpoOFAd";
if (chbAll60t70.Checked == true)
{
strSubscription += ":wE9z4HjJ";
}
else
{
if (chbHor1t60.Checked == true)
{
strSubscription += ":yRczF320";
}
else
{
if (chbHor60t70.Checked == true)
{
strSubscription += ":8qz1ExKb";
}
}
}
}
else
{
if (chbAll60t70.Checked == true)
{
strSubscription += "wE9z4HjJ";
if (chbHor1t60.Checked == true)
{
strSubscription += ":yRczF320";
}
else
{
if (chbHor60t70.Checked == true)
{
strSubscription += ":8qz1ExKb";
}
}
}
else
{
if (chbHor1t60.Checked == true)
{
strSubscription += "yRczF320";
if (chbHor60t70.Checked == true)
{
strSubscription += ":8qz1ExKb";
}
}
else
{
if (chbHor60t70.Checked == true)
{
strSubscription += "8qz1ExKb";
}
}
}
}
Loading
Blake ArnoldPosted Nov 16, 2007, 10:26 AM
AlanPosted Nov 16, 2007, 10:19 AM
Hi Blake,
Can't say I'm 100% clear what you're trying to do but here's a method which takes a given string and inserts colons every 'x' spaces apart from at the end. I've embedded it into a console app to make sure it works OK:
using System;
class Program
{
static void Main()
{
string s = "1234xXpoOFAd5678";
string t = InsertColons(s, 4);
Console.WriteLine(t);
t = InsertColons(s, 8);
Console.WriteLine(t);
Console.ReadLine();
}
static string InsertColons(string s, int spaces)
{
if (spaces < 1 || spaces >= s.Length)
return s;
int number = s.Length / spaces;
int remainder = s.Length % spaces;
if (remainder > 0) number++;
string[] partitions = new string[number];
for (int i = 0; i < number; i++)
{
if (i < number - 1 || (i == number - 1 && remainder == 0))
partitions[i] = s.Substring(i * spaces, spaces);
else
partitions[i] = s.Substring(i * spaces);
}
return String.Join(":", partitions);
}
}
Blake ArnoldPosted Nov 16, 2007, 9:21 AM
Hey Allan,
I appreciate your candor and I thought about it last night that I could remove the == true statement. However, I think that there is a way to simplify this code, maybe? So, let me lay it out for you what I am trying to do.
Right now I have 4 checkbox controls on my form. And when one is clicked it correlates to a subscription code. If there is more than one subscription code it needs to be delimited by a ':'. Examples ([12345][12345:67890]) The problem arises as I add more subscriptions the if loop will become more complicated. So, this is the pseudo code that I was raking over in my mind.
string strTest = "";
if (checkbox2.checked) {strTest += "1234";}
if (checkbox1.checked) {strTest += "5678";}
//Here is the psudo code because I havent taken the time to sit down and write it
if (!strTest.lenght <= 4) //So if the length of the string is not less than or equal to 4 than
{
insert a : every four spaces. //The problem that I see with this is that it will put a : at the
//end of the string which I dont want. So, I would then have to
//remove the : at the end of the string
}
So, that is the idea that I was having can anyone tell me if that will work? And if so, how I go about inserting a character every so many spaces except at the end if possible?
Thanks
AlanPosted Nov 15, 2007, 5:18 PM
I don't think you actually can simplify it, Blake, or at any rate it would be dangerous to try if the code works as the whole thing is so intricate.
One thing you can do is to get rid of all the '== true' which is unnecessary for bool variables.