I am trying to show a message box from PowerShell with yes and no buttons.
I can display a message box with an OK button:
[system.windows.forms.messagebox]::show("Hello, world!")
And I can create a variable $buttons with the buttons I want:
$buttons=[system.windows.forms.messageboxbuttons].yesno
And I can see that the Show() static method is overloaded and that one of the options is to give three parameters:
Show(String, String, MessageBoxButtons) Displays a message box with specified text, caption, and buttons.
So naturally(?) I decided to call this:
[system.windows.forms.messagebox]::show("Are you sure?","",$buttons)
And this results in an error:
Cannot find an overload for "Show" and the argument count: "3".
But there IS an overload for "Show" that accepts three arguments!
What am I doing wrong?
(And can someone tell me why calling a method in PowerShell is usually done by using the dot syntax: object.method() but requires "::" for the MessageBox class? It's confusing.)
Correct way of doing this can be
$buttons=[system.windows.forms.messageboxbuttons]::yesno;
[system.windows.forms.messagebox]::Show("Are you sure?","",$buttons);
Notice "::" instead of "." in the first line. YesNo value is defined staticly on System.Windows.Forms.Messageboxbuttons, so you must use "::" (static call) instead of "."
Note that "[system.windows.forms.messageboxbuttons].yesno" is an attempt to call a "YesNo" property on an instance of System.Type, which does not exist and therefore result in a $null
Hope it helps !
Cédric
Edit ---
Keith solution using an implicit cast made by powershell for the enum is more elegant.
It just does not work on PS V2 CTP 3 which I still use but work fine on RTM version.
The complete explication was worth giving, though...
Try this way:
[windows.forms.messagebox]::show('body','title','YesNo')
And the distinction between using :: and . is static method vs instance method. Notice above that we didn't create a MessageBox object. We are just using a static method on MessageBox with the :: syntax.
Related
I am automating a page using selenium with java and trying to use a case insensitive xpath with the help of translate function as follows.
driver.findElement(By.xpath("//a[contains(translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'tools')]")).click();
'tools' text exists on the page as 'Tools'. [T as caps]
Now my question is,
What does '.,' means in the above code?
Using 'tools' in place of '.,' gives all the //a links. Reason?
Whenever I use 'Tools' instead of 'tools' in the above code, it does not work.
Someone help me here.
image 1
image 2
Now my question is,
What does '.,' means in the above code?
Using 'tools' in place of '.,' gives all the //a links. Reason?
Whenever I use 'Tools' instead of 'tools' in the above code, it does not work.
The dot step is an abbreviated syntax, from the specs:
. selects the context node
Because it's used as parameter for a function that expects a string, will be casted by the means of string() function.
The string 'tools' always contains the string 'tools', thus you are not filtering any selected a element when you used instead of .
In the other hand, any lowercase string will never contain the string 'Tools', so you won't be able to select anything.
So I am doing a simple personal project in winforms with F#. My code used to work, but now throws this exception for seemingly no reason.
An unhandled exception of type 'System.InvalidOperationException' occurred in FSharp.Core.dll
Additional information: The initialization of an object or value resulted in an object or value being accessed recursively before it was fully initialized.
The code is a member method that is being invoked from the constructor of the form itself
do
//lots of other constructor code before this point
// render the form
form.ResumeLayout(false)
form.PerformLayout()
form.ReloadGoals
//several other members before here
member form.ReloadGoals =
let x = 10 //crashes on this line
The website where I grabbed the template for the project I am using is this one.
Unfortunately I have made some substantial additions to this.
I would be glad to post more code, but I need to know what code would be relevant exactly, as I am not exactly sure and don't want to bog down the post in extraneous code.
Also I can't really find a lot of documentation on System.InvalidOperationException.
Every time I find it, it is being used as an example of an exception you can throw on your own, not what causes it.
See The F# 3.0 Language Specification (final version, PDF), §8.6.1 Primary Constructors in Classes:
During construction, no member on the type may be called before the last value or function definition in the type
has completed; such a call results in an InvalidOperationException.
Almost certainly, your code in the question doesn't tell the full story. If you hit the above
mentioned restriction, then there's somewhere an attempt to access a field or member not fully initialized.
Some example:
type X() as this =
let x = this.X
member __.X = 42
X()
One workaround might be to encapsulate the offending code in a member of its own and call that in the constructor instead. Another would be the wrapping in a function definition.
This will be an incomplete answer, since I cannot reproduce the problem (using F# interactive, the given example, the ReloadGoals modification, and Form.Show, the code runs fine). However, there are strange things happening:
Taken from the template, there should be a handler method for the Form.Load event, which fires when the type is fully constructed. Why is additional loading code in the constructor instead of this event handler? Load exists precisely to counter this kind of problem with unorderly initialization.
The template you are using isn't exactly sane F#. For example, initControls is a value of type unit that is evaluated where it is defined; its binding to a name is absolutely useless and should be replaced with a simple do. Writing initControls in the do block later has no effect at all. form.ResumeLayout(false); form.PerformLayout() should be equivalent to form.ResumeLayout(true), but I don't understand what these are doing in the constructor in the first place. The event handlers have two possibly unnecessary indirections: one to a delegate constructor, another to a method that has no real reason to exist -- the handlers should be lambdas or simple, private functions. Why are they public members?!
The error appearing in the question is probably caused by the usage of form in its own constructor. Move your new usage to the Load event handler, and it should work.
Personally, I would go further and ditch implementation inheritance by instantiating a plain Form and subscribing to its events. For example, in FSI, something similar to the template could be done like this:
open System.Drawing
open System.Windows.Forms
let form = new Form()
form.ClientSize <- new Size(600, 600)
form.Text <- "F# Form"
let formLabel = new Label()
formLabel.Text <- "Doubleclick test!"
formLabel.DoubleClick.Add <| fun _ -> form.Close()
form.Controls.Add(formLabel)
form.Show()
which uses no inheritance at all. (In an application, you'd use Application.Run etc instead of form.Show().) This does not run into initialization problems as easily and, additionally, is very useful if you want to encapsulate the form inside a simpler type or even just a function.
Following #DimitreNovatchev's article Programming in XPath 3.0, and using BaseX GUI as the test environment, I tried some of the examples that define functions that accept functions as parameters. E.g. with
let $compose :=
function($f as function(), $g as function())
(The rest of the code isn't relevant to this error, but you can see it as the third example under Function Composition.)
I get this error from BaseX:
Error:
Stopped at 43-compose.xpath, 2/39:
[XPST0003] Expecting 'as', found ','.
The point where the error was detected was on the second line, just before the comma. Apparently, the processor expects the $f parameter declaration to say not just that $f should be a function, but also the return value of the function.
I don't know whether it's right for BaseX to expect that or not. Presumably, Dimitre's examples tested successfully before he made that presentation at Balisage. Maybe something changed in the XPath 3.0 spec between that article and when BaseX was released?
OK, found the answer. I got an evaluation key for Saxon EE, so I was able to try another processor. For future reference, this was the command line:
C:\Program Files\Saxon>java -cp saxon9ee.jar net.sf.saxon.Query -s:"input.xml" -
q:"ex5.xpath" -qversion:3.0
Note that -qversion:3.0 is currently required in order to get any 3.0 functionality.
Saxon throws an error at the same point, but gives a helpful suggestion on how to fix it:
Error on line 2 column 39 of ex5.xpath:
XPST0003 XQuery syntax error near #... function($f as function(), $#:
function() is no longer allowed for a general function type: must be function(*)
I changed function() to function(*) wherever a general function type was wanted, and the errors went away, both in BaseX and in Saxon.
So apparently BaseX was correct (but Saxon's error message was more helpful, as is often the case!). Sounds like something did change in the spec recently. I haven't been able to figure out what the relevant change was, from the change log. But regardless of what changed, the spec currently says that a FunctionTest must have either a * within the parentheses, or an as after them. (This applies to declarations of parameters that are functions, but does not apply to inline functions themselves.)
I am constructing a diagram editor in WPF on Windows 7.
Notwithstanding that I am on the verge of learning significant design techniques (TDD, Prism, MVVM, dependency injection), though I understand a handful of established design patterns, here is my question:
As a whole, the commands will have different numbers and type combination of parameters.
(To be clear, each command has a fixed set of parameters)
For example, the following, all of which can be performed with the mouse :
Command Create New Node : parameter = location for new node (Point)
Command Move Node to new position : parameters= Node (graphNode), new location (Point)
Command make an edge connecting two nodes: parameters= From Node(graphNode), To Node(graphNode), Type of Edge (GraphEdgeType)
How ought I apply either the factory or abstract factory pattern to best encapsulate such commands?
What is the preferred way for the client to pass these parameters to the Command executive?
(I have hunted here and elsewhere but not found questions or answers so explicitly framed, and ready to be redirected to something I couldn’t find:-)
[EDIT] I have not been explicit enough:
IF I make a CommandFactory to return Commands, should it be passed commandType (an Enum, say) and a parameter set object ... or should it only be passed the commandType, so that the client subsequently primes the command with parameters?
I suspect that this https://cuttingedge.it/blogs/steven/pivot/entry.php?id=91 (which I found at Command Pattern : How to pass parameters to a command?) is what I am looking for even though I do not yet understand it - it likely transcends any of the melange of techniques I was envisaging but could not express.
How ought I apply either the factory or abstract factory pattern to best encapsulate such commands?
What are you talking about? Just pass in a class with properties for all the "parameters" as parameter.
(If anything this sounds like you need a state machine.)
so I am trying to assign a number to a variable that is dynically generated from a binded array...when i try and assign it and trace it out nothing happens, which means I am obviously doing something wrong but I am not sure? just for fun i decided to bind the data to a label like so...
<s:Label text="{this.dd.selectedViews.length}"/>
and that work and updated properly, but when running in debug mode i got this warning...
warning: unable to bind to property 'length' on class 'Array' (class is not an IEventDispatcher)
so what would be the best method of assigning the array to a variable to use throughout my application
thanks in advance for any help
I'm not really sure what you are asking here, but maybe this will help you out. As your error message says, the Array class is not an IEventDispatcher. What that means is that if you try to use a plain old Array as the source of a data-binding expression, it generally is not going to work.
If you need to bind to an array, you can try using a different class such as ArrayCollection, which supports data binding.