|
please help this program will check if the letters inputted are any number of "X" in the beginning and then any number of Y or no Y at all in the middle then any number of X in the end both x in the beginning and end must be the same numbers if it is valid or invalid..
#Valid inputs:
xxyxx , xyx , xxxx , xxxyyyyxxx, xx, xxxxyxxxx,
#invalid inputs:
xyxx , xxyx , yyyyyy , y , xxxxyx, xxxxy, yxxxxx
i managed to get all of them into there proper validity except the input "xx", "xxxxxxxx", xxxx... if there is no Y in the input if will have an error of "index out of range exception was unhandled ,index was outside the bounds of the array".... please help me correct it please here are the codes........
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LanguageAcceptor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int countx1 = 0, countx2 = 0, i = 0, x = 0, y = 0, letterctr = 0;
char[] pew = textBox1.Text.ToCharArray();
int len = pew.Length;
while (i < len)
{
if(pew[i].Equals('x')||pew[i].Equals('y'))
letterctr++;
i++;
}
if(letterctr==0)
{
MessageBox.Show("Valid");
}
else if(letterctr == len){
if(pew[0].Equals('x')){
while(pew[x].Equals('x')){
if(x==len)break;
countx1++; x++;
}
Array.Reverse(pew);
while(pew[y].Equals('x')){
countx2++;
y++;
}
if(countx1==countx2){
MessageBox.Show("Valid");
}
else
MessageBox.Show("Invalid");
}
else
MessageBox.Show("Invalid");
}
else
MessageBox.Show("Invalid");
}
}
}
modified on Friday, October 9, 2009 10:05 AM
|
|
|
|
|
First off, read the instructions at the top of the page, where it says "How to get an answer to your question"
Secondly, change the subject "help me pls..........help me with my codes" to something a litle more descriptive.
Then, enclose your original code in <pre>...</pre> (or use the "code block" bit above the input text area - it does the same thing). This preserves the formatting and make it a whole lot easier to read your code. Nobody is going to want to wade through your spaggetti manually matching brackets and indents.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
You're right. I guess I took a look at it since I'm in good mood today
|
|
|
|
|
I looove his reply "what else is wrong? pls help"!
Ain't it just wonderful round here when the new students start their courses?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Ah, it's that time of year again.. you're right!
|
|
|
|
|
Seriously.. Simple debugging of this code should give you the answer. I'll give you a hint on where you should start to check your logics.
while (pew[x].Equals('x'))
{
if (x == len) break;
countx1++; x++;
}
|
|
|
|
|
im not good in c# ... what else is wrong? pls help
|
|
|
|
|
This is not about being a good C# programmer or not - you would get the same problems in C, C++ and Java as well since it is your logics that fail when you try to access an array index beyond the arrays length.
And like someone else commented, I think there is a lot easier way of achieving what you are trying to do..
|
|
|
|
|
Much better! But when I said "your original code" what I meant was "your original code" i.e. with the formatting and indentation still in, not stripped out by your previous post! Whacking <pre></pre>round unformatted code just makes it pretty colours...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
I'd use a Regular Expression; but maybe your teacher wants you to do it the hard way?
|
|
|
|
|
I've written an addon for Firefox that I want to port to IE8. Unfortunately I'm having trouble getting to the right documentation and I've still not been able to figure out what language should I develop the BHO in. C++ or C#? Or are there other options too?
Any links to documentation and online resource that could help me would be appreciated.
|
|
|
|
|
Preferred?? No. C++ would be the "preferred" language since all of the header files for this only work in C++. Using C#, you'd have to recreate all the structures, contants, P/Invoke headers, ... yourself, by hand.
|
|
|
|
|
Thanks for the reply Dave.
Turns out that not only is C# not the recommended language, the recommendation is actually against using it. Only one instance of .net CLR will be loaded and if two BHO have conflicting versions of .net, one of them is going to be out of luck. Also, each tab will need to load the .net library making this an expensive operation.
|
|
|
|
|
Assembly.Load(...) requires to give the fully qualified name, so does it mean Assembly.Load(..) can load only assemblies present in GAC?
Can a Assembly.Load(...) method load a private assembly?
|
|
|
|
|
shivapriyak wrote: Assembly.Load(...) requires to give the fully qualified name, so does it mean Assembly.Load(..) can load only assemblies present in GAC?
Nope, it can also load an assembly that's present in-memory[^] (as an array of bytes), and you can also load an assembly by giving a partial name[^]. This snippet from PIEBALDConsult may help;
System.Reflection.Assembly assm ;
name = System.IO.Path.GetFileNameWithoutExtension ( Filename ) ;
assm = System.AppDomain.CreateDomain ( name ).
Load ( System.IO.File.ReadAllBytes ( Filename ) ) ;
I are Troll
|
|
|
|
|
5!
|
|
|
|
|
It is a nice snippet!!
I are Troll
|
|
|
|
|
LoadWithPartialName is deprecated. Don't use it.
It will happily load the newest version of the assembly that's available on the machine. If you think that's what you need, please think again. At some point in the future, this newest version is very likely to be incompatible with your app.
In fact, using LoadWithPartialName in a .NET 2.0 app will crash as soon as a newer .NET 4.0 version of the library is installed.
|
|
|
|
|
Daniel Grunwald wrote: LoadWithPartialName is deprecated.
Thanks for mentioning it, it's also in the documentation that was linked to.
Daniel Grunwald wrote: At some point in the future, this newest version is very likely to be incompatible with your app.
Yup. Even if all the signatures stay the same, something is bound to break sooner or later, and it's always best to be prepared.
Daniel Grunwald wrote: In fact, using LoadWithPartialName in a .NET 2.0 app will crash as soon as a newer .NET 4.0 version of the library is installed.
It's not obsolete in 2.0, and I doubt that Microsoft will bring out 4.0 with a feature[^] that breaks a lot of existing code
I are Troll
|
|
|
|
|
In my app, there was a use of Assembly.LoadWithPartialName("System.Xml") . It caused a crash when .NET 4.0 was installed on the machine.
The documentation of LoadWithPartialName mentions this problem in the exception table:
BadImageFormatException : assemblyFile is not a valid assembly.
-or-
Version 2.0 or later of the common language runtime is currently loaded and partialName was compiled with a later version.
|
|
|
|
|
Daniel Grunwald wrote: In my app, there was a use of Assembly.LoadWithPartialName("System.Xml"). It caused a crash when .NET 4.0 was installed on the machine.
Because it tries to load a 4.0-assembly in a 2.0 application.
I agree that you shouldn't just ask the framework for a dll, but I assumed that you were talking about assemblies that you created yourself, not any of the assemblies from the system.
I are Troll
|
|
|
|
|
Hi, all,
In my Windows Form, there is a TableLayoutPanel control having about 15 rows(column count is not taken into account) and a Button, and in the button_Click event, there are two methods RemoveUpperRow() and AddNewRow() inserted as follows:
modified: Sorry, just noticed there are sth else I need to provide before you check my codes.There are also one label in each cells in the first column, and this is the reason I am able to do rolling by the following codes.
private void RemoveUpperRow()
{
Control c = tlp_MouseArea.GetControlFromPosition(0, 1);
tlp_MouseArea.Controls.Remove(c);
tlp_MouseArea.RowStyles.RemoveAt(0);
for (int i = 1; i < tlp_MouseArea.RowCount; i++)
{
c = tlp_MouseArea.GetControlFromPosition(0, i+1);
if (c == null)
break;
TableLayoutPanelCellPosition controlPosition = new TableLayoutPanelCellPosition(0, i);
tlp_MouseArea.SetCellPosition(c, controlPosition);
}
}
private void AddNewRow()
{
tlp_MouseArea.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 18F));
#region new label and its settings
Label lbl_New = new Label();
if (int.Parse(tlp_MouseArea.GetControlFromPosition(0, 13).Text) < 15)
{
lbl_New.Text = Convert.ToString(int.Parse(tlp_MouseArea.GetControlFromPosition(0, 13).Text) + 1);
}
else
lbl_New.Text = "0";
lbl_New.Dock = DockStyle.Fill;
lbl_New.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
lbl_New.TextAlign = ContentAlignment.MiddleCenter;
#endregion
TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition(0, 14);
tlp_MouseArea.SetCellPosition(lbl_New, pos);
tlp_MouseArea.Controls.Add(lbl_New);
}
My problem is that it works fine, but it seems to be too slow, and the whole form is flicking till the end, that's something nobody would want to see. So, is there anything wrong with my code? Would you give me some suggestions? Your kind help is so much appreciated!
Liang
modified on Friday, October 9, 2009 7:26 AM
|
|
|
|
|
How about using SuspendLayout and ResumeLayout ?
|
|
|
|
|
Hi,
Thanks for your reply. I've tried that one before, it didn't help. .
And I tried again after your post, it performs better than the first time, but problem still exists...
I guess, but not quite sure, it caused by that for loop in the first method...
Anyone else have better idea? Thank you.
modified on Friday, October 9, 2009 8:51 AM
|
|
|
|
|
You can try to enable DoubleBuffering ont he form, but, this is probably not going to fix your problem either. You have to keep in mind that redrawing controls all the time (while you move the grid) is expensive. The more controls you have, the longer the redraw is going to take.
|
|
|
|
|