quick question for a java noob
I'm just trying to write some functions to query a SQL Server db, so I looked up some stuff and I'm trying to use the JTDS driver. Downloaded it and dropped the .jar file in C:\Program Files\Java\jre1.6.0_07\lib is that correct?
Because I get a ClassNotFoundException when I do this Class.forName("net.sourceforge.jtds.jdbc.Driver");
I should probably go grab Head First Java or something, but I'm not doing anything too complicated.
got past hurdle one
had to install to the ext folder in libs
C:\Program Files\Java\jre1.6.0_07\lib\ext
on my way to being pro
Starfuck on
jackfaces
"If you're going to play tiddly winks, play it with man hole covers."
- John McCallum
How does one keep a WebBrowser control in C# from navigating when a user clicks? I want to be able to force it to open links in a new browser window. I tried setting .Url to the URL I wanted it to stay on in the _Navigating event handler, but that didn't work.
The way I'm trying to do it is causing a weird StackOverflowException in winforms that can't be caught, so I don't think I'm doing it right. It felt hackish anyway, so I wasn't expecting it to work.
How does one keep a WebBrowser control in C# from navigating when a user clicks? I want to be able to force it to open links in a new browser window. I tried setting .Url to the URL I wanted it to stay on in the _Navigating event handler, but that didn't work.
The way I'm trying to do it is causing a weird StackOverflowException in winforms that can't be caught, so I don't think I'm doing it right. It felt hackish anyway, so I wasn't expecting it to work.
In your Navigating handler, have you tried setting e.Cancel = true? That should cause the control to stop the event (stop Navigating), and then allow you to pop open the e.Url property in a new window.
How does one keep a WebBrowser control in C# from navigating when a user clicks? I want to be able to force it to open links in a new browser window. I tried setting .Url to the URL I wanted it to stay on in the _Navigating event handler, but that didn't work.
The way I'm trying to do it is causing a weird StackOverflowException in winforms that can't be caught, so I don't think I'm doing it right. It felt hackish anyway, so I wasn't expecting it to work.
In your Navigating handler, have you tried setting e.Cancel = true? That should cause the control to stop the event (stop Navigating), and then allow you to pop open the e.Url property in a new window.
... should.
I didn't even think about e. I'll try that after I make these ramen noodles.
edit: Success!
private void txtFeedContent_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if(e.Url != new Uri(currenturl))
{
e.Cancel = true;
}
//Code to open link in default browser goes here
}
.NET installation problems are the bane of my existence.
It does keep log files, which was the only way I found out there was a permission problem on a directory it was trying to install to (what) the last time I had issues.
End on
I wish that someway, somehow, that I could save every one of us
Yeah he's also a giant dumbfuck too; Microsoft Office's giant COM library being partially written in .NET being the biggest reason. I'm not too worried because, really, the only thing that's used on the system is my application anyways.
I could just change it to call interop services on shell32.dll as well, though, or whatever.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
I'm trying to handle a click on a link in the WebBrowser:
string htmlfile = @"<html><head><title>";
htmlfile = htmlfile + title + @"</title></head><body>";
htmlfile = htmlfile + @"Open in browser: <a href='" + url + @"'" + @" onclick='window.external.URLClicked('" + url.Replace("\"", @"'") + @"')'>" + title + @"</a>" + body + @"</body>";
(the code will look less ugly later)
A sample result, which looks fine:
Open in browser: <a href='http://xkcd.com/744/' onclick='window.external.URLClicked('http://xkcd.com/744/')'>Walkthrough</a>
My URLClicked method, inside the form with the control:
private void URLClicked(string url)
{
MessageBox.Show("Clicked a link");
}
I get a syntax error at line 2, character 1. Even though there's only one line. I'm doing it exactly as several examples have done it as far as I can tell.
Is there any actual performance advantage to using #define for constants as opposed to just doing int whatever = 0
No. But you can cause all sorts of interesting bugs due to scoping and macro expansion if you use #DEFINE. Remember that the preprocessor macro expansion can and will replace anything that matches the define in all files that include the header you declared the #DEFINE in.
Ugh. I spent 2 days one time trying to track down a weird bug which turned out to be dueling #DEFINE TRUE statements in header files from two different groups that were getting included in the final build. And in that case, last one in wins.
I really don't miss C/C++ all that much.
Kakodaimonos on
0
Options
GnomeTankWhat the what?Portland, OregonRegistered Userregular
Is there any actual performance advantage to using #define for constants as opposed to just doing int whatever = 0
If you don't tack a 'const' on to that, then yes, it handles them differently. It can't optimize the value as a constant (as it could with a #define, because it is a constant then) unless you mark it const. Some compilers still will (I believe Visual C++ and gcc will still do some static analysis and figure out it's actually constant), but it's still good practice to mark it const.
If you do that, mark the int flag const, then no, there is no difference, and in fact, you shouldn't use #define. Use enums or constant ints.
What I am doing is tracking a set of integer values such as X/Y coordinates that don't change they represents positions of UI elements during pre-defined states, and there's a lot of them. Like... i have 15 right now and I'm probably only halfway done.
They get called at least a few times in a typical application usage. They are very specifically named, so there is no possibility for conflicts...
I usually use #defines out of habit, but I don't exactly write a lot of C code.
There might be some scoping concerns with consts? ...but that'd probably be easily avoided by just marking them static (particularly if it's in a header).
End on
I wish that someway, somehow, that I could save every one of us
Anyone here familiar with the Google maps API? I've got an idea for a project to automate some boring stuff I have to do every now and then and was wondering if it's possible to feed lists of partial addresses (usually missing city and zip code) and retrieving the full address instead of having to do it one by one by copy and paste.
What I am doing is tracking a set of integer values such as X/Y coordinates that don't change they represents positions of UI elements during pre-defined states, and there's a lot of them. Like... i have 15 right now and I'm probably only halfway done.
They get called at least a few times in a typical application usage. They are very specifically named, so there is no possibility for conflicts...
but I should use constants anyway because: ??
consts are technically safer, I guess. I dunno, I program C for a living and I pretty much always use defines for stuff like this.
Maybe consts are a little more efficient because the compiler will only allocate storage for it once? Then again, I would imagine consts wouldn't be stored in the static section anyway if we're talking about integers and they resolve to ASM immediates.
What I am doing is tracking a set of integer values such as X/Y coordinates that don't change they represents positions of UI elements during pre-defined states, and there's a lot of them. Like... i have 15 right now and I'm probably only halfway done.
They get called at least a few times in a typical application usage. They are very specifically named, so there is no possibility for conflicts...
but I should use constants anyway because: ??
consts are technically safer, I guess. I dunno, I program C for a living and I pretty much always use defines for stuff like this.
Maybe consts are a little more efficient because the compiler will only allocate storage for it once? Then again, I would imagine consts wouldn't be stored in the static section anyway if we're talking about integers and they resolve to ASM immediates.
We need a real C guru for this one. eecc?
Haha, I'm flattered, but I'm not a guru.
Besides, I'm going to end up repeating what you guys have said.
I believe that once you turn on optimisation, it won't make a difference.
But then, as is always the case with programming, there are a whole bunch of caveats depending on what the const variable is, and how it's used.
e.g. provided you never try to take the address of the variable.
void putPoint( const int *pOptionalLocation );
#define DEFAULT_LOCATION 6
putPoint( & DEFAULT_LOCATION ); // Good luck with this.
vs
const int nDefaultLocation = 6;
putPoint( & nDefaultLocation ); // Works as expected
or the classic:
const char *pData = "Blah blah blah"; // Will practically always be allocated
Which is really a specific case of the more general guideline that arrays of const variables will almost always be allocated*:
It's decided that with only 5 elements, it's just cheaper to copy them into the stack via mov instructions!! Hahahaha!
On the other hand, making the array static const int allocates it in static memory, which saves the time needed for initialisation at the expense of constant memory usage.
Personally, unless a variable needs the specific advantages offered by a #define, I'd use a const because they turn into the same binaries most of the time. Also, #defines have a few disadvantages that can be hard to track down via compiler messages:
Actually, that error isn't so bad, it's in C++ where you can have something like
// Oh noes! Has global scope!
#define MAX_VALUE 8
class tExample
{
const int MAX_VALUE = 2;
};
On the other hand, #defines do offer some advantages - but they are quite specific, and you'd sort of know if you want to use them. I have seen it used in quite a few places (mostly pure C libraries), e.g.
Say you're providing a library, and you'd like to have defaults that can be optionally user initialised:
user_config.h : Custom user config file with stuff like:
// Do this if you want to change the default number of foobars, otherwise leave undefined
#define DEFAULT_FOOBARS 10
// Do this if you want to change the default number of doohickeys, otherwise leave undefined
// #define DEFAULT_DOOHICKEYS UNUSED
Which in the long run is a bit harder to maintain.
e.g. add a new attribute - default enable state. Using #defines, you'd have to go:
#define UI_OKBUTTONEN 1
#define UI_CANCELBUTTONEN 0
// ... carry on for each ui element
using a const array:
const int g_nDefaultEnable[ uie_EnumSize ] =
{
1,
0
}
You do have to make sure that the elements in the array are filled in properly and match their proper enum, though!
That is a bug I've been bitten by before.
Again, trade-offs:
In embedded systems, you might find you have way more code space than stack space, so you use the #defines and the switch() because that's code space and doesn't take up the very limited memory you have!
This seems a little far fetched, but is it possible in C#/.NET to store a list of method names in an App.Config key (comma-separated), then have the program inspect the Config file, and run the specified methods?
Building a web test suite, so would be nice to have this flexibility instead of recompiling the main class every time I want to make a new test sequence. I could use a unit test framework, but I'd like to explore this avenue first. Would Reflection be a good place to start?? Alternatively is there another way to achieve this 'read method names from file, run methods as listed' programatically?
Airan on
0
Options
KakodaimonosCode fondlerHelping the 1% get richerRegistered Userregular
edited May 2010
Reflection is probably the better approach. That's how NUnit works. Reflection + custom attributes that are used to tag the test cases and test setups.
You should take a look at NUnit and NUnitASP. It may do what you need:
#define kSearchBarPortraitXCoordinate 0
#define kSearchBarPortraitYCoordinate 544
#define kSearchBarLandscapeXCoordinate 0
#define kSearchBarLandscapeYCoordinate 478
#define kSearchBarResponderPortraitYCoordinate 659 // Y coordinate for when keyboard is displaying in portrait mode
#define kSearchBarResponderLandscapeYCoordinate 316 // Y coordinate for when keyboard is displaying in landscape mode
#define kSearchBarPortraitWidth 768
#define kSearchBarPortraitHeight 38
#define kSearchBarLandscapeWidth 1024
#define kSearchBarLandscapeHeight 38
So there's x/y/height/width for the elements for both device orientations, and there's a couple of extra values for application state, where, since only one value is different, you only see that one (in this case Y). There is no "ResponderWidth" since becoming a responder doesn't change the width of the control.
Another way of doing this, and perhaps solidifying an answer is, instead of just storing my integer values and then creating the struct to which they fit as needed, I could just pre-define my array of structs and use the enum/array approach that eecc has provided, which seems pretty cool.
That way my values get stored to satisfaction, and I am not creating a struct every time the user flips their device over. Not that I really need that performance. I was just curious. Regardless, the struct/enum/array approach seems easier to maintain from a code perspective.
I'm trying to handle a click on a link in the WebBrowser:
string htmlfile = @"<html><head><title>";
htmlfile = htmlfile + title + @"</title></head><body>";
htmlfile = htmlfile + @"Open in browser: <a href='" + url + @"'" + @" onclick='window.external.URLClicked('" + url.Replace("\"", @"'") + @"')'>" + title + @"</a>" + body + @"</body>";
(the code will look less ugly later)
A sample result, which looks fine:
Open in browser: <a href='http://xkcd.com/744/' onclick='window.external.URLClicked('http://xkcd.com/744/')'>Walkthrough</a>
My URLClicked method, inside the form with the control:
private void URLClicked(string url)
{
MessageBox.Show("Clicked a link");
}
I get a syntax error at line 2, character 1. Even though there's only one line. I'm doing it exactly as several examples have done it as far as I can tell.
Anyone? And some kind of reference for Winforms would be awesome. I would get the O'Reily book to go with the C# 3.0 book that has been immensely helpful, but they don't seem to have one for WinForms.
So reading the H/A thread about the software job and the division problem makes me want to say, "What would I do if division/modulus was broken on my platform/library? I'd find a different job that doesn't require me to work with broken components or libraries or doesn't attempt to fix them before trying to integrate a half-assed-bug-filled implementation of bit shifting and division."
One of my responses to the trivia answer in the past with a "towers of hanoi" was "I'd defer that question to the programming department that specializes in non-business oriented algorithms, then I'd continue with the project or task at hand that is probably more important. But, if you want to see my code from my C++ Class, I'll be happy to give it to you." I don't think he liked my response, but then again, when he offered me $9 an hour before that, he deserves whatever he gets.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
0
Options
syndalisGetting ClassyOn the WallRegistered User, Loves Apple Productsregular
edited May 2010
More Objective C fun here.
Right now, I am generating three NSMutableArrays, all of which (by themselves) can drive a tableview. What I WANT to do is put these three arrays into a single NSMutabaleArray, and have that drive a sectioned tableView.
And this is crashing out on me, giving the error: -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6faad80
So building the parent array is really my first major hurdle. I know the search predicates are right, I know that the individual mutable arrays are good on their own; they pop into my plumbing perfectly if I simply rename their output to entityArrayCharacter and comment out thew rest of this code.
Once I have the array, I THINK this is right below, but some insight would be awesome here as well...
Also in my .M:
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [entityArrayCharacter count];
// NSLog(@"%i",[entityArrayCharacter count]);
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [entityArrayCharacter objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"Name"];
return [array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return @"Classes";
else if(section == 1)
return @"Races";
else {
return @"Powers";
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
//First get the dictionary object
NSDictionary *dictionary = [self.entityArrayCharacter objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Name"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.text = cellValue;
return cell;
}
Of course, this is all assumptions, as I haven't gotten my array to the point where this code is executing. I am sure I am missing something all kinds of simple here... would love some help though. My app is making HUGE strides, and little things like this keep me from trying to hit my deadline.
syndalis on
SW-4158-3990-6116
Let's play Mario Kart or something...
0
Options
GnomeTankWhat the what?Portland, OregonRegistered Userregular
edited May 2010
Seriously, don't use #define. Just use constant int's, where you can actually control the scoping (by putting them in a namespace or a class/struct). You also don't run the risk of incredibly hard to track down compiler problems caused by same-name #defines and other constructs.
You are putting NSMutableArrays into your parent array, but when you retrieve them in your table methods you are trying to assign them to NSDictionary ... why?
Jasconius on
0
Options
GnomeTankWhat the what?Portland, OregonRegistered Userregular
I'm trying to handle a click on a link in the WebBrowser:
string htmlfile = @"<html><head><title>";
htmlfile = htmlfile + title + @"</title></head><body>";
htmlfile = htmlfile + @"Open in browser: <a href='" + url + @"'" + @" onclick='window.external.URLClicked('" + url.Replace("\"", @"'") + @"')'>" + title + @"</a>" + body + @"</body>";
(the code will look less ugly later)
A sample result, which looks fine:
Open in browser: <a href='http://xkcd.com/744/' onclick='window.external.URLClicked('http://xkcd.com/744/')'>Walkthrough</a>
My URLClicked method, inside the form with the control:
private void URLClicked(string url)
{
MessageBox.Show("Clicked a link");
}
I get a syntax error at line 2, character 1. Even though there's only one line. I'm doing it exactly as several examples have done it as far as I can tell.
Anyone? And some kind of reference for Winforms would be awesome. I would get the O'Reily book to go with the C# 3.0 book that has been immensely helpful, but they don't seem to have one for WinForms.
WinForms is incredibly well documented on MSDN, and a lot of other places. It's probably one of the most well documented and sampled toolkits in history (seriously).
The syntax error may not be in that line of code, we would need to see the whole class (preferably spoilered so it isn't huge) to get an idea of where the syntax error is.
Seriously, don't use #define. Just use constant int's, where you can actually control the scoping (by putting them in a namespace or a class/struct). You also don't run the risk of incredibly hard to track down compiler problems caused by same-name #defines and other constructs.
That is why you use #undef at the end of the file. #define is perfectly acceptable as long as you treat it with the proper amount of respect.
Ethea on
0
Options
GnomeTankWhat the what?Portland, OregonRegistered Userregular
Seriously, don't use #define. Just use constant int's, where you can actually control the scoping (by putting them in a namespace or a class/struct). You also don't run the risk of incredibly hard to track down compiler problems caused by same-name #defines and other constructs.
That is why you use #undef at the end of the file. #define is perfectly acceptable as long as you treat it with the proper amount of respect.
And why do any of that when you can just use equally efficient constant integers that follow correct syntax and scoping rules?
There is no point, at all, except wanting to be a crusty old codger who still thinks the pre-processor is a great idea.
Seriously, don't use #define. Just use constant int's, where you can actually control the scoping (by putting them in a namespace or a class/struct). You also don't run the risk of incredibly hard to track down compiler problems caused by same-name #defines and other constructs.
That is why you use #undef at the end of the file. #define is perfectly acceptable as long as you treat it with the proper amount of respect.
And why do any of that when you can just use equally efficient constant integers that follow correct syntax and scoping rules?
There is no point, at all, except wanting to be a crusty old codger who still thinks the pre-processor is a great idea.
I had a problem when you mentioned a problem that is caused by bad programming, not a bad language feature. As far as defines go, I personally like them as they people to write macros. And we all know macros are black magic.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Showbox
{
class HTML
{
//There's probably a better way to do this.
private string basepath = Application.StartupPath.Replace(@"\", @"/") + "/";
private string filename = "HTML/index.html";
public string GenerateHTML(string title, string csspath, string body, string url)
{
//Generates an HTML document and returns it
string generatedpath = @"file:///" + basepath + filename;
//csspath will hold the location of the css used to generate pages
//temporary indicates whether or not the file is temporary. May end up not
//using it.
//Add a check to create the file if it doesn't exist
TextWriter w = new StreamWriter(Application.StartupPath + @"\HTML\index.html");
//Build a simple HTML file
string htmlfile = @"<html><head><title>";
htmlfile = htmlfile + title + @"</title></head><body>";
htmlfile = htmlfile + @"URL: " + url + "</body>";
//write to file
w.WriteLine(htmlfile);
//close file
w.Close();
return generatedpath;
}
public void DeleteGeneratedHTML()
{
//Stub for a method that deletes a generated HTML file.
//Used when the program starts and exits
}
}
}
The relevant methods:
private void URLClicked(string url)
{
MessageBox.Show("Clicked a link");
}
private void tsReloadFeeds_Click(object sender, EventArgs e)
{
currenturl = Application.StartupPath.Replace(@"\", @"/");
//Refresh all feeds
//run a check to make sure something is selected
lstFeeds.Items.Clear();
RSS rss = new RSS();
List<string> RSSList = rss.LoadRSS(Application.StartupPath.Replace(@"\", @"/") + "/Feeds.xml", 0);
TitleList = rss.GetTitleList();
URLList = rss.GetURLList();
DescriptionList = rss.GetDescriptionList();
List<int> count = new List<int>();
count = rss.GetCount();
int i = 0;
while (i != URLList.Count)
{
ListViewItem item = new ListViewItem(TitleList[i]);
item.SubItems.Add(URLList[i]);
item.SubItems.Add(CountList[i].ToString());
lstFeeds.Items.AddRange(new ListViewItem[] { item });
i++;
}
//test
HTML generator = new HTML();
//MessageBox.Show(generator.GenerateHTML("test", "buh", true));
string thefile = generator.GenerateHTML(TitleList[i - 1], "no css", DescriptionList[i - 1], URLList[i - 1]);
txtFeedContent.Navigate(thefile.Replace(@" ", @"%20"));
//MessageBox.Show(thefile.Replace(@" ", @"%20"));
currenturl = thefile;
}
private void txtFeedContent_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//txtFeedContent.Url = URLList;
//setting to the previous url to keep it from navigating
//MessageBox.Show(e.Url.ToString());
if(e.Url != new Uri(currenturl))
{
e.Cancel = true;
}
//open link in default browser
}
The generated HTML and generated URL look correct when I have them show in a message box. There's no error when I click the link, only when I click the button that calls tsReloadFeeds_Click.
edit: I forget that I took the onClick out of the anchor, but that's in the other post.
Posts
Pfft.
Assembly is too easy to read. Obfuscated C at least tries.
But really:
MUMPS is practically obfuscation by design.
I think someone here works with it?
Tofu wrote: Here be Littleboots, destroyer of threads and master of drunkposting.
Oh.
I misremembered.
He's working with a descendent of MUMPS.
I am so sorry templewulf.
If only so it can be posted with this:
got past hurdle one
had to install to the ext folder in libs
C:\Program Files\Java\jre1.6.0_07\lib\ext
on my way to being pro
"If you're going to play tiddly winks, play it with man hole covers."
- John McCallum
The way I'm trying to do it is causing a weird StackOverflowException in winforms that can't be caught, so I don't think I'm doing it right. It felt hackish anyway, so I wasn't expecting it to work.
... should.
I didn't even think about e. I'll try that after I make these ramen noodles.
edit: Success!
edit: Screenshot for the curious: http://i46.tinypic.com/2mnkxaw.png
My thumbnail generator that ties into the Windows Shell doesn't work on computer with bogus installs. Now I gotta troubleshot this beast.
It does keep log files, which was the only way I found out there was a permission problem on a directory it was trying to install to (what) the last time I had issues.
CLR program manager Jesse Kaplan thinks that's a bad idea.
Yeah he's also a giant dumbfuck too; Microsoft Office's giant COM library being partially written in .NET being the biggest reason. I'm not too worried because, really, the only thing that's used on the system is my application anyways.
I could just change it to call interop services on shell32.dll as well, though, or whatever.
I'm trying to handle a click on a link in the WebBrowser:
(the code will look less ugly later)
A sample result, which looks fine:
My URLClicked method, inside the form with the control:
I get a syntax error at line 2, character 1. Even though there's only one line. I'm doing it exactly as several examples have done it as far as I can tell.
I doubt it matters much with modern compilers, but I doubt ancient ones handled both equally well.
No. But you can cause all sorts of interesting bugs due to scoping and macro expansion if you use #DEFINE. Remember that the preprocessor macro expansion can and will replace anything that matches the define in all files that include the header you declared the #DEFINE in.
Ugh. I spent 2 days one time trying to track down a weird bug which turned out to be dueling #DEFINE TRUE statements in header files from two different groups that were getting included in the final build. And in that case, last one in wins.
I really don't miss C/C++ all that much.
If you don't tack a 'const' on to that, then yes, it handles them differently. It can't optimize the value as a constant (as it could with a #define, because it is a constant then) unless you mark it const. Some compilers still will (I believe Visual C++ and gcc will still do some static analysis and figure out it's actually constant), but it's still good practice to mark it const.
If you do that, mark the int flag const, then no, there is no difference, and in fact, you shouldn't use #define. Use enums or constant ints.
What I am doing is tracking a set of integer values such as X/Y coordinates that don't change they represents positions of UI elements during pre-defined states, and there's a lot of them. Like... i have 15 right now and I'm probably only halfway done.
They get called at least a few times in a typical application usage. They are very specifically named, so there is no possibility for conflicts...
but I should use constants anyway because: ??
There might be some scoping concerns with consts? ...but that'd probably be easily avoided by just marking them static (particularly if it's in a header).
Steam Profile
3DS: 3454-0268-5595 Battle.net: SteelAngel#1772
consts are technically safer, I guess. I dunno, I program C for a living and I pretty much always use defines for stuff like this.
Maybe consts are a little more efficient because the compiler will only allocate storage for it once? Then again, I would imagine consts wouldn't be stored in the static section anyway if we're talking about integers and they resolve to ASM immediates.
We need a real C guru for this one. eecc?
Haha, I'm flattered, but I'm not a guru.
Besides, I'm going to end up repeating what you guys have said.
I believe that once you turn on optimisation, it won't make a difference.
But then, as is always the case with programming, there are a whole bunch of caveats depending on what the const variable is, and how it's used.
e.g. provided you never try to take the address of the variable.
void putPoint( const int *pOptionalLocation );
vs
or the classic:
Which is really a specific case of the more general guideline that arrays of const variables will almost always be allocated*:
e.g.
* Again, always caveats:
Will not allocate the array, but will become the equivalent of (with optimisations):
Whereas:
would allocate the memory on the stack (check out the assembly produced by gcc! It's fascinating!)
It's decided that with only 5 elements, it's just cheaper to copy them into the stack via mov instructions!! Hahahaha!
On the other hand, making the array static const int allocates it in static memory, which saves the time needed for initialisation at the expense of constant memory usage.
Personally, unless a variable needs the specific advantages offered by a #define, I'd use a const because they turn into the same binaries most of the time. Also, #defines have a few disadvantages that can be hard to track down via compiler messages:
Actually, that error isn't so bad, it's in C++ where you can have something like
On the other hand, #defines do offer some advantages - but they are quite specific, and you'd sort of know if you want to use them. I have seen it used in quite a few places (mostly pure C libraries), e.g.
user_config.h : Custom user config file with stuff like:
// Do this if you want to change the default number of foobars, otherwise leave undefined
#define DEFAULT_FOOBARS 10
// Do this if you want to change the default number of doohickeys, otherwise leave undefined
// #define DEFAULT_DOOHICKEYS UNUSED
And then a:
default_config.h :
#ifndef DEFAULT_FOOBARS
#define DEFAULT_FOOBARS 60
#endif
#ifndef DEFAULT_DOOHICKEYS
#define DEFAULT_DOOHICKEYS 21
#endif
etc
library.c :
#include "user_config.h"
#include "default_config.h"
// No matter what, DEFAULT_DOOHICKEYS and DEFAULT_FOOBARS are now defined to *something*
int getDefaultFooBars(void)
{
return DEFAULT_FOOBARS;
}
Heck, you could try to error check to provide guarantees at compile time using this:
#if DEFAULT_FOOBARS < 20
// Report compile time error, e.g.
#error "DEFAULT_FOOBARS must be greater than or equal to 20"
#endif
Again, these are some very specific cases where #defines can be more useful than consts, but you'd know if you want to use them.
Now, having said all that, I'd be tempted to use a combination of enums and const int arrays for what I *think* Jasconius is describing:
e.g.
just because if you use #defines, you might end up with something like:
Which in the long run is a bit harder to maintain.
e.g. add a new attribute - default enable state. Using #defines, you'd have to go:
using a const array:
You do have to make sure that the elements in the array are filled in properly and match their proper enum, though!
That is a bug I've been bitten by before.
Again, trade-offs:
In embedded systems, you might find you have way more code space than stack space, so you use the #defines and the switch() because that's code space and doesn't take up the very limited memory you have!
Building a web test suite, so would be nice to have this flexibility instead of recompiling the main class every time I want to make a new test sequence. I could use a unit test framework, but I'd like to explore this avenue first. Would Reflection be a good place to start?? Alternatively is there another way to achieve this 'read method names from file, run methods as listed' programatically?
You should take a look at NUnit and NUnitASP. It may do what you need:
http://www.nunit.org/
http://nunitasp.sourceforge.net/download.html
what I am describing but I think it's approaching that. These are the constants I have defined right now.
#define kAutoCompletePopoverPortraitXCoordinate 380
#define kAutoCompletePopoverPortraitYCoordinate 216
#define kAutoCompletePopoverLandscapeXCoordinate 525
#define kAutoCompletePopoverLandscapeYCoordinate 0
#define kAutoCompletePopoverWidth 342
#define kAutoCompletePopoverHeight 328
#define kSearchBarPortraitXCoordinate 0
#define kSearchBarPortraitYCoordinate 544
#define kSearchBarLandscapeXCoordinate 0
#define kSearchBarLandscapeYCoordinate 478
#define kSearchBarResponderPortraitYCoordinate 659 // Y coordinate for when keyboard is displaying in portrait mode
#define kSearchBarResponderLandscapeYCoordinate 316 // Y coordinate for when keyboard is displaying in landscape mode
#define kSearchBarPortraitWidth 768
#define kSearchBarPortraitHeight 38
#define kSearchBarLandscapeWidth 1024
#define kSearchBarLandscapeHeight 38
So there's x/y/height/width for the elements for both device orientations, and there's a couple of extra values for application state, where, since only one value is different, you only see that one (in this case Y). There is no "ResponderWidth" since becoming a responder doesn't change the width of the control.
Another way of doing this, and perhaps solidifying an answer is, instead of just storing my integer values and then creating the struct to which they fit as needed, I could just pre-define my array of structs and use the enum/array approach that eecc has provided, which seems pretty cool.
That way my values get stored to satisfaction, and I am not creating a struct every time the user flips their device over. Not that I really need that performance. I was just curious. Regardless, the struct/enum/array approach seems easier to maintain from a code perspective.
Anyone? And some kind of reference for Winforms would be awesome. I would get the O'Reily book to go with the C# 3.0 book that has been immensely helpful, but they don't seem to have one for WinForms.
One of my responses to the trivia answer in the past with a "towers of hanoi" was "I'd defer that question to the programming department that specializes in non-business oriented algorithms, then I'd continue with the project or task at hand that is probably more important. But, if you want to see my code from my C++ Class, I'll be happy to give it to you." I don't think he liked my response, but then again, when he offered me $9 an hour before that, he deserves whatever he gets.
Right now, I am generating three NSMutableArrays, all of which (by themselves) can drive a tableview. What I WANT to do is put these three arrays into a single NSMutabaleArray, and have that drive a sectioned tableView.
My code in question is as follows:
In my .M: And this is crashing out on me, giving the error:
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6faad80
So building the parent array is really my first major hurdle. I know the search predicates are right, I know that the individual mutable arrays are good on their own; they pop into my plumbing perfectly if I simply rename their output to entityArrayCharacter and comment out thew rest of this code.
Once I have the array, I THINK this is right below, but some insight would be awesome here as well...
Also in my .M: Of course, this is all assumptions, as I haven't gotten my array to the point where this code is executing. I am sure I am missing something all kinds of simple here... would love some help though. My app is making HUGE strides, and little things like this keep me from trying to hit my deadline.
Let's play Mario Kart or something...
WinForms is incredibly well documented on MSDN, and a lot of other places. It's probably one of the most well documented and sampled toolkits in history (seriously).
The syntax error may not be in that line of code, we would need to see the whole class (preferably spoilered so it isn't huge) to get an idea of where the syntax error is.
Getting this error:
Parse error: syntax error, unexpected '"' in /user/student/mfarrell/public_html/cs4250/adhoc.php on line 6
Line 6 I'm using str_replace.
Heres an example of str_replace from the PHP Manual
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
Edit: Nevermind. Escape character. Blargh.
See how many books I've read so far in 2010
That is why you use #undef at the end of the file. #define is perfectly acceptable as long as you treat it with the proper amount of respect.
And why do any of that when you can just use equally efficient constant integers that follow correct syntax and scoping rules?
There is no point, at all, except wanting to be a crusty old codger who still thinks the pre-processor is a great idea.
I had a problem when you mentioned a problem that is caused by bad programming, not a bad language feature. As far as defines go, I personally like them as they people to write macros. And we all know macros are black magic.
The relevant methods:
The generated HTML and generated URL look correct when I have them show in a message box. There's no error when I click the link, only when I click the button that calls tsReloadFeeds_Click.
edit: I forget that I took the onClick out of the anchor, but that's in the other post.