F# System.TypeInitializationException... why? - user-interface

I'm attempting to make a gui application in F# that uses a dialog box to open a file
however whenever I try to use it, the program crashes with that exception
here's my code:
let openAndDrawChart e =
let dlg = new OpenFileDialog(Filter="CSV Files|*.csv")
if dlg.ShowDialog() = DialogResult.OK then // code crashes here
let pieChart = drawChart dlg.FileName
boxChart.Image <- pieChart
btnSave.Enabled <- true
If I try to put a try with block around it, the program just crashes after the "with" keyword
Also, if I replace the code following then with a () then the program still crashes, the code never gets past the if ... then statement
So does anyone here know what I'm doing wrong, and how I can fix it?
I just want to point out that I'm more accustomed to making gui applications in C#, so trying to do it in F# is new to me
Here's the exception's exact words:

OK so, this is a little bit embarrassing but
apparently the program doesn't play well with an entry point...
At the bottom of my code I had this:
[<EntryPoint>]
let main args =
0
Which I just removed...
I didn't think it mattered much since the program executed the code above the entry point anyway
But apparently it was was enough to mess up the whole thing!
Sorry for the inconvenience, though it might be a good thing to leave this here for future reference

Related

Can a IDE find logic errors?

Can a good IDE (e.g. Visual Studio) find basic logic errors?
or is there no such thing as a "basic" logic error and all of these errors are undetectable by the IDE?
Yes, some IDEs (like Visual Studio) have continous syntax checks, that can find some logical errors. However, a logical error can only be spotted if there is something odd in the code, there is no AI trying to figure out what the code is actually intended to do.
If you for example write this in a C# method in Visual Studio:
int a = 1;
int b = 2;
Console.WriteLine(a + a);
then the IDE will notice that you never used the variable b, and put a warning in the form of a squiggly line under the variable. Pointing on it will reveal the message The variable 'b' is assigned, but its value is never used.
The IDE can't know if you intended to output a + b rather than a + a, and simply the use of a + a is not odd enough to render a warning, but it can see that you created a variable b and that you probably intended to use that for something.
Not really.
Sometimes it can pick up that a code path may never execute I think.
int x = 9;
if (x != 9)
{
foo();
}
and it maybe able to tell you that you've declared something without using it. It's stuff you can catch yourself. However, the real power is in the debugger where you can use "watch" or locals/autos and follow the code with step-in/out/over at any scope, see when they change, and change the values yourself to see what needs to happen. It's a good way to test logic. In assembly, you can move your code back a few lines and repeat it... it's not guaranteed to work, but you can override anything.
Edit: see https://en.wikipedia.org/wiki/Halting_problem

Can someone show a known good working function in Xojo 2014?

So.. I just downloaded Xojo 2014 for OS X, and up to this point have found it a pretty simple and effective development environment.
However, I've been trying to make a function or sub routine for 45 minutes. Every time I try following tutorials or the Xojo documentation I get the following error:
I've followed (even though I could be missing something) the directions here: http://docs.xojo.com/index.php/Function
Even though there is no full example in the documentation (bad development environment).
Also, in the screen shot is showing a sample function I copied and pasted off Xojo forums and is supposed to work. I'm not a programming newb per say, but more an Xojo newb. I've also had experiences with silly bugs in RealStudio in the past.
Can someone maybe point out what I could be missing?
You cannot use the Function, Sub, End Function, or End Sub lines in the method editor. Doing so will cause a syntax error because method declarations are automatically added by the IDE based on the values you enter into the method editor's name, parameters, and return type fields.
e.g.
as said,
Function, Sub, End Function, or End Sub is done for you,
you can do 'exit sub' if nothing need to be returned, else just return a proper value
like some events need a true or false, so if you need the exit the function, just return false
You cannot add inline functions within your Xojo code. You add methods to your project items these ways:
Insert->Method
"+" button on editor toolbar, then Method
Insert button on main toolbar, then Method
Option-Command-M (on OS X)

Syntax error when using Me in VB

Here's an interesting thing, I have used Visual Studio 2010 previously to make basic VB programs, and never had an issue with anything. Now I am just perplexed as to why this is happening.
I haven't even gotten very far into the coding phase of the first form, and already I am getting yelled at for a syntax error when I use "Me", whether it is Me.Close(), or whatever. Instead of posting the code as text here, it would be best to just show the syntax highlighting.
http://kelina-enterprises.com/img/vb-syntax-error-stackoverflow.png
::EDIT:: This image was taken down, it is not a real error.
Here's the funny thing, when I test the program, it runs just fine if I run the last successful build, which executes those "errored" lines.
Is this something I should even concern myself with, or should I just ignore these apparently false syntax errors?
Do keep in mind that if this is something simple that I have just neglected to take care of for some reason (nothing comes to mind), it has been about 3 years since I last used VB, and not even for a year at that.
You cannot write VB statements inside a Class, such as, Me.Close() or MessageBox.Show("??"). For example,
Class Test
MessageBox.Show("test")
End Class
is obviously syntax error
Inside a Class, you can write only declarations or methods.
In your case, you can place the codes in the Load event handler like this ...
Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Fullscreen, over everything, maximized
Me.Width = My.Computer.Screen.WorkingArea.Width
Me.Height = Screen.PrimaryScreen.Bounds.Height
Me.CenterToScreen()
Me.TopMost = True
Me.WindowState = FormWindowState.Maximized
End Sub
You don't need to type-in the Private Sub ... End Sub. Go to form design, double click the form and the Load event should be appeared.
It looks like you mean to put that code within the load event of the form, you can't have code that's not inside some sort of routine.
If you double-click on the form in design mode, the load event should be automatically created for you and you can then move the code into it.
There are also other events of the form that you may want to use, e.g. Shown event, these can be created by clicking on the appropriate selection in the drop-down box next to the one that has LoginForm selected.

sigabrt on filteredArrayUsingPredicate

I'm a noob and trying to convert an example from a book into an app I can use.
The sample app is a modified version of the contacts application and it works.
I've done some further modification, and the search no longer works. It sigabrts on the following line
self.filteredAnswercards = [flattenedArray
filteredArrayUsingPredicate:predicate];
I'm stumped.
my head is bloody from beating it against my keyboard.
ANY help is massively appreciated.
Thanks.
My suggestion was to wrap the line that crashes inside a #try/#catch block and, inside the catch, log the exception and the result of the exception's callStackSymbols method.
For the record, part of the problem with the 4.x versions of Xcode is that they are much worse than 3.x versions at telling you where an exception is coming from. For this reason, getting familiar with tricks that make a program or the debugger tell you what you need to know is very important.
I would guess that predicate is nil. Where did you assign it? Or did you never assign it?
Very hard to say without seeing more code. Sigabort generally means an exception has been thrown. You can put a break point in objc_exception_throwto get a back trace which should help highlight the cause.
If you get NO new info with the above, others have said that a full reboot of the computer can help... but I have not had a situation where I could verify this.
---- edit based on comments -----
It sounds like filteredAnswercards is nil, that will definitely cause a Sigabort. Allocate that array properly and you should be good to go.

Should we create objects if we need them only once in our code?

This is a coding style questions:-
Here is the case
Dim obj1 as new ClassA
' Some lines of code which does not uses obj1
Something.Pass(obj1) ' Only line we are using obj1
Or should we directly initiaize the object when passing it as an argument?
Something.new(new ClassA())
If you're only using the object in that method call, it's probably better to just pass in "new ClassA()" directly into the call. This way, you won't have an extra variable lying around, that someone might mistakenly try to use in the future.
However, for readability, and debugging it's often useful to create the temporary object and pass it in. This way, you can inspect the variable in the debugger before it gets passed into the method.
Your question asks "should we create objects"; both your examples create an object.
There is not logically any difference at all between the two examples. Giving a name to an object allows it to be referred to in more than one place. If you aren't doing that, it's much clearer to not give it a name, so someone maintaining the code can instantly see that the object is only passed to one other method.
Generally speaking, I would say no, there's nothing wrong with what you're doing, but it does sound like there may be some blurring of responsibilities between your calling function, the called function, and the temporary object. Perhaps a bit of refactoring is in order.
I personally prefer things to be consistent, and to make my life easier (what I consider making my life easier may not be what you consider making your life easier... so do with this advice what you will).
If you have something like this:
o = new Foo();
i = 7
bar(o, i, new Car());
then you have an inconsistency (two parameters are variables, the other is created on the fly). To be consistent you would either:
always pass things as variables
always pass things created on the fly
only one of those will work (the first one!).
There are also practical aspects to it as well: making a variable makes debugging easier.
Here are some examples:
while(there are still lines in the file)
{
foo(nextLine());
}
If you want to display the next line for debugging you now need to change it to:
while(there are still lines in the file)
{
line = nextLine();
display(line);
foo(line);
}
It would be easier (and safer) to have made the variable up front. It is safer because you are less likely to accidentally call nextLine() twice (by forgetting to take it out of the foo call).
You can also view the value of "line" in a debugger without having to go into the "foo" method.
Another one that can happen is this:
foo(b.c.d()); // in Java you get a NullPointerException on this line...
was "b" or "c" the thing that was null? No idea.
Bar b;
Car c;
int d;
b = ...;
c = b.c; // NullPointException here - you know b was null
d = c.d(); // NullPointException here - you know c was null
foo(d); // can view d in the debugger without having to go into foo.
Some debuggers will let you highlight "d()" and see what it outputs, but that is dangerous if "d()" has side effects as the debugger will wind up calling "d()" each time you get the value via the debugger).
The way I code for this does make it more verbose (like this answer :-) but it also makes my life easier if things are not working as expected - I spend far less time wondering what went wrong and I am also able to fix bugs much faster than before I adopted this way of doing things.
To me the most important thing when programming is to be consistent. If you are consistent then the code is much easier to get through because you are not constantly having to figure out what is going on, and your eyes get drawn to any "oddities" in the code.

Resources