15,997,597 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View .NET questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Ian A Davidson (Top 191 by date)
Ian A Davidson
23-Jul-14 2:31am
View
Do you mean, how can you overcome the rounding problem?
I expect people have written numerous articles on it, because it is a problem all computers have in one form or another due to the way floating point numbers work.
Ian A Davidson
23-Jul-14 2:22am
View
Deleted
Math.Round(value, 2, MidpointRounding.AwayFromZero);
Ian A Davidson
23-Nov-13 17:06pm
View
Reason for my vote of 1 \n My vote of 1.
Ian A Davidson
2-Nov-13 13:01pm
View
Translation of first paragraph includes "watch the live broadcast of the match..."
Ian A Davidson
27-Oct-13 19:29pm
View
I think not a simple one. You're basically looking at developing some form of artificial intelligence. If I gave you the numbers 2,4 and 6 I'm sure you immediately see that the next number in the sequence is 8. So you might write a function to check for the difference between the numbers, and simply add that on to the last number to find the difference. But then if I gave you the numbers 2, 4 and 8 again your brain would immediately see that the next number is 16, but now you've got to include in your function checking to see if the number is doubling. Okay, so now what if I give you the numbers 1, 4, 9? Basically your function would have to know about every possible type of mathematical sequence and test for them. What about 1,3,6,10? Etc, etc. And these few examples are extremely simply sequences. Unless you are going into A.I. I'd say steer clear.
Ian A Davidson
18-Oct-13 16:07pm
View
Deleted
Consider another profession?
Ian A Davidson
11-Oct-13 7:07am
View
This is not C#
Ian A Davidson
5-Oct-13 21:52pm
View
In most cases, you probably want File.GetLastWriteTime().
Reason: Creation time will give you the time the file was created on the disk. And, if the file is copied, for example, that time will be updated to reflect that. So in general creation time often will not reflect the time the content of the file was created.
Example: Create new text file at t1; file will be 0 bytes, but creation time will be t1. Edit file, adding the content and save at time t2. Creation time is still t1, but last modified time is t2.
Copy text file to new file name at time t3. Creation time is t3, but last modified time is still t2 (the time the "content" was last modified or "created").
However, the value returned by either method will be affected by what the disk drive supports and/or how the original operating system handles the file. And, for example, a downloaded file is likely to show the same time for both values, which is the time of download. So it may be that you are looking for a different solution altogether:
In terms of Excel, Excel (and Word, etc) writes into the file its own record of time and date of creation and modification, so it may be that you want to use that instead, which is where answers like Solution 1 (if it can be got to work) may be better.
Ian A Davidson
26-Sep-13 21:01pm
View
Sorry! I've deleted my comment since I've just realised this must be some kind of test question, because if you were coding it your IDE should immediately indicate where the problems are.
Regards,
Ian.
Ian A Davidson
26-Sep-13 20:57pm
View
Deleted
Is the space between the plus signs causing problems? (It says "x+ +"; should be "x++").
Also, unless "m" is a field of the class, I believe you need to initialise it: "int m = 0;"
Ian.
Ian A Davidson
14-Sep-13 19:53pm
View
Deleted
"If you don't know how something works, don't do it".
The question should be, why would someone write code like this?
Ian A Davidson
3-Sep-13 4:46am
View
Search for "Object Oriented Programming", or "OOP". If you don't know what that is about, then you are quite right - don't bother using classes! (And I would wonder why you are using C# at all, which is founded on objects). However, if you get your head around OOP, then it can make writing different kinds of software a lot simpler and more understandable. Please, please, don't become like so many programmers I see, who simply stick "methods" into "classes" and call it OOP. So often they end up with a load of static functions (sub-routines, if you like) in placeholders which bear no relation to objects - basically, procedural programming, but even harder to understand because functions are spread all over the place.
Find out what OOP is about, and use it well; life will become much simpler for you and for anyone else who comes to maintain your code.
Regards,
Ian.
Ian A Davidson
20-Aug-13 13:58pm
View
Not really. I'm getting the error on *PathName*, not on *Flags*. It doesn't matter what I use for Flags. You did say in a previous comment that you'd read the question. What did he say about PathName? Is the reported error just a bug in the Code Analysis tool?
Regards,
Ian.
Ian A Davidson
20-Aug-13 11:48am
View
Thanks. The parameters are defined as pointers to wide-character strings, so unicode is definitely correct. The problem is that it is complaining that the pointer itself should be only 4-bytes on a 64-bit system (i.e. the same as on a 32-bit system), rather than 8-bytes, which pointers on 64-bit systems are by default. I don't know why, and I don't know, but it could even just be some obscure bug in MS's Code Analysis tool. Unfortunately I've had to take a break from that code anyway, as some other stuff has priority, so it may be a few more weeks before I am able to look at it again. In the meantime I'll be trying MS's forums to see if anyone there knows what's going on.
Thanks again,
Ian.
Ian A Davidson
19-Aug-13 4:12am
View
Deleted
I'm sorry if I was seen as being rude; I only answered your reply in the way you answered me.
I agree, that sometimes people answer badly - for example, I saw your reponse to the "globals" question, and gave it a +5 because others had replied that it just shouldn't be done (which I'm not sure about myself - I think there are some cases where a global is required / can be useful - such as a magic number, for example).
However, it says something when people are more concerned about getting an exact answer to a question, no matter how ridiculous, rather than correcting the person asking the question and encouraging them to write good code.
I guess I'm a bit fed up of this forum, because it seems a lot of time is expended on giving "correct" answers to questions that are basically pointless, and really shouldn't be getting asked at all, but when someone has a genuine question, no one has an answer for it.
Regards,
Ian.
Ian A Davidson
19-Aug-13 2:19am
View
Deleted
"Whatever"!
1) If all the OP wanted was "Hello World", then why does s/he want if-else statement anyway?
2) I guess a stupid question would get stupid answers and comments like yours.
Roughly speaking: instead of pointing out that the bicycle asked for would have square wheels, you are providing one whose wheel nuts are loose and whose brakes don't work ;)
I can't believe you are defending atrocious coding to answer a question which makes no sense in the first place.
Ian A Davidson
18-Aug-13 18:30pm
View
Deleted
I don't quite get that! It is exactly equivalent in terms of logic and execution, except it is correct code!
Ian A Davidson
18-Aug-13 12:30pm
View
Yes, I think that is my next point of call. I'll raise it on one of Microsoft's forums or as a comment on the page where they explain the error.
Thanks anyway.
Ian.
Ian A Davidson
18-Aug-13 4:08am
View
Thanks for taking time to reply.
I'm afraid neither of those pages mention this situation or what to do about it; I did already do my research ;)
I was hoping someone here might have come across this problem before and known what to do about it. But never mind.
Thanks again,
Ian.
Ian A Davidson
16-Aug-13 13:05pm
View
Thanks for comment, and that may be the case as well, but no - the problem is with the string (PathName).
Ian.
Ian A Davidson
14-Aug-13 19:56pm
View
Can you say which line the error occurs on?
As OriginalGriff points out, you are deleting an object after you've placed it in your vector, but I don't think that's causing the problem you are seeing (once you've fixed that and if you don't correct the other mistake, I expect you'll memory access violation error later in the code, when you try to use cc_tab1).
I suspect the error you are seeing is in the for loop, right?
Question: What does Breadth_First_Search do? You pass it the first item in "queue", and then iterate through. But then in the for loop you're removing the first item in the queue.
What about m_vertices? Can you be sure there are the same number of items in it as there are in "marked"?
Finally, you are also referencing cc->m_nodes by index (i). But you are only adding a node if "marked[i]" is true. So there may not be the same number of nodes. Do you want to use something like "m_nodes->back()" instead?
Quite frankly, the code seems extremely hard to understand. I think it needs some serious thought about what you are trying to do. Use comments as you go along, not only to explain it to someone else, but also to force yourself to think about what you are doing and check it would work or whether you need other checks (for whether you indexes are in range, for example).
Regards,
Ian.
Ian A Davidson
14-Aug-13 19:34pm
View
This is extremely bad code.
You should generally avoid "goto", but you should never ever use it to break a logical construct like this.
The better, and functionally equivalent, solution would be to get rid of the "else" part altogether - move the 'cout<<"World";' line outside of the if block, thus:
////////
if (true)
{
cout<<"Hello";
}
cout<<"World";
////////
and, by the way, this then is also functionally equivalent:
////////
cout<<"Hello";
cout<<"World";
////////
If this IS an interview question, as someone else thought it might be, the correct answer I would be looking for would be along the lines of "Why would you? The whole point of if/else is to perform conditional commands."
But perhaps this is some sort of Quantum physics question?
////////
if (cat.IsAlive) { ... } else { ... }
////////
Regards, Ian.
Ian A Davidson
14-Aug-13 19:30pm
View
One of the most pointless questions I've seen in a while. Why would you want to? The very construct is designed to be one or the other. If something is true do this, otherwise (i.e. if it is not true) do that. If you want to do both actions when the "something" is true, then you want them both in the "if" block. If you want to do the other bit whether the "something" is true or false, then it should be after the "if" block (not in an else block).
Sounds like you need to learn some logic.
Ian A Davidson
23-Jun-13 16:58pm
View
I agree with SA. Getting the OnStart event to create a thread and the OnStop event to cause the thread to finish is the right answer, so you should post it as a solution.
Ian.
Ian A Davidson
7-Jun-13 3:44am
View
The link is included in my solution.
Regards,
Ian.
Ian A Davidson
7-Jun-13 3:26am
View
Two possibilities I can think of: either (a) @currDate is already declared somewhere else, or (b) it could be because "DECLARE" is a special statement that must always be included at the START of the procedure; the interpreter is "being nice to you", allowing you to declare it where you have it, but actually declaring it at the start, and then, effectively, ignoring the if statement. Have you tried taking the declare statement out altogether? What happens then?
Ian A Davidson
7-Jun-13 3:26am
View
Deleted
Two possibilities I can think of: either (a) @currDate is already declared somewhere else, or (b) it could be because "DECLARE" is a special statement that must always be included at the START of the procedure; the interpreter is "being nice to you", allowing you to declare it where you have it, but actually declaring it at the start, and then, effectively, ignoring the if statement. Have you tried taking the declare statement out altogether? What happens then?
Ian A Davidson
7-Jun-13 3:21am
View
I don't believe the brackets are needed. http://msdn.microsoft.com/en-us/library/ms182717.aspx
Ian A Davidson
6-Jun-13 3:48am
View
It sounds to me as if John has answered your question. From a brief reading of ClickOnce deployment (http://msdn.microsoft.com/en-us/library/142dbbz4(v=vs.80).aspx) - I have never had the opportunity to use it yet myself - it sounds as if it does everything you want. You don't have to e-mail anyone, so I don't know what your comment is about.
Regards,
Ian.
Ian A Davidson
27-May-13 19:06pm
View
So I still don't get your problem with my answer. Stop talking gibberish and stick to trying to provide useful information as in your own solution below. Regards,
Ian.
Ian A Davidson
27-May-13 18:32pm
View
1) I don't care what screen you have or what Windows identifies yours as. Windows will "identify" it as whatever driver you have used for it. You could as well use a generic driver, but in that case if it does return a physical size, it is likely to be wrong.
2) I've not specified whether OP should use imperial or metric, merely provided some possible calculations to determine screen resolution (in either measurement system). I'm sure he is competent enough to convert, as indeed the sample code does.
The information I have provided is correct.
Cheers,
Ian.
Ian A Davidson
27-May-13 13:42pm
View
Spot on +5.
However, and I'm not sure about this, but my guess is that the reference returned by string.Empty will always exist, and you can access it by another call to string.Empty. :)
Ian A Davidson
27-May-13 11:43am
View
I don't see it. The link you have given seems to show the exact opposite. Firstly no where on the page is the default value assigned. The default assignment is not optimised away! In fact the example at the bottom of the page (which as it says is strongly discouraged), relies on the very fact that it is not optimised out - the fields have a default value before the assignment initialisation runs, otherwise the code wouldn't work: a is initialised to 1, because a and b are first assigned their default values of zero, before the initialisers of the class run, so when a's initialiser runs a = b+1 = 0+1 = 1, and then b's initialiser runs, b = a+1 = 1+1 = 2.
Regards,
Ian.
Ian A Davidson
26-May-13 18:03pm
View
Sorry you disagree. I was being polite. Some of the advice given would be good if the question was different. (Although, why you would bother with "number" at all in this case I'm not sure; I'd go with "public int Count { get; set; }" ) However, as the question stands, there is nothing here to help. Hence 1 star.
Ian.
Ian A Davidson
26-May-13 18:01pm
View
I didn't say "it is bad practice to always initialise variables and class members". It IS bad practice to initialise fields ("class members") to their default value, since they are initialised to that already, so you're doing it twice. If you want clarity, put a comment to state that the default value is what you want it set to. I.e. in the above case, "int number; // number initialised to zero". I don't believe this is a "clarity / brevity" issue, but a pure misunderstanding of the C# language and/or ethos. As to variables, you always need to initialise them, and the compiler will complain if you try to use one before it's been assigned to. All I can say is you have obviously never written a compiler for the CLR (at least, I hope not...)
Regards,
Ian.
Ian A Davidson
26-May-13 17:27pm
View
I agree with SA to an extent, except that I don't think we know enough about the rest of your code. My gut feel, however, is that your object model is wrong and needs some rework.
Regards,
Ian.
Ian A Davidson
26-May-13 17:24pm
View
While some of what you have here is generally good, this is not addressing the question at all. We don't know the rest of the code, or what he is trying to do.
Ian A Davidson
26-May-13 17:08pm
View
I love the way so many people seem to think that the compiler can/will "optimise" almost anything. It seems to be an excuse for many a bad piece of code and bad practice.
No offence intended, just trying to help people learn here, and that sometimes means having to refute mis-information.
Regards,
Ian.
Ian A Davidson
26-May-13 17:06pm
View
You solution says "uninitialized value ... [is] an absolute no go." If this was C++ I would agree with you, but this is C# so fields are never uninitialized, and the compiler is intelligent enough to tell you whenever you attempt to use a value that is not initialised, and won't compile in that case. Integers are initialised to zero (and, by the way, reference types are initialised to null).
Ian A Davidson
26-May-13 16:24pm
View
Manfred, your comment is still incorrect. In C# (or any other .NET language, i.e. if you're using the CLR), initialising a field (either static or instance field) of a class to its default value is redundant (except in some rare cases), and so you're just adding a performance hit to your application (admittedly probably only a problem if you are creating thousands of those objects, but it is good practice not to do it). Read here: http://msdn.microsoft.com/en-US/library/ms182274(v=vs.80).aspx
Regards,
Ian.
Ian A Davidson
26-May-13 12:41pm
View
I'm with Richard. Show us how to you are calling this.
Ian.
Ian A Davidson
26-May-13 12:35pm
View
What do you mean? I can't see anything wrong with this.
Ian.
Ian A Davidson
15-May-13 11:55am
View
Yes, I think that actually introduces an error. If number of days is less than 7 the result will be 1, because it is forced to 7 and 7/7 = 1. But if the number of days is 8 to 13 inclusive, the result will also be 1 because the integral value of 8/7 = 13/7 = 1. So there are now two weeks that are week 1.
Regards,
Ian.
Ian A Davidson
15-May-13 7:04am
View
Sorry if I am misunderstanding you. I agree that the question was "How can I find the week number of a given date?" The title is "Help to find out the Week number of year?" So the question I would put to you is, "what does a year's week number mean to you?" To me it means the number of the week of any year where each week starts on a fixed day (e.g. Sunday or Monday). The solution you have proposed assumes each week number is based on the day on which the YEAR starts. I.e. for a year where 1st Jan is Wednesday, even allowing for the typo in your code (I think you mean "60 * 60 * 24", not "60 * 60 - 24"), in your solution Monday 13th Jan (for example) would fall in week 2 (it would in fact return 1, since it is zero-indexed; which is fair enough): int((13 - 1) / 7) = 1 (+1 = week 2) - where the weeks are Wed 1st-Tues 7th; Wed 8th-Tues 14th. The standard answer in this case, however, should be week 3! Week 1 is: Wed 1st-Sun 5th; Week 2: Mon 6th-Sun 12th; Week 3: Mon 13th-Sun 19th. (OR, if starting on Sunday: Wed 1st-Sat 4th; Sun 5th-Sat 11th; Sun 12th-Sat 18th). I hope this helps. Regards, Ian.
Ian A Davidson
15-May-13 4:10am
View
Throwing dates at it: a few dates from each of the beginning of Jan and the end of Dec, for all the years between 1997 and 2015. :)
Ian A Davidson
15-May-13 4:08am
View
He asked for the week number in the year. Even if you'd specified the same year in your calculation, this answer as well as the other one doesn't take into account which day is the first day of the year - so a week here may be defined as Monday to Sunday one year, Tuesday to Monday another year, Wednesday to Tuesday another year, etc, etc.
Regards,
Ian.
Ian A Davidson
15-May-13 4:04am
View
Up'd to 5.
This is a good solution, provided that you are happy with the way the week number is calculated: being based on Monday as the first day of the week and the first week of the year being the first one which starts on Monday, (and the return value being a string).
Note that this means you will have week of year values from 0 to 53. And you can't simply add one to the week, since when the year starts on Monday it will already be week one, so you'd make it week two.
You can do the same calculation as strftime (COleDateTime) does by using this formula:
int week = ((yearDay + 8 - weekDay) / 7);
(Where yearDay = 0 to 365 and weekDay = 1 to 7; Monday = 1)
Regards,
Ian.
Ian A Davidson
15-May-13 3:58am
View
True. I was just saying that since you've used mktime once you can calculate the week without having to call it again and work out a "difference" - you already have that information (except, if you want to adjust the partial weeks correctly, there are a few cases where you would have to call it again). As we have determined, you can do exactly the same calculation as strftime does by doing this: int week = ((date.tm_yday + 8 - monToSun) / 7); Besides which, I've just realised your answer is actually incorrect as it determines the week number based on whatever day of the week is the 1st of Jan. I.e. if 1st Jan is a Wednesday, then weeks will be from Wed to Tues. I.e. Wed 1 Jan to Tues 7th Jan is week 1; Wed 8th Jan to Tues 14th Jan is week 2; etc. Regards, Ian.
Ian A Davidson
15-May-13 3:51am
View
Yes, Monday is the first day of the week in both, but the difference is that the ISO standard (note it's not "my" ISO definition - ISO is the International Standards Organisation!) defines the first week of the year as the week with the first Thursday, whereas the strftime function makes the first Monday the beginning of the first week.
The strftime function, as you point out, also doesn't adjust the partial weeks at the start and end. Note that although it's not been marked as the answer, this solution I've provided is the ONLY one given so far which does give the standard week numbers.
Who'd have thought such a simple question could have so many possibilities? :)
Cheers,
Ian.
Ian A Davidson
14-May-13 23:02pm
View
No worries.
I've only down-voted one of your answers. I've only really been looking at one question today; the one on finding the week of year. It's quite an interesting problem. And I down-voted your answer only to 3. Not because it's yours, but because after looking into the problem I honestly don't think that function works - it doesn't seem to provide either a "standard" result, nor the result the OP originally asked for. Although I could yet be proved wrong. And I do recognise it might be a useful function under some circumstances and it is a good find. In fact perhaps I will put the answer back to 5. But no, I haven't down-voted any other answers of yours. In fact I haven't seen any of your other answers. It was pure "coincidence" that I happened to see that question.
Don't be so paranoid! :)
You haven't offended me at all, and I think you are reading my responses rather negatively.
Be more positive :)
Take care,
Ian.
Ian A Davidson
14-May-13 22:40pm
View
Actually (sadly gripped by this aren't I!) I've just looked again at strftime definition for %W and it says "Week of year as decimal number, with Monday as first day of week (00 – 53)". So it would appear it doesn't take the locale into account.
I can also see now that it takes the first week of the year being from the first Monday in the year.
Therefore this calculation provides identical results (week resulting as 0 to 53):
int week = ((date.tm_yday + 8 - monToSun) / 7);
(Where date.tm_yday = 0 to 365 and monToSun is the week day, 1 to 7; Monday = 1)
You could replace that in my code above, but I'm not going to spend more time working out how to do the adjustment for that, as I believe my code is providing the ISO standard answer, matching what is presented in my diaries!
Regards,
Ian.
Ian A Davidson
14-May-13 22:08pm
View
Well, I ran a few dates from beginning of Jan and End of Dec through it for years between 1997 and 2015. Try it yourself and see what you get. Looking through all the years, when it returns 0, the "standard" week is actually 1, or 52 or 53 of the previous year. Sometimes when it returns 52 or 53 it should be week 1 of the next year. Some years the week number is out by 1 for the entire year, and sometimes it is largely correct, except for the few days at the beginning and/or end.
So it looks like it would still need some adjustment, though it is less clear what.
But anyway, I've enjoyed looking into this tonight - didn't realise such a simple question could have so many controversies (have you looked at the Wikipedia discussion page?). The basic calculation (week from beginning of the year) is straight forward.
I realise fitting in with Windows' locale options is important :) If you find a function that does it correctly, or some setting perhaps that sorts strftime out, by all means share it.
Regards,
Ian.
Ian A Davidson
14-May-13 21:24pm
View
Seems a good solution if wanting a string. Would go with strftime rather than COleDateTime, unless the necessary dlls, etc, are already loaded and initialised.
However, I note it does not deal with many of the edge cases - namely first and last weeks of the year, returning 0 and 53 in many cases where it should return 52 or 53 and 1 respectively.
Ian.
Ian A Davidson
14-May-13 21:18pm
View
This doesn't seem to be at all what was asked for.
Ian A Davidson
14-May-13 5:13am
View
There's really no need to go to the trouble of taking difference between the date and the first of January. Once you've called mktime, the tm structure you passed will have been completed to contain the day of year and the week day, so you only need to call it once for the date you are interested in and then calculate based on that. See my solution :)
Regards,
Ian.
Ian A Davidson
14-May-13 2:05am
View
Not particularly. Just rather surprised to see someone going through questions marked as answered telling people who answered them that they shouldn't be wasting their time and that they are cluttering up the forum! Cluttering the forum with comments? Surely the forum is more cluttered by the number of unanswered questions, and the number of terrible questions just asking for code. At least this guy had tried to do some coding and was asking a genuine question. I answered trying to show (a) the different options available to him (because it's not quite clear what he was trying to do) and (b) some principles in writing clear and neat code. (Not that if I was writing a linked list I would write it this way). As to OriginalGriff's answer, perhaps he did provide a better "guide" rather than simply providing code, but whether he did or not, his solution wasn't there when I posted mine (as you yourself said, I put in effort - that takes time - and sometimes by the time I've replied to a question others have already posted their own solutions). So we both answered the question in our own way. Which is "more helpful" I guess depends very much on your point of view. Personally I think they compliment each other rather nicely.
Regards,
Ian.
Ian A Davidson
14-May-13 1:27am
View
Deleted
That's nice. I'm glad for him. Have fun.
Ian.
Ian A Davidson
14-May-13 1:08am
View
Sorry - can't always tell what is homework - can you? Really? And this guy had put a lot of effort in, so felt it was good to help. Judging by the number of terrible questions and code presented to this forum, I get worried that there are going to be a huge number of extremely bad coders out there in future - just trying to help stem the tide!
Ian A Davidson
13-May-13 20:01pm
View
Glad to hear it Steven. Could you please use the "Question or Comment?" button to reply, rather posting comments as solutions - I only saw this by chance! - and mark the solution as answered, if indeed it is. Thank you! And all the best with your program.
Regards,
Ian.
Ian A Davidson
13-May-13 19:55pm
View
Homework?
Ian A Davidson
13-May-13 19:23pm
View
PS. If compiling to run on x86, you don't need to worry about the Wow6432Node - windows will automatically look there instead. However, if you are compiling for x64 or "Any CPU", then you will need to. The best answer would be to look in both places. I'll update the code above with this change. Ian.
Ian A Davidson
13-May-13 6:17am
View
I meant use a strongly typed collection, like List<T>, not List. I.e. if you're using it to hold strings, how about using List<string>, or maybe StringCollection? I would suggest you rename your MyData method and property so that they don't have the same name (that is just bound to end in confusion!) However, these things in themselves is not going to fix the indexing issue you have. The code you supply here will add elements, but as Lukeer has pointed out, you'll end up with one every time you change the text (therefore including incomplete entries). How about a "submit" buton ("OK"?) to assign the change instead - or possibly detect the change in focus? Obviously I'm not sure what you're trying to achieve by added the text from a text box into a list! Do you really need to do this, bearing in mind that you have the text in the text boxes when OK will be clicked? I think you need to rethink what you are actually trying to do, do a bit of rework - as Lukeer has suggested - and then if all else fails, do some debugging. Regards, Ian.
Ian A Davidson
13-May-13 6:07am
View
Thank you lukeer, for correcting my typo. :)
Ian.
Ian A Davidson
11-May-13 13:51pm
View
Okay. I wouldn't have spotted that. I never used scanf because it's such a flaky function. But I hope you take note of the code I've supplied. The insert function provided in the question doesn't make much sense.
Regards,
Ian.
Ian A Davidson
11-May-13 9:56am
View
PS. You do know that your "start" node in your example doesn't have a 'info' value set don't you?
Ian A Davidson
9-May-13 7:46am
View
Take a look here:
http://stackoverflow.com/questions/4279532/methods-of-storing-application-data-settings-without-the-registry
There's a link there that points to an interesting looking project on CodeProject itself:
http://www.codeproject.com/Articles/12914/INI-Reader-Writer-Class-for-MFC-and-ANSI-C-Windows
I may have been a bit hasty in saying that you should not ever use those functions (as someone on that first forum page points out, the functions will probably never disappear). However, it is clear that even if using ini files through those functions, there is some interaction with the registry (it seems they look in the registry first!) therefore they can't be the most efficient. You are better off either using the registry directly as suggested, or writing the file youself.
If it really must be an "INI" format file, then perhaps try looking that CIniFile, linked above. However, if it is simply a case of needing to save a couple of values in some sort of initialisation file which needs to be modifiable by a user, then you could simply write them as a couple of values in a plain text file (and give it a generic extension, such as ".dat"), or define your own format, or use a well structured and documented specification like XML.
Regards,
Ian.
Ian A Davidson
8-May-13 19:58pm
View
You see black on black because you've copied and pasted from the question and left the line "Collapse | Copy Code" in, which messes up the interpretation of the styles. If you remove that line you will find that it shows as white on black.
Regards,
Ian.
Ian A Davidson
8-May-13 19:46pm
View
Alt + 0225 (on the number pad) = á :)
Ian A Davidson
8-May-13 7:39am
View
Is the title you've used misleading?
It sounds like the problem you want solved is how to call your application which creates a pdf file from a web page (probably a CGI script?) In which case the "scheduling" is more about checking the last time the pdf was created, and either returning the pre-created one or creating a new one if the month has changed?
So the question is actually about how to create the PDF file on request as needed?
Am I right?
Ian.
Ian A Davidson
6-May-13 15:18pm
View
Is this homework?
Ian A Davidson
6-May-13 4:51am
View
Sounds like homework to me. Have you tried anything yourself yet?
Ian A Davidson
6-May-13 3:57am
View
Very little knowledge here, but my guess is that you need to do some validation on "bytBLOBData" to ensure that no SQL code can be "injected" by means of the file that the attacker specifies - e.g. to start with, make sure that any special characters are correctly escaped, etc, so that the attacker cannot use things like quotes and brackets to add additional clauses.
But I fear someone who has more experience of SQL with C# would need to explain exactly what is required.
Regards,
Ian.
Ian A Davidson
6-May-13 3:38am
View
Have you resolved the problem and got it working?
Well done.
All the best,
Ian.
Ian A Davidson
5-May-13 21:10pm
View
The result depends on how you implement GetWidth. In my example above, it is the same since I simply return "width". However, you could choose to implement it differently, for example by getting the value from some bitmap header structure or something that your class manages. This is another reason to use methods rather than accessing variables directly.
Did you see what I said about initialisation? The variables are not destroyed by the time you reach the line on which you say they are. I am wondering why you think they would be. Are you sure you just haven't initialised them correctly?
Regards,
Ian.
Ian A Davidson
5-May-13 20:52pm
View
+5 if I could. If you have time, why not post a solution, or a link to one, explaining how to do it?
Ian A Davidson
5-May-13 20:52pm
View
And now I've formatted it, we can see there is also another single quote missing after Lname, before phone number. +5.
Ian A Davidson
5-May-13 20:37pm
View
This is C++, not C#.
Ian A Davidson
5-May-13 20:15pm
View
This is wrong for C++. (You are probably thinking C#?)
"img" isn't a pointer to a LoadImage object.
"LoadImage img;" is creating a LoadImage object - it will create an object of LoadImage using the default constructor. Whether this has been implemented correctly to initialise the members is another matter, and I doubt it actually loads an image (unless there is some default specified) ;)
Regards,
Ian.
Ian A Davidson
4-May-13 4:42am
View
Use the Windows event log (either the general Application log or create a dedicated application one).
Ian A Davidson
4-May-13 4:39am
View
Number one reason I think is because you are asking for free code of something that could have taken someone months to develop. If there's not a project group online for this somewhere who are offering it for free, then you have two options: 1) develop it yourself, or 2) find out what company has developed such software and buy it from them.
People are entitled to be paid for the work they do - "The worker is worthy of his wages."
Regards,
Ian.
Ian A Davidson
4-May-13 4:28am
View
Looks like you are missing a closing bracket.
Ian A Davidson
4-May-13 4:27am
View
Short answer. No.
Ian A Davidson
4-May-13 4:25am
View
Really? I'm not the person asking people on a coding forum to write a program for them without giving any specification of any kind whatsoever, so that no one could help even if they wanted to, which I suspect they wouldn't since this is YOUR homework. If you want to learn how to program you have to program - you don't ask other people to do it for you.
Ian A Davidson
4-May-13 3:42am
View
What version of VS are you using? Have you installed all the features?
Ian A Davidson
4-May-13 3:40am
View
That's nice. I hope you enjoy creating your program. (Whatever a "train trolley dolly" is...)
Ian A Davidson
4-May-13 3:16am
View
You need to fix your code, not hide the error. This indicates a serious coding mistake somewhere.
Ian A Davidson
3-May-13 18:53pm
View
You would need to do this instead:
int value = (int)Math.Ceiling(Math.Abs(15) / 20.0);
I've left your call to Abs so you can see the difference.
Ian A Davidson
3-May-13 18:40pm
View
Because, as I said, it's doing integer arithmetic so loses everything after the decimal point.
15 / 20 = 0
15 / 20.0 = 0.75
Ian.
Ian A Davidson
3-May-13 18:30pm
View
Sorry, but I've spent rather a lot of time on this, in the hope that it may have been just the extension method issue that you had a problem with, but I'm afraid SA was right that you obviously do not know what you are doing. I don't have time to write your program for you! I don't know what you are trying to do, especially if you don't. I assumed you had an HtmlDocument loaded, or knew how to load it. Why are you working on extension methods when you haven't even got that far? Do you even know what you are trying to achieve? I'm afraid I can't help any further. It sounds like you need to get a book on C# / programming in general. Sorry.
Ian A Davidson
3-May-13 17:56pm
View
You need to work with double, not int. Conversion to int always loses the decimal place. Thus:
int x = -16 / 20; // x will be 0
double y = (double)-16 / (double)20; // y will be 0.8.
Note that at least one of the operands in the expression also need to be double (in this case cast to double) - it doesn't hurt to ensure both are, since the compiler will do the cast anyway. Otherwise integer arithmetic will be used and then simply assigned to the double. I.e.
double z = -16 / 20; // z will also be 0
Specifying the decimals part also causes it to interpret the value as a double, hence the answer is right.
double y = -16 / 20.0; // y will be 0.8.
By the way, I don't understand your question either, because 5 / 20 will also give the answer of zero, not one.
Regards,
Ian.
Ian A Davidson
3-May-13 17:43pm
View
You've lost me completely, sorry. Do you even know what you are trying to do? Perhaps you should edit your question, supplying the code of your latest extension method and the method from where you are calling it. But it sounds like whatever you are doing is beyond you at the moment and you need to practice some basics of the language as I said before.
Regards,
Ian.
Ian A Davidson
3-May-13 17:36pm
View
He got me on "Reading pixel data ... without loading it into memory". :D
Ian A Davidson
3-May-13 17:31pm
View
It seemed to make sense. Thank you SA. :)
Ian A Davidson
3-May-13 17:31pm
View
How are you loading up the HtmlDocument? Don't tell me you've left it as null.
Ian A Davidson
3-May-13 13:35pm
View
I'm too good to you! I have updated my solution to be more explicit, giving an example of how you would call the method. I hope it is clear now, otherwise, give up for now until you understand some more of the basics.
Regards,
Ian.
Ian A Davidson
3-May-13 13:17pm
View
No. You need to replace "TestProg" in my example with your program's or module's namespace. If it's not in the same namespace from where you are calling it you would most likely add a "using" statement to include the namespace.
Think about it this way: how would you call the method if it didn't have "this" declared in the first parameter?
public static HtmlElement GetElementsByName(HtmlDocument document, string classname) ...
You would do:
GetElementsByName(doc, "TheClassname");
With an extension method, it's similar - just that you're calling it "on" the parameter which you have declared with "this", and then don't pass that as a parameter:
doc.GetElementsByName("TheClassname");
It does sound like you need to do some basic language learning before you start trying to create and use extension methods!
Regards,
Ian.
Ian A Davidson
3-May-13 12:37pm
View
You call it as if it's a method on the class you're extending, in this case HtmlDocument.
I.e. if you have:
HtmlDocument doc /* = etc */;
You would simply do this:
doc.GetElementsByName("TheClassnameToBeLookedfor");
Regards,
Ian.
Ian A Davidson
3-May-13 11:16am
View
Yes. (You could even possibly start at zero, so you would have 256 permutations). so using your example you would have: 151030 will be 1500 151070 will be 1501 152930 will be 1502 152970 will be 1503 171030 will be 1701 etc. So if you had a lot of 15xxxx, your new numbers would run from 1500 to 15FF. Regards, Ian.
Ian A Davidson
3-May-13 11:15am
View
Deleted
Yes. (You could even possibly start at zero, so you would have 256 permutations). so using your example you would have:
151030 will be 1500
151070 will be 1501
152930 will be 1502
152970 will be 1503
171030 will be 1701
etc.
So if you had a lot of 15xxxx, your new numbers would run from 1500 to 15FF.
Regards,
Ian.
Ian A Davidson
2-May-13 7:55am
View
No worries. I'd just logged on and it was the first question I saw :)
I've just read your second question and made an addition to my answer about that.
I hope you will enjoy learning VC++.
Regards,
Ian.
Ian A Davidson
1-May-13 19:37pm
View
1) "Run from explorer" - as per the OP's question - "clicking on the exe file from the explorer window".
2) That is NOT the meaning of the application type. Look up C# compiler options.
A target of "exe" (console application) will set the Subsystem field of the PE header to 3 (WINDOWS_CUI - i.e. Windows Console UI); the image will run this in the Windows character subsystem.
A target of "winexe" (Windows application) will set the Subsystem field value to 2 (WINDOWS_GUI); the image will run in the Windows GUI subsystem.
Regards,
Ian.
Ian A Davidson
1-May-13 17:57pm
View
The application type changes the start-up code that is injected (the CLR host) into the executable when it is compiled to IL.
I see that a Console application can indeed appear to be a "Windows application", in that it can display message boxes, windows, etc, but it is still running within the console which will be displayed if "run from explorer".
This may be what the OP wants.
I have read of some hacks to hide the console in this case, which don't seem to be ideal to me, and I don't know of any method to determine how the application was started other than providing a command line switch.
To my mind, having two applications is clearer and better, and if it's been written properly, with the code that does the work separated out from the "main" method, you won't be doubling up. If anything I think you would often end up with better code because the "business logic" would have been written correctly without any assumptions about how output is displayed. We work exactly like this when developing services - the service code is just a wrapper around the code that does the actual work, which can then be incorporated into a console application for testing.
Anyway, at least I have learned something from this discussion, so even if it went over and above what the OP was asking about, thank you.
Regards,
Ian.
Ian A Davidson
1-May-13 17:09pm
View
Deleted
My comment was to Zoltan. He implied that I for one had not read the question.
Ian A Davidson
1-May-13 17:08pm
View
I'm not confused thank you.
Yes, I'm sure there are other types of application.
But can "any" application be, as you put it, "windowed" and "console" at the same time? If so I for one would like to know how. And I mean genuinely "windowed" or "consoled", not "run as console but then hide it" or "run windowed but show the console". I.e. can an application run as a "windowed" app in one mode, and as a console application (that could be run from a batch script for example, and outputs to the console, etc)?
Regards,
Ian.
Ian A Davidson
1-May-13 17:04pm
View
Deleted
Good answer though SA. I just want to let you know that I am not the one who voted it down.
Regards,
Ian.
Ian A Davidson
1-May-13 17:03pm
View
You really think he's only asking if he can START an application from the console and/or from Windows? Do you REALLY think that? Well, I know there are some dumb questions on here, but I simply made the assumption that someone in a coding forum would know that it is possible to launch a windows app from the console, so it is more logical that he's asking whether an application can operate as either.
Furthermore, I'm sure simply showing the console in Windows is different from running in the console.
Ian A Davidson
1-May-13 16:59pm
View
Deleted
Rude. You think I didn't read the question? Of course I did, but it seems I'm the only one who made an assumption that he's not so thick that he doesn't think you can't start a Windows application *from* the console. Maybe he is, perhaps he could clarify the question.
Ian A Davidson
1-May-13 13:28pm
View
What is to prevent someone from running the application from the console without parameters, or from running it from explorer with parameters by creating a shortcut or by simply dragging and dropping one or more files onto it?
Apart from that. if you compile as a Console application it would open a console window if run from within Windows (apparently there are ways to give the impression of not being a console application by hiding or "freeing" the console), and a Windows Forms application will return control immediately to the console after starting (and cannot output to the Console) so that neither would operate as if they are the other type
Regards,
Ian.
Ian A Davidson
1-May-13 2:34am
View
That is a good point though: for the sake of the person using the code it probably shouldn't be const because it is modifying the data of the class.
But technically, the class member is a pointer, so by declaring the method const, the pointer becomes const and the function cannot change it, which it doesn't. It doesn't prevent the method modifying the content of what the pointer points to! So actually no the compiler doesn't complain.
If the implementation was different it might matter; so for that reason I think I would agree with you in this case.
It's an interesting example of good OO design versus programming language behaviour.
I'll update my example with your recommendation.
Thanks for that.
Regards,
Ian.
Ian A Davidson
30-Apr-13 17:58pm
View
Sorry, but I feel that most of the answers to the questions you've asked here are answered by, or can be arrived at from, the solution I provided last night.
You have attempted to write a class using OOP and yet you are asking whether it's possible to return to procedural programming (defining functions where you have to repeatedly pass the parameters in yourself). Of course it's possible, but why would you want to?
The only case I can think of is if you want to write some "extension methods" (to borrow C# terminology) to a class which you cannot modify - for example if you decide to scrap this class altogether and use vector instead. Then you might define the functions in terms of taking a vector. You could then, in fact turn them into template functions. Now that might be cool.
As to the Bubble sort not working: it does work - I tested it last night as I was reworking your broken version which tried to access memory outside of the array.
It may not be the most efficient way to sort, but it does sort the values.
I note that you don't output the array again after the call to "Bubble", which I expect is why you're not seeing it.
Constructor/destructor: A constructor is used to initialise member variables and allocate resources that the class needs. A destructor is used to free those allocated resources.
In C++ I would recommend that you should get into the habit of ALWAYS defining a (virtual) destructor even if it does nothing, so that you don't forget that you may need to do something in it. If there is nothing for it to do, put a comment in it to that effect. (This is Scott Meyers' approach of "Never doing 'nothing' silently").
In your case I note that you still haven't implemented the destructor as per my example, so if this application were more than a simple 'main' function creating the one object and then exiting, the memory leak you have introduced by not having one could well build up and up. When you write a class you should be doing your utmost to ensure that it will cause NO memory leak whatsoever.
I mentioned that you could use vector instead of allocating the memory for the array yourself. That would also simplify your life with respect to copying - you wouldn't need to define your own copy or assignment methods as the default compiler defined ones would work automatically, since vector manages its own copying. (You also wouldn't need to free it in the destructor).
Regards,
Ian.
Ian A Davidson
30-Apr-13 3:59am
View
So why don't you do it then? I don't understand the problem.
Ian A Davidson
29-Apr-13 10:27am
View
+5.
I cannot confirm either if the declspec declaration will work - I was trying to find if I had an example, but surprise, surprise, I don't.
I think I'd go with the latter option anyway, as you can better hide your implementation.
Ian.
Ian A Davidson
29-Apr-13 9:26am
View
That is well spotted that the decrement will already have been applied! Well done.
"--x" is the same as "x = x-1", so in this case "--this->tail" is the same as "this->tail = this->tail - 1", so the code is doing "this->tail = (this->tail = this->tail -1)" where the ending result is the same, but the assignment is being done twice, as if he'd written "this->tail = this->tail - 1; this->tail = this->tail". And this effect is the same whether the variable is static or not!
(-- is being applied to "tail", not "this", because operator precendance dictates that -> operator will be evaluated before the prefix -- operator)
The "this" has no place (is utter nonsense, as you put it) because the variable is static. So "this->tail = --this->tail", if it were to be kept should be replaced with a simple "--tail".
However, the point I made regarding this remains, in that I don't believe he would have any idea what "tail" would end up pointing to. It's obviously meant to be a linked list, having a "next" pointer, in which case it obviously cannot be guaranteed that the "previous" pointer is immediately before "tail", otherwise what's the point of "next".
Regards,
Ian.
Ian A Davidson
28-Apr-13 20:05pm
View
Thank you. I was just trying to get him to stop. Sorry about that, I will know in future not to do that. Hope it can be cleared up.
Ian.
Ian A Davidson
28-Apr-13 19:45pm
View
Deleted
We're not interested in your stupid stream. GO AWAY.
Ian A Davidson
28-Apr-13 11:44am
View
I'm afraid I don't know, I've not had to do anything like that.
I hope you have success in your endeavours.
Ian.
Ian A Davidson
28-Apr-13 11:28am
View
Nope. It's irrelevant.
How the code is written will depend on whether you can compile it in both 32 and 64-bit, but it doesn't change how you declare the dll exports / imports. My answer below will work equally for either. Indeed, I compile all my dlls in both 32 and 64 bit.
Ian.
Ian A Davidson
28-Apr-13 4:46am
View
Beat me to it :)
Ian A Davidson
28-Apr-13 4:40am
View
I tried looking at Git once. It seemed horrendously complicated. Then later when I discovered SVN I've not looked back. I'm not sure why anyone would want to change!
:)
Ian A Davidson
28-Apr-13 4:37am
View
Looking at this code, don't you even have an idea how to do it?
I know it's not as simple as it looks if you are trying to limit the final size of the e-mail (because attachments will be encoded - e.g. base-64 encoded, which will increase their size significantly) but even so, you must have tried keeping track of the total size of the raw files and e.g. not adding the attachment/s if it/they would exceed the size limit, displaying a message to the user instead? Are you after some code to try to calculate the exact final message size?
Regards,
Ian.
Ian A Davidson
28-Apr-13 4:21am
View
Helpful link.
Ian A Davidson
27-Apr-13 20:37pm
View
+5. Even though the question seems not to have been resolved, you obviously know what you are talking about and been able to provide some good ideas and suggestions. :)
Ian.
Ian A Davidson
27-Apr-13 20:28pm
View
+5. Yes, a dropdown box would make more sense. Possibly even a date picker (which I believe will automatically adjust to the user's language). But the links provided, and the example expression, if they must stick to the regular expression method, also look helpful.
Ian A Davidson
27-Apr-13 19:47pm
View
He says it is Windows, and I don't think the IDE matters unless more detail is required on exactly how to do things.
Ian A Davidson
27-Apr-13 0:39am
View
Apologies for the double post. I'm not sure why that happened - I only clicked "submit" once!
Ian A Davidson
26-Apr-13 14:03pm
View
Could you clarify what you mean, please, by only being able to loop to a fixed number of rows? Are you trying to display 16 lines at a time on screen, or do you want to create more than one file with 16 lines in each?
Regards,
Ian.
Ian A Davidson
25-Apr-13 17:21pm
View
The UI will "hang" for as long as you are performing anything in that thread - i.e. it sounds as if in this case the loading of the dll (which from your description then creates another thread to do the work in). The reason is the WinProc / DlgProc function, which is what handles windows messages, is in that thread. Any long-winded process which you don't want to cause the UI to "hang" will need to be in a separate thread in the first place.
Regards,
Ian.
Ian A Davidson
25-Apr-13 17:01pm
View
Any time.
Ian.
Ian A Davidson
25-Apr-13 16:59pm
View
Good point. I was wondering about it - I just noticed that the encoding on this page turned it blue and looked it up here: http://msdn.microsoft.com/en-GB/library/2e6a4at9(v=vs.80).aspx I didn't spot that it was just a CLI thing.
Personally I've not done C++ with the CLI (what's the point when it is so different from proper C++, and if you want to use the CLR, C# is so much nicer).
But still, even if you're doing proper C++ in Visual Studio it's probably best to avoid it. Besides, as you indicate, one should use variables which indicate what they are for, rather than what type they are.
Regards,
Ian.
Ian A Davidson
25-Apr-13 4:41am
View
Yes, that is possibly what he wants to do, but to be honest there are so many problems with this code, who knows?
Ian.
Ian A Davidson
24-Apr-13 20:26pm
View
No, you need to change it to: st[0] = '\0';
Arrays are zero-indexed, so when you create an array of 1 item, its index is 0.
I suspect TC is lagging behind with security checks, or perhaps the default compiler options are more relaxed. VC++ has been much improved over the years and default compiler settings more secure - e.g. many stack checking features are on by default, especially when compiling in debug. Look at what switches are set and look them up in MSDN to find out what they do, and familiarise yourself with what the defaults are in release and debug mode.
Regards,
Ian.
Ian A Davidson
24-Apr-13 13:12pm
View
Thank you. :)
Ian A Davidson
24-Apr-13 11:06am
View
Oh yep. Sorry I "checked" that by doing a comparison, but obviously I did something wrong there. Oh dear. Changed it to a 5 :D
Ian A Davidson
24-Apr-13 10:44am
View
+4. Correct result but wrong reason. He's resetting p2=p1, so he's pointing at the beginning of the array, but then in the second loop he's doing p2-- so it's (presumably) outputting the first integer (1) and then pointing to random information before the array.
Regards,
Ian.
Ian A Davidson
23-Apr-13 14:25pm
View
Once again, someone down-voting without any comment as to why. That is most disappointing. If there's something wrong with my article, please say so.
Thanks,
Ian.
Ian A Davidson
23-Apr-13 12:00pm
View
It's labelled "Solution 2" below, and it has been marked as the answer.
Ian.
Ian A Davidson
23-Apr-13 9:17am
View
No, I understood the question. I'm asking "Why?".
why would you want to write a C# program, which then tries to control the cmd environment to do things which you can easily do directly from the C# program itself (see Griff's solution).
If you want to do it in the command line, write a .cmd (batch) file instead.
Regards,
Ian.
Ian A Davidson
23-Apr-13 3:50am
View
Why would you want to start the command line to do it? Why not just execute the required executable with the required parameters?
Regards,
Ian.
Ian A Davidson
21-Apr-13 8:01am
View
What are you using to install it?
Ian A Davidson
20-Apr-13 12:35pm
View
I think that's what the others have been saying - you need to run your application as local admin.
Regards,
Ian.
Ian A Davidson
19-Apr-13 7:38am
View
Do you know how to draw a line? I hope so. I think your question is more about trigonometry than coding. You should look into that. Hint: 6 sides => each side is 60 degrees from its adjacent sides. Regards, Ian.
Ian A Davidson
18-Apr-13 7:56am
View
You need to supply more information and preferably a code sample indicating where the exception is being thrown.
Regards,
Ian.
Ian A Davidson
15-Apr-13 4:03am
View
Good answer. +5.
Ian A Davidson
15-Apr-13 3:48am
View
Rather than just "downvoting", it would be more helpful to have some proper feedback / comments.
The idea of this forum is to help people improve their coding skills.
You'll find the example of declaring non-implemented constructors in Scott Meyer's books (Effective C++ or More Effective C++, I can't remember which) as mentioned by H.Brydon in Solution 1.
Thanks,
Ian.
Ian A Davidson
14-Apr-13 11:56am
View
Hi Dpk_Patel,
I'm rather curious that you supplied your own answer to your question!
Are you asking whether this is a good example?
There are a number of things I would change (apart from the name of the class itself, which I assume is just for this example) - see below for my reworking of your example. But my main comment is with the use of const, or lack thereof.
Firstly, in the copy constructor you should declare the reference const. That is how the compiler would generate it for you if you don't explicitly declare it; by declaring it yourself you're overriding this, and in this case it means that if you ever have an object of the "code" class that is itself constant, you won't be able to copy it.
E.g. this won't compile:
const code A(100);
code B(A); // Compiler error
You should get into the habit of correctly declaring const any parameters (especially references or pointers) which the method doesn't modify, for the same reason.
Also, you should get into the habit of declaring methods const when they don't modify the class, so that they can be called on a const object of that class.
See my example below.
By the way, in this example we don't actually even need to declare a copy constructor as the compiler generated one works fine to copy your member variables. You need to supply a copy constructor when the class contains pointers or handles or some other such member variables that need special treatment in handling.
(In fact this is why you don't often see them in code. You are more likely to see them as prohibitions of copying, as per the example in my solution. This is often done when getting an object of the class to copy correctly, such as when it contains handles or the like, would just be too complicated or potentially too costly. If it wouldn't normally be needed, then prevent it. At the very least by doing this, if you find yourself trying to copy an object where copying is prevented you know that you either need design the code differently, or work on the copy method for the class to do it properly).
With all that said, here's my reworking of your example. I hope it will help you to learn some things.
Regards,
Ian.
#include <iostream>
// Note the most common and accepted naming standards are used here:
// Class and Method names use Pascal case.
// Member and local variables (objects) use camel case.
class Code
{
private:
int id; // Variable members should be declared private.
public:
Code() // Default constructor
: id(0) // Initialise id to something, otherwise its value will be undefined
{
}
Code // Parameterised Constructor
(
int a
)
: id(a) // Note also that we use the initialiser wherever possible...
{
// ... rather than assigning here. Initialisers are more efficient.
}
Code
(
const Code& x // const reference because we don't modify it!
)
: id(x.id) // This class has access to all private members, even on another instance!
{
}
void Display() const
{
std::cout << id;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
const Code a(100); // Example const class.
Code b(a);
Code c = a;
std::cout << "\nA: ";
a.Display();
std::cout << "\nB: ";
b.Display();
std::cout << "\nC: ";
c.Display();
std::getchar();
return 0;
}
Ian A Davidson
13-Apr-13 15:19pm
View
Quite. You would need to define m_len before m_str.
However, I don't see the relevance to the question being asked. (Not that I'm sure I even understand the question!! ;) )
Regards,
Ian.
Ian A Davidson
13-Apr-13 15:05pm
View
You should use interlocked methods to increment and decrement the count value as atomic actions (this is important because of the very nature of this class the value can be accessed by other threads). I.e. use Interlocked.Increment(ref NumDogs); instead of NumDogs++; and Interlocked.Decrement(ref NumDogs); instead of "NumDogs--;".
In fact, there is an example of exactly what saimm is asking for (showing the difference between using the interlocked and non-interlocked methods) on the MSDN page of Interlocked.Increment: http://msdn.microsoft.com/en-us/library/dd78zt0c.aspx
I'm going to post this as my answer.
Regards,
Ian.
Ian A Davidson
13-Apr-13 14:44pm
View
The class members will be constructed in the order in which they are defined in the class (i.e. in the class definition in the .h file), regardless of how you then order them when you implement the constructor. But I didn't see this asked in the question? Regards, Ian.
Ian A Davidson
8-Apr-13 11:13am
View
Still not convinced it's a very good question. Where and how is "VariableNotInTheThread" defined? You say "Look how this is not created in the thread", but you don't have a thread in your example - From where are you calling this "ReloadChunks" method? If you're accessing a different object instance from your thread then of course the variable will not be the same because it will be part of the other object. You could make the variable static, but then for anything but the simplest of assignments you will need to ensure that you use suitable locking access (e.g. Interlocked.Exchange). Keeping the variable it non-static, if you access the same instance of the object created in one thread from another thread then it will be setting the same value. You would still of course need to make sure your class is thread-safe (again, for example, by using appropriate locking mechanisms when accessing shared data). Regards, Ian.
Ian A Davidson
6-Apr-13 15:48pm
View
Use the ToString method to format the DateTime. Don't make work for yourself as well as for anyone maintaining the code!
Ian A Davidson
3-Apr-13 12:50pm
View
+5. Well found.
Ian A Davidson
3-Apr-13 12:49pm
View
Reduce performance from what? If you want to append to XML you will have to read a certain amount of it in anyway, in order to ensure you're putting stuff at the right point so that it does not become invalid XML, as Rob has pointed out. If performance is an issue, you're probably storing the information in the wrong format to start with - how about using a database? However, if you want to stick to using an XML file, the solution Pallavi has found looks like a good one. Or, if you are always writing nodes in the same place at the end of the file you could possibly write your own code to "seek" to the last tag from where you want to rewrite the ending. However, as far as performance goes, appending a line (e.g. text-delimited) to a plain text file would be quicker than either of those methods, since you can simply open for appending and move to the end of the file before writing it. It depends what you are using the data for, and how easy it would be to parse it back in. Regards, Ian.
Ian A Davidson
3-Apr-13 4:28am
View
+5 for Parse, TryParse. I don't know what the "convert" code is supposed to be here - it won't work. Parse, TryParse, ParseExact or TryParseExact are the way to go; the one you use will depend on what you need to do and what the likely inputs will be. You should, if you like, "compare and contrast" those methods of the DateTime class. Regards, Ian.
Ian A Davidson
29-Mar-13 5:52am
View
I'm sorry, perhaps I've not understood the question. If you have some XAML, then you can just save it and load it back as you would ordinary XML. Regards, Ian.
Ian A Davidson
28-Mar-13 6:39am
View
Deleted
I agree. The question is ambiguous. Are you literally trying to "convert" a single TCHAR into a string (i.e. so you have a string of one character) or do you mean that you want to convert a wide-character string (e.g. a TCHAR* or TCHAR[] that's been compiled in unicode) to a multi-byte string?
Ian A Davidson
28-Mar-13 6:10am
View
Voted 5. Having a user application that communicates with the service is the only solution I can think of as well. WCF seems to be the current standard, as you mentioned.
Ian A Davidson
28-Mar-13 6:04am
View
Because XmlDocument is not a static class.
Ian A Davidson
28-Mar-13 6:02am
View
Why on earth would you even want to do this?
Ian A Davidson
27-Mar-13 14:14pm
View
You make it sound like there are two irreconcilable statements here, but it is only because your assertion is incorrect. If "one wants to access the same object from different threads" then the object MUST be thread safe.
Ian A Davidson
26-Mar-13 12:24pm
View
+5 for the answer. However, Member9941061 please note 1) you shouldn't need your method to be public, because you're calling it from a private method; 2) see Jeff's solution. Why do you need to construct an array of the values in your text boxes anyway, when you have direct access to them?
Ian A Davidson
26-Mar-13 7:49am
View
Hi dilzz, I don't know asp.net, so I'm not posting this as a solution, and I don't quite understand what you're trying to do. What do you mean by "select"? If you mean when a user hovers over it with a mouse, then there is no need to change the class. Use the ":hover" selector in your CSS. E.g. "a.shortcut-dashboard:hover
{
background-color:yellow;
}" Check out all the available selectors, e.g. here http://www.w3schools.com/cssref/css_selectors.asp
However, if you're trying to show the page selected, then I'd do this when I generate the html page itself, changing the class of or style the element as you have suggested.
Ian A Davidson
26-Mar-13 6:36am
View
Sorry. I deleted my solution. I should have noted that it says "Linux". I'm afraid I'm rather rusty on Unix.
Ian A Davidson
26-Mar-13 5:48am
View
Could you clarify what you mean? Do you mean you want to run the exe FOR 24 hours, or every hour for 24 hours or what? (If you want an exe to be running permanently, I'd suggest Windows Task Scheduler is not the way to go - write a Windows Service instead).
Ian A Davidson
26-Mar-13 5:11am
View
In that case your question is wrong. You need to describe how the file is being sent to you and what you are doing to receive it - extract of your code would be ideal. E.g. is it being dropped in a folder on your system, sent by WCF, SOAP, etc, dropped by carrier pigeon????
Ian A Davidson
24-Mar-13 17:41pm
View
There are definitely problems with this. You shouldn't need to make your application give focus to every control in order to get the data. What a terrible user experience you're going to present to the user! What if they want to modify several items, they enter one piece of data, then suddenly find the focus flash through all the fields and, presumably, end on the last one. Now they need to click back to another item, and it happens all over again. And apart from that, you say "process all controls as if the user would do when the form is first filled". This is a GUI application - your application should never assume that the user will visit EVERY field or in the order you're presenting it to them. It's not a command line application where you ask for each item sequentially.
Seriously, you need to redesign this.
Ian A Davidson
18-Mar-13 5:07am
View
As nv3 has pointed out, you're comparing two pointers, not the strings themselves. Since you are assigning them in the same order, that would explain why you always get the same result. You need to scrap your implementation of strcmp and use the standard one. Then output what you want in each case - e.g. int diff = strcmp(red, black); if (diff < 0) { /*cout*/ } else if (diff > ) { /*cout*/ } else { /*cout*/ }
Ian A Davidson
18-Mar-13 4:57am
View
Yes, well done for noticing that!
I probably missed it because the arguments are declared as arrays rather than the pointers that they are (which I thought is a bit weird) and maybe I've been in C# world too long now and would never pass a char array. But mainly because the idea of redefining strcmp was just so bizarre along with the other errors I didn't look that closely at the actual logic...
Well done nv3.
Ian A Davidson
15-Mar-13 7:01am
View
Very interesting. Thanks for this link. This should be listed as the answer to this question!
Ian A Davidson
14-Mar-13 12:28pm
View
It does sound like you are missing the include of the header file where "rand" is declared.
Ian A Davidson
14-Mar-13 12:22pm
View
With all due respect, you have obviously not read the question.
Ian A Davidson
14-Mar-13 11:44am
View
TimeSpan doesn't do years!
Ian A Davidson
14-Mar-13 8:14am
View
http://msdn.microsoft.com/en-us/library/ms182137(v=vs.80).aspx
Ian A Davidson
14-Mar-13 8:12am
View
http://msdn.microsoft.com/en-us/library/ms182338(v=vs.80).aspx
Ian A Davidson
14-Mar-13 7:51am
View
Okay I see. I thought you meant doing the check for the negative number :)
Your class looks good. I like the constructor that takes parameters to be formatted.
Ian A Davidson
14-Mar-13 7:48am
View
What do you mean by "basic use of throw keyword"?
You throw an exception using it :) E.g. "throw new ArgumentException("paramName");"
Or you can rethrow an exception from a catch block simply by using "throw;" e.g. after doing something with or to the exception:
catch(Exception e)
{
/* Log error or add data to the exception, for example e.Data.Add("MyInfo", info); */
throw;
}
Ian A Davidson
14-Mar-13 7:39am
View
I think I'd go for this option myself - provided that the value being tested is indeed a parameter of the method in which the exception is being raised.
Ian A Davidson
14-Mar-13 7:37am
View
Could you clarify what you mean by implementing the required logic IN the exception?
Ian A Davidson
14-Mar-13 7:35am
View
Good answer. However, one point you might like to take note of yourself: Your Exception class should be serialisable - i.e. have the Serializable attribute and implement the constructor that takes SerializationInfo and StreamingContext parameters.
Also, you probalby ought to seal this class since it is highly unlikely you will derive from it (in fact it is good practice to seal every class you develop in the first instance, unless you are sure you are going to derive from it, or explicitly want to allow others to derive from it). I.e. declare it "[Serializable] public sealed class NegativeNumberException : Exception" ... and add the constructor "public NegativeNumberException(SerializationInfo info, StreamingContext context) : base(info, context) {} "
Ian A Davidson
14-Mar-13 7:26am
View
You should avoid catching "Exception", and catch derived exceptions that you know how to handle. Having said that, in a very few exceptional cases, I still do catch "Exception" - e.g. in order to simply log the error and then exit the application. However, this is often more down to laziness than anything else. It's not recommended.
Edit: You can catch "Exception" provided you are going to rethrow it. And I mean, rethrow, not create your own exception from it. I.e. "catch(Exception e) { /* Do something with e*/ throw; }
Ian A Davidson
14-Mar-13 7:22am
View
You shouldn't ever throw "Exception". Always use one of the standard derived classes or create your own exception class. This therefore is NOT an answer to the solution, even though it has been marked as one.
Ian A Davidson
12-Mar-13 10:44am
View
Not convinced. The code that T2A uses looks horrendous. Granted, it uses WideCharToMultiByte, but (in the code that comes with Visual Studio 9) it also uses _alloca to alocate the buffer. This function is deprecated because it's not secure. I guess MS might have updated their libraries from the one I'm looking at, but I doubt it somehow; I imagine they are more focused on .Net these days. If you read the information about this function there are all kinds of issues around exception handling.
Secondly, T2A calculates the buffer length required assuming that every 2-byte character in the unicode string will fit into 1 character of the multi-byte string (which seems to me to be a contradiction in terms in that case). If the string cannot be converted to fit in the buffer, the function simply fails. There is no check for overrun to reallocate the buffer accordingly. In fact there's even a comment MS's code to this effect!
I think you'd be better off writing the code that calls WideCharToMultiByte yourself in a loop to check that the converted string fits in the buffer allocated and loop to reallocate accordingly.
Regards,
Ian.
Ian A Davidson
12-Mar-13 7:57am
View
Ooops. Yes I see now that Progammady DID say it is in Unicode. I've edited my embarrasing comment! :) Another useful hint: if your code does or can use the STL, allocate the buffer using a vector of chars (std::vector<char>) then you can let it handle the memory management and don't need the delete. Then take the reference to the first char in the buffer as you would for a char array: "char* pArg = &buffer[0];" where buffer is the vector. Ian.
Ian A Davidson
12-Mar-13 7:49am
View
You do need to worry about *&. Because you're passing the pointer by reference, you can't just take the reference of a buffer. (E.g. "char buffer[50]; function(&buffer[0]);" will not compile!). Also, you forget that the operator to convert a CString, LPCTSTR, returns a const char*, so you'd have to cast away the const as well - only to be done when absolutely necessary and not a good idea when passing to an external function (and this is ignoring the fact that it's being compiled in unicode, where LPCTSTR is a const wchar_t*). So if you weren't compiling in unicode you COULD do something like, "CString myString("Hello World"); char* p = (char*)(LPCTSTR)myString; function(p);", but you wouldn't. I hope! Regards, Ian.
Ian A Davidson
12-Mar-13 7:43am
View
Best answer so far. Just take note, as Eugen has commented, that if you are compiling in Unicode to use a wide-character conversion copying function, e.g. "WideCharToMultiByte", instead of strcpy, and that in doing so you will not be able to rely on "GetLength" to give you the size required for your buffer. A good starting point in that case would be to assume that you need twice the length, allocate a buffer accordingly, and then check for ERROR_INSUFFICIENT_BUFFER, resize accordingly and loop until it is successfully copied.
Regards, Ian.
Ian A Davidson
11-Mar-13 10:47am
View
"Value" is a System.DateTime!
I expect you can indeed indeed use the operators, but in order to find out the number of Years difference, your options are a bit more limited because - unless you know something I don't - TimeSpan doesn't do years!
So you still either need to do what I suggested (add 18 years to the earlier DateTime) or use the more long-winded traditional route of comparing Year, then Month then Day, of the fashion:
int deltaY = y2 - y1;
if ((deltaY < 18) ||
((deltaY == 18) && ((m2 < m1) || ((m2 == m1) && (d2 < d1))))
{
// Warn
}
Regards,
Ian.
Ian A Davidson
11-Mar-13 9:56am
View
Glad to hear it. All the best,
Ian.
Ian A Davidson
11-Mar-13 7:01am
View
Subtract returns a TimeSpan which doesn't have a Years (or Year!) property, so I don't believe this will work, as I say in my solution below.
Ian A Davidson
11-Mar-13 6:24am
View
You're comparing number of Days. The question was asking to compare Years.
Ian A Davidson
11-Mar-13 5:44am
View
Would you like to give a meaningful code example of what you are trying to do? The example given appears to be illegal C++ and contains a set of randomly named variables that don't reference each other or anything else.
Regards,
Ian.
Ian A Davidson
3-Mar-13 21:13pm
View
No idea why this got "down-voted" - this is a much better answer than so-called "solution 1", which starts "No we can't answer that". (I really disagree with this "voting" system).
This book explains so much about using C# - what to do, what not to do - with reasoning behind it, explaining that actually you're writing code to compile a program into another language (Intermediate Language, or IL). It really is a MUST for any C# developer - doesn't matter if you are beginner or think you're an "expert".
If do you think it's too complicated for a beginner (which I don't think it is, given the fact I used it - and still do refer to it - and that, Muhamad, you said you have already studied C# for 6 months) then start with a "beginner's" book, such as those by Wrox Press (e.g. the VS 2010 one here http://www.amazon.co.uk/dp/0470502266/?tag=hydra0b-21&hvadid=9556677789&ref=asc_df_0470502266 - though not having read this one I can't recommend it personally). I don't think these will take you as far as the more in-depth books, such as the one I've already mentioned.
Reading is the answer, I'm afraid. Read, and practise writing various (useful) programs.
Regards,
Ian.
Ian A Davidson
2-Mar-13 12:58pm
View
When you have been a software engineer as long as I have, you know the purpose isn't to write "sexy" code.
The best code is code that is maintainable.
There are places where the ternary operator is useful, but it would normally be for the simplest of cases. (One example, off the top of my head, may be to decide what value to pass to a base class in the constructor of an object). In your case, go with if/else. You would have saved yourself a lot of time already, and you'll save yourself a lot of pain later. (E.g. what if later you find you need to do something else when m_iPage is/is not 7 - you'd probably have to change to if/else anyway. Or what if you decide you also need to do something when m_iPage is 6, etc).
Regards,
Ian.
Show More