|
I am working on this particular part of code in c# and I keep receiving error messages and I dont know what I am doing wrong. Can anyone help me out?
public static void personalDetails()
{
string [,] books =
{"The Time Machine, Science Fiction, By H.G.Wells",3};
{"Ender's Game, Science Fiction, By Orson Scott Card",1};
{"Brave New World, Science Fiction, By Aldous Huxley",3};
{"Frankenstein, Horror, By Stephen King",1};
{"Misery, Horror, By Stephen King",2};
{"The Dark Half, Horror, By Stephen King",2};
{"Firestarter, Horror, By Stephen King",3};
{"Torment, Mystery, By Lauren Kate",3};
{"Before I fall, Mystery, By Lauren Oliver",3};
{"Entwined, Mystery, By Heather Dixon",2};
{"The Thirteenth Chime, Mystery, By Heather Dixon",1};
{"My Sister's Keeper, Mystery, By Jodi Picoult",3};
{"Harry Potter and the Deathly Hallows(Harry Potter,#7), Mystery,By J.K.Rowling",2};
{"The Da Vinci Code, Mystery, By Dan Brown",2};
int book
const MAX_BOOK = 5;
if(book >=5)
Console.WriteLine("You are at your limit!")
}
|
|
|
|
|
There are several problems here:
const int MAX_BOOK = 5;
public static void personalDetails()
{
string[,] books = {
{ "The Time Machine, Science Fiction, By H.G.Wells", "3" },
{ "Ender's Game, Science Fiction, By Orson Scott Card", "1" } };
int book = 0;
if (book >= MAX_BOOK)
Console.WriteLine("You are at your limit!");
}
|
|
|
|
|
Where to start? Well, you haven't initialised your array, so the first line needs to become:
string [,] books = { Next, you are adding in an integer into each line but it's expecting 2 strings so you should make it:
{"The Time Machine, Science Fiction, By H.G.Wells","3"}, As you are using an array initializer here, you need to end it with another } because you are adding in multiple items. I've already taken care of the first one for you at the top, so you'd end your initialisation with:
{"The Da Vinci Code, Mystery, By Dan Brown","2"}}; Note that the lists in the array must be separated by commas and not semi-colons.
int book needs to be terminated with a semi-colon as in:
int book; Beyond that, you declare a constant but don't tell it what type the constant is, so try
const int MAX_BOOK = 5; You haven't terminated your Console.WriteLine with a semi-colon. Make this:
Console.WriteLine("You are at your limit!"); Now, some general comments. First of all, you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0. I assume you mean to assign it to be the length of the array, so you actually need to use that value in there (hint - you need to use books.Length).
Secondly, you have declared a constant that you don't use - this is not a great idea; use the constant in place of the 5 in the if test.
Honestly, this is fairly basic stuff. You have to learn to read the compiler output and figure this stuff out. I saw the problems in your code just by looking at it - I didn't have the advantage of the compiler errors here; you do, so learn to use them. If you're using Visual Studio, the errors will be underlined and they will tell you exactly what's wrong.
|
|
|
|
|
Pete O'Hanlon wrote: you declare book but you don't set it to a value so it will never trigger the WriteLine condition because an int defaults to 0.
Worse actually, it will fail to compile. It doesn't satisfy the definite assignment rules (it doesn't even try).
|
|
|
|
|
I'm not sure if there is a better forum for this one - but I will put it here because the plug-in is written in C#...
I have an Outlook 2010 plug-in that I created, with a deployment solution. After installing it via the MSI file, there is a configuration file that must be manually copied to its installation folder,
C:\Program Files\<Company Name>\<Product Name>
The odd thing is, one of my colleagues reported that his file "vanished" from the folder. Today, the same thing happened on my machine. Both are running Windows 7 (one machine is 32-bit, the other 64). I checked my Recycle Bin, and it was not there either.
Does Windows 7 have some protective feature that removes files from protected directories automatically? I do know that Administrator rights are required to copy to that location.
|
|
|
|
|
<System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.WaitHandle.WaitOneNative(SafeWaitHandle waitHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(TimeSpan timeout, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(TimeSpan timeout) >
Exception like this.
The web application, here on clicks, singleton object creates separate thread then that thread read a data read from xml file. using XMLDocument.
then after read that file data updated to database, then waiting for another request for that, with in the thread ManuelResetEvent wait is performed, if comes next request from while onlick button its Ret and release the thread block and doing that same process.
as per requirement,In between if needs, can change xml value dynamically, but when i edit that xml and saving, the thread was aborting automatically then entire application crashed.
code is
private void XMlreaderProcess()
{
long delayTimeSpanTicks = 0;
try
{
while (Active)
{
try
{
triggerCtrl.Reset();
if (delayTimeSpanTicks == 0)
{
triggerCtrl.WaitOne();
}
else
{
triggerCtrl.WaitOne(new TimeSpan(delayTimeSpanTicks));
}
StartProcess();
}
catch(Exception ex)
{
delayTimeSpanTicks = 0;
}
finally
{
}
}
}
catch(Exception ex)
{
}
finally
{
Active= false;
}
|
|
|
|
|
Not wishing to touch your design (which sounds pretty dodgy), just explain this exception: A ThreadAbortException is raised within a thread when Thread.Abort is called on it. Either your code, or some framework code, must be doing that. My guess would be that the web framework kills any threads that were started as the result of a request once the request is finished, but it is just a guess – hopefully someone can post who actually knows the answer.
Your posted code is swallowing exceptions – and it also appears to be using exceptions as part of normal code flow control, which is probably worse.
|
|
|
|
|
Thank for reply.
This is not based on web request process. This is schedule based background work. the user can update the time of the scheduling from web page.
on the application host the thread initiate the process.At beginning the thread started and waiting for that particular schedule time( which is getting from database)And that thread is waiting until that time using manualResetevent ( block the thread process using waitOne(milSec).
If comes at the particular times, the process is read the xml file and that data would be updated to database. for that xml reading
XmlDocument configDoc = new XmlDocument();
XmlNode parentNode = null;
configDoc.Load(tConfigfilePath);
if (null != configDoc)
{
XmlNode Id = configDoc.SelectSingleNode("/CCC/AAA");
Id.SetValue("ZZZ", detectOid.InnerText);
}
Then that value inserted to database. Again that waiting for next day time using below code. In between time if any changed happened on the xml file, suddenly the catch ( with in a while loop) is catch the exception.
private void XMlreaderProcess()
{
long delayTimeSpanTicks = 0;
try
{
while (Active)
{
try
{
triggerCtrl.Reset();
if (delayTimeSpanTicks == 0)
{
triggerCtrl.WaitOne();
}
else
{
triggerCtrl.WaitOne(new TimeSpan(delayTimeSpanTicks));
}
StartProcess();
}
catch(Exception ex)
{
delayTimeSpanTicks = 0;
}
finally
{
}
}
}
catch(Exception ex)
{
}
finally
{
Active= false;
}
|
|
|
|
|
Hi,
How to create a license file or digital signature for a setup file
on c#.
|
|
|
|
|
Hi All
I know this may not be the right place for this but can't see an appropriate place and hope someone is familiar with the issue i am having.
I have a c# app which i am deploying using studio 2008 deployment project.
Thing is i will be installing a 'loose' file and I only want to install it if the file does not exists on the target location or is newer than the one that does already exist.
A simple task i thought!! but i can't find any worthwhile help on this anywhere (probably googling wrongly).
Be very grateful if anyone can help me with this.
Thanks in advance
|
|
|
|
|
Google for Custom Action. You will need to add one to your setup to validate the file if it not being controlled via the setup package
No comment
|
|
|
|
|
Hi,
How to find coordinates and width and height of each word in a pdf file using iTextSharp. I found few articles on iTextSharp to create pdf files and convert documents to pdf files.
Can anybody help me to find out the coordinates of each word. Thanks in advance.
modified 15-Dec-11 5:51am.
|
|
|
|
|
I want to analyse the Sql Syntax,
and divide the Sql string in to different parts,
like "selected columns part","selected tables part","where part"
and build classes for these parts,
For Example:
Select CompanyName from Companys
"CompanyName" is the "Selected Column" part;
"Company" is the "Selected Table" part;
Then put these parts into Orient-object classes;
The problem I met is that:
How can I anlyse the syntax of "Sql Query",and sperated them in to different parts?
Best wishes;
|
|
|
|
|
It's not that simple (it never is).
Despite SQL syntax being basically simple, it hides a lot of potential complexity in the form of nested queries, joins, and so forth. To do a job that actually is of some use, you need to parse SQL properly.
Google can help (parsing SQL has been done before): Parse SQL query[^]
If nothing else, it gives you a number of starting points. Time for some reading!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Really thanksful for your reply,
I've done a lot on this,
and already to generate sql query by Objective Codings,
But I cant do it reversely,
you know make Objects by Sql query.
Sincerely;
|
|
|
|
|
Hi All!
I have created an Event Handler for several buttons. When a button is clicked I am searching through my array of buttons to see which one has been clicked:
//my event handler
void Button_Click(object sender, EventArgs e)
{
Button ClickedButton = (Button)sender;
if(ClickedButton == null)
return;
for(int i = 0; i < MaxButtons; ++i)
{
if(ClickedButton == MyButtons[i])
//Do Stuff!
return;
}
}
Is there a better way of finding out which button has been clicked?
Thanks for any advice!
Mike
|
|
|
|
|
Huh?
sender is the Control that caused your handler being called. So if it was a Button, then (Button)sender is it. You already have that in your code. Didn't you understand your own code? Or did you mean to ask something else?
BTW: if several Buttons share a single Click handler, and need a slightly different handing, then you could store a parameter in the Button's Tag property, or you could maintain a Dictionary<button, someparametertype=""> to look up the parameter value.
|
|
|
|
|
Thanks Luc,
Yes, I do have a single event handler for several buttons. I will take a look at the tag property and see if that will do what I want - what I have seen on the net looks like it should.
I will let you know how I get on
Cheers
Mike
|
|
|
|
|
Hi Luc,
That was easy!
All I do now is set the tag to an integer value when initiating the buttons, and then in my Event Handler retrieve the tag value and that identifies which button was pressed.
Thanks very much for your help!
Mike
|
|
|
|
|
You're welcome.
|
|
|
|
|
Here's another alternative:
private void ButtonsForWhatEver_Click(object sender, EventArgs e)
{
switch((sender as Button).Name)
{
case "btn_whatEver1":
break;
case "btn_whatEver2":
break;
case "btn_whatEver4":
break;
default:
break;
}
} Discussion:
1. there are endless discussions here on CP, and StackOverFlow, about the relative efficiency of using a string in a switch statement compared to a numeric value. For example:[^]
1.a. if your "buttons are not being pushed" a thousand times a minute , in this case using a string will probably have an insignificant performance hit.
1.b. what the compiler does in translating structs into IL is quite complex: with a switch statement with many string conditions, I read that it will generate and use a hash-table. In other circumstances, I read, a Dictionary is used "under the hood." For an in-order sequence of integers: the most performant possible jump-table is created, I read.
2. In a Button Click EventHandler you really don't need to worry that "sender" is going to be "null," or not a Button: code that would invoke a Button EventHandler directly, without a Button being pushed, would be an abomination
2.a. If you did have code in a Button Click EventHandler that needed to be used from outside the BC EH, then that code should be broken out into its own method, and then the BC EH can call that method.
best, Bill
When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then. Blaise Pascal
modified 15-Dec-11 3:58am.
|
|
|
|
|
Hi Bill,
Thanks for replying
i did think of using a switch, but I may have up to 250 buttons, which would make the switch statement rather large!
I have just tried Luc's suggestion of using the tag property, and this seams to have done it very efficiently. I will have to look further in to tags to see what else I can do!
Cheers
Mike
|
|
|
|
|
250 buttons? What is the rationale for using one event handler for 250 buttons? What's wrong with one event handler per button? Is there alot of common code for each button? For 250 buttons I would have an event hander for each button and create a method which executes common code. The button handler would call this method and pass (to the method) parameters specific for each button.
|
|
|
|
|
250 buttons? are you out of your mind? that is horrible.
If you need a large grid full of buttons, and you care about responsiveness a tiny bit, then go for a single Control (I'd suggest a Panel), and:
- paint the grid lines and the button texts/images (if any) in its Paint handler;
- and inside its MouseDown handler, figure out which virtual button is clicked (the e.X and e.Y parameters give you the mouse-down coordinates relative to the top left corner of the Control), then act on that.
|
|
|
|
|
... since this is a kind of a long response, with a lot of content that does not relate directly to the first thread/set of comments on the OP's thread, and code examples, I will post this here as a separate "fresh" response to the OP ...Miwin Solutions wrote: but I may have up to 250 buttons Hi Mike,
Like the other commentators on this thread, I "gasp" at the idea of 250 buttons ! But, I also assume it's possible that not all of them are shown/visible at the same time. "bunches of them" on each TabPage in a TabControl is one idea that comes to mind.
Some more information from you can help further refine the proposed solutions here: and, imho, definitely influence implementation strategy:
1. how are these buttons "organized:" are they in a row, column matrix ? Are they "all over the place" ? Are they inside different containers, or in nested containers ?
2. will every one of them invoke/execute/call a unique function (perhaps in addition to some common functionality they all invoke) ? Or, will "bunches" of them execute the same code ?
2a. a rarer case (?) would be the requirement that code that each (or some number of) Button(s) executes changes during the application lifetime.
3. is the unique code each, or "bunches of," Buttons will call "elaborate," or "simple" ?
If you use a Tag, you have to cast the Tag back to whatever object type you require ... again I think this is no big deal, unless you are getting a thousand button presses a minute ... and then ? ... you're going to need some kind of "selector structure" which that converted Tag can be used to access to select the code required to be invoked. That could be a switch statement, a nested if then/else block (please no, anything but that one), or, as Luc suggested, a Dictionary look-up.
Hints:
1. case statements in a switch block can be "or'd;" to create "bunches," all with the same handler:
case "button4":
case "button5":
Console.WriteLine((sender as Button).Name);
break; But, once written that way, you are not going to be able to modify without re-coding, and re-compiling.
2. executable code (anonymous functions) can be stored directly in a Tag object and invoked with one extra level of casting: you'll need a delegate:
private delegate void uniqueButtonCodeToExecute(); And, you can set code to be executed in the Click EventHandler like this:
SomeButton.Tag = new uniqueButtonCodeToExecute(() => Console.WriteLine("in Button8's unique code")); And then, the Click EventHandler for each Button could work like this:
private void ButtonUniqueCode_Click(object sender, EventArgs e)
{
((sender as Button).Tag as uniqueButtonCodeToExecute)();
} 3. Similarly, you could have a Dictionary with KeyValue Pairs whose Keys were of Type object, and whose Values were of Type anonymous function/method, or valid code for calling an external named function/method:
private Dictionary<object, uniqueButtonCodeToExecute> ButtonCodeDictionary = new Dictionary<object,uniqueButtonCodeToExecute>(); You'd create the Dictionary entry per Button like this:
ButtonCodeDictionary.Add(SomeButton, () => Console.WriteLine("in Button8's unique code")); Then, the Button Click EventHandler could be:
private void ButtonsForWhatEver2_Click(object sender, EventArgs e)
{
ButtonCodeDictionary[sender]();
} What if you want get the definition of the code unique to each Button out of the assignment as anonymous function (which I'd recommend for code clarity, maintenance, re-usability, etc.) ? Then you can use something like this:
ButtonCodeDictionary.Add(theButton, () => SomeUniqueButtonsMethod("Button8")); In this example the lambda notation () => does the right thing to set the delegate to an externally defined method, passing a parameter: here's what the function could look like:
private void SomeUniqueButtonsMethod(string buttonName)
{
Console.WriteLine(buttonName);
} So, you have a wide range of options to use here.
My own choice, assuming 250 buttons, and every one of them executing a different method/function, would be to use the Button's Tag, store one of a sequence of integers in each Tag, and then use the Tag cast to an Int as the selector in a big switch statement: after all, you could execute code anywhere in the application, inside any of those switch cases that are selected, via techniques such as cross-class method/function exposure, invoking static members of a static "uber-class" that all classes could see, etc.
But, I remain convinced that a scenario that uses 250 buttons, each with unique code to be executed, is probably a sign that the code strategy, the architecture, really needs to be changed.
I've been playing around with storing executable code in Dictionaries for several years: for example, in this article for DevX:[^], written for .NET 2.0, in which I make the Value of the Dictionary's KeyValue Pairs, an actual delegate definition itself, and then use "DynamicInvoke" to execute them (scroll down in the article to the section titled: "Introducing Dynamic Code Dictionaries").
When I consider the brief span of my life, swallowed up in the eternity before and after, the little space which I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which knows me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then. Blaise Pascal
modified 15-Dec-11 20:58pm.
|
|
|
|
|