|
Mostly PadRight, with a smattering of Replace thrown in.
And to think I wrote a version last year using four for loops; oh the shame!
|
|
|
|
|
Okay - I've got a working solution without a single loop or conditional statement, nor does it use recursion. Probably not the most elegant solution, not as compact as Guffa's solution, but interesting none the less.
|
|
|
|
|
I'll show you mine if you show me yours....
In the interest of being the smart-ass brown-noser in the class I used line drawing characters rather than asterisks. Anybody can use asterisks.
System.Console.WriteLine
(
"\u250C".PadRight ( Width - 1 , '\u2500' ) + "\u2510\n" +
"".PadRight ( Height - 2 , '@' ).Replace
(
"@"
,
"\u2502".PadRight ( Width - 1 ) + "\u2502\n"
) +
"\u2514".PadRight ( Width - 1 , '\u2500' ) + "\u2518"
) ;
|
|
|
|
|
I suspect your solution is printing one character too wide.
My solution is not so compact:
private static void Main(string[] args)
{
Console.Write("Width:");
int width = Convert.ToInt32(Console.ReadLine());
Console.Write("Height:");
int height = Convert.ToInt32(Console.ReadLine());
string end = new string('*', width);
string middle = string.Concat("*", new string(' ', width - 2), "*", Environment.NewLine);
string fill = new string('-', height - 2);
fill = fill.Replace("-", middle);
Console.WriteLine(end);
Console.Write(fill);
Console.WriteLine(end);
Console.ReadLine();
}
|
|
|
|
|
In what I posted I use width-1 so it's not a character too wide. (Pad only adds enough characters to satisfy the requested length.)
I've never looked at constructors for string so I didn't think of that, but
"".PadRight ( width , '*' )
results in the same thing as
new string('*', width)
I suspect the constructor is more efficient.
Another tool for the toolbox, thanks.
|
|
|
|
|
Letting the replace do the looping is an interresting idea. The string constructor can do some looping too.
int w=6,h=4;Console.Write(("h"+new string('w',h-2)+"h").Replace("h",new string('*',w)+"n").Replace("w","*"+new string(' ',w-2)+"*n").Replace("n", Environment.NewLine));
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
StringBuilder.Append will do that as well, leading me to suspect that the string constructor uses a StringBuilder. Stepping into the source code should confirm it.
|
|
|
|
|
I doubt that. I believe that the string constructor does something much less complicated than using a StringBuilder. It probably just allocates the memory needed for the string and fills it with the character. Also, the StringBuilder returns a finished string object, so it would be complicated to merge the string object being created with the string object returned from the StringBuilder.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
|
It's fun to obfuscate homework.
int w=6,h=4;h*=w;for(int i=h;i>0;Console.Write("*\r\n ".Substring(--i%w==0|w==i%w+1|w>i|h<i+w?0:3,i%w==0?3:1)));
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Oh no! You didn't use Environment.Newline! The world is going to end!
|
|
|
|
|
PIEBALDconsult wrote: The world is going to end!
The world is going to \r\n!
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Ok, here is good question. I am building an application, which has a mainform, that opens a splash screen, shows a load progress bar, then jumps to the system tray. All of that is working fine. Now I am trying to implement some code that will allow me to insure that only one instance will open. I have done this successfully using a Mutex module and its WaitOne function. Now when the WaitOne function fails, i catch it with an else clause, which works, debugging shows it caught correctly, now I want to somehow tell the already open instance that someone tried to open you again, and to pop up a message balloon saying here i am. I have the following code, which sends a system message using PostMessage, and I have overridden the WndProc procedure in the MainForm, but the WndProc does not catch the message. I cannot figure out why. I know that the WndProc catches messages, because I tested it by inserting a thumbdrive, and the WM_DEVICECHANGED message was caught, but my custom message is no caught which leads me to beleive there is a problem with PostMessage, anyway here is my code
Mutex mutex = new Mutex(true, "MyProgram");
[STAThread]
static void Main()
{
if(mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
mutex.ReleaseMutex();
}
else
{
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.ALREADYOPEN, IntPtr.Zero, IntPtr.Zero);
}
}
In the NativeMethods, HWND_BROADCAST is defined as 0xffff and the ALREADYOPEN message is defined with RegisterWindowsMessage("ALREADYOPEN")
then in the MainForm i have the following code
protected override void WndProc(ref Message m)
{
if(m.Msg == NativeMethods.ALREADYOPEN)
{
here is the code to show the BalloonToolTip, but this is never reached
}
base.WndProc(ref m);
}
In debug I can watch all the original creation messages being caught, and like I said the WM_DEVICECHANGED message is caught when I insert a thumb drive, but for some reason my custom message is not being caught, something has to be wrong in the PostMessage area I am thinking
Can anyone shed any light here?
|
|
|
|
|
Ok, i got i figured out, at least in my case. Instead of using PostMessage, I had to use SendNotifyMessage(IntPtr hwnd, IntPtr msg, IntPtr wparam, IntPtr lparam)
then just pass (IntPtr)0xffff as hwnd, and my custom msg as IntPtr, and IntPtr.Zero for both params and it works
|
|
|
|
|
I have a form1.cs and a formHelper class, so how can i show the relationship between form1 and formHelper class. Basically, the formHelper process the command which were invoked by the user. Also Can somebody demonstrate Association through code. Lets say, Class B has a Class A instance so how can i show in Visual studio 2005 class diagram.
Thanks
|
|
|
|
|
netJP12L,
You can create a diagram in VS by right clicking on your class in the solution viewer, and selecting "Create class diagram" (its something like that, cant remember exactly)
Regards,
Gareth.
|
|
|
|
|
Anyone have example of web service method for google language translation using http post method in asp.net using C# language or any idea of how to write it ?
|
|
|
|
|
If i've got the following code and its selecting the following values
C0603~A81_01
C0603~A81
how can i change it so that it splits these two values up
at the moment it is reading it as C0603~A81_01C0603~A81
thanks in advance
string roomcodeselect = "Select room_code from room_types where room_code like '" + room_code + "%' ORDER BY room_code DESC";
SqlCommand cmdroomcode2 = new SqlCommand(roomcodeselect, con2);
StringBuilder htmlStr_room_code5 = new StringBuilder("");
SqlDataReader readerroomcode = cmdroomcode2.ExecuteReader();
while (readerroomcode.Read())
{
htmlStr_room_code5.Append(readerroomcode["room_code"]);
}
lbtest2.Text = htmlStr_room_code5.ToString();
Session["sessiontest1"] = lbtest2.Text.ToString();
//string test2 = (string)Session["Sessiontest1"];
db = Session["sessiontest1"].ToString(); // "jh00~pr11_01";
if (db.Contains("_"))
{
string[] split = db.Split('_');
string s_number = split[1];
int i_number = Int32.Parse(s_number);
i_number++;
s_number = i_number.ToString("00");
db = split[0] + "_" + s_number;
}
|
|
|
|
|
I only gave a cursory glance, but the problem seems to be here: htmlStr_room_code5.Append(readerroomcode["room_code"]); You will have to add a statement that says htmlStr_room_code5.Append(Environment.NewLine); to your loop.
Having said that, there's a lot more to correct. Inline queries are frowned upon; I suggest you read up on Sql injection.
Cheers,
Vikram.
"If a trend is truly global, then that trend ought to be visible across ANY subset of that data" - fat_boy
|
|
|
|
|
Like Virkram said, you are creating a string with all the values that match the LIKE statement. Either split each value returned, or use a WHERE room_code = 'value'; since im guessing room_code is unique, thus should only return 1 value.
Also, on a side note, maybe have a look @ http://msdn2.microsoft.com/en-us/library/ms229002.aspx[^].
Its ALOT easier to read code when you name objects with the camel/pascal naming convention eg: readerRoomCode instead of readerroomcode.
Regards,
Gareth.
|
|
|
|
|
Hi
I have a radiobutton list, how do i specify in the c# to enable disbable one of the items in the list
thanks
|
|
|
|
|
<br />
private void DisableRadioButton()<br />
{<br />
RadioButtonToDisable.Enabled = false;<br />
}<br />
now that wasn't too hard was it? Of course, you can go wild and actually pass a reference to the particular radio button you want to disable and disable that one.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
"There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib
"Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
|
|
|
|
|
No your post disables the whole of the radiobuttonlist
I just want to disable one of the items in the list and I cannot find the correct syntax to do so
maybe you should read my original post again
|
|
|
|
|
sorted it myself:
RadioButtonList_audit.Items[0].Enabled = true;
thanks for all your wisdom
|
|
|
|
|
Actually, it only disables one, the one named "RadioButtonToDisable".
As is, the addendum to the post says that you can expand on it where you can pass a parameter so you can choose which one you want to disable.
so something like this could exist:
enum eRadioButtonList
{
RadioButton1 = 1,
RadioButton2 = 2,
RadioButton3 = 3
}
private void DisableRadioButton(eRadioButtonList rbl)
{
switch(rbl)
{
case 1:
RadioButton1.Enabled = false;
Break;
case 2:
RadioButton2.Enabled = false;
Break;
case 3:
RadioButton3.Enabled = false;
Break;
}
}
There, did your homework
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
"There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib
"Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
|
|
|
|
|