Use a string as an argument in StringReader - wonderware

I'm using scripting with .NET in Wonderware Archestra IDE. This works fine:
dim SR as System.IO.StringReader;
SR = new System.IO.StringReader(OPCClient_L09.Valve.AliasDatabase);
But I need like this and it's not work:
dim SR as System.IO.StringReader;
dim Input as String;
Input = "OPCClient_L09.Valve.AliasDatabase";
SR = new System.IO.StringReader(Input);

You've declared a String type, which is the Wonderware String type. StringReader is expecting a System.String (i.e. the .NET type).
Change your String declaration to System.String:
dim SR as System.IO.StringReader;
dim Input as System.String;
Input = "OPCClient_L09.Valve.AliasDatabase";
SR = new System.IO.StringReader(Input);

Related

JSON serialization for option types

I'm running across an issue when I define a field of option type while serializing for JSON.
Before works (without option)
[<DataContract>]
type Article = {
[<field: DataMemberAttribute(Name="version") >]
version: string
}
After throws error (with option)
[<DataContract>]
type Article = {
[<field: DataMemberAttribute(Name="version") >]
version: string option
}
method threw exception:
System.Runtime.Serialization.SerializationException: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''.
Related Code
let response = request.GetResponse() :?> HttpWebResponse
use reader = new StreamReader(response.GetResponseStream())
use memoryStream = new MemoryStream(ASCIIEncoding.Default.GetBytes(reader.ReadToEnd()))
let result = (new DataContractJsonSerializer(typeof<Article>)).ReadObject(memoryStream) :?> Article
Here is a definition with a nullable int. String can already be null to which the zillions of Null reference exceptions attest...
open System
[<DataContract>]
type Person2 = {
[<DataMember(Name="Name") >]
entityName: Nullable<int>
[<DataMember(Name="Type") >]
entityType: String
}
However if you plan to fill this with a bunch of nulls maybe you should consider a class. This is really horrible but here's how it looks:
let p1 = { entityName = Nullable(10); entityType = "John"}
let p2 = { entityName = System.Nullable(); entityType = null}
val p1 : Person2 = {entityName = 10;
entityType = "John";}
val p2 : Person2 = {entityName = null;
entityType = null;}

What is the idiomatic way for an implementation to depend on a partial record type?

My intent is to define a module with functions which can operate on all records types which comply with certain assumptions about the keys.
To illustrate, let us have the following code:
> type DBRow = { id: string ; createdAt: System.DateTime } ;;
type DBRow =
{id: string;
createdAt: System.DateTime;}
> let logCreationInfo row = printf "Record %s created at %s " row.id (row.createdAt.ToString()) ;;
val logCreationInfo : row:DBRow -> unit
I would like to change the above logCreationInfo to be able to operate on all records which have id: string and createdAt: System.DateTime (and maybe other things).
Coming from typescript's structural typing, I'd have expected this to be trivial, but I am exploring the possibility that there is a more idiomatic way to handle this in F#.
I had attempted to handle this using interfaces, but even if that could work, since F# supports only explicit interfaces, this will not be suitable for types I don't define myself.
You could use statically resolved type constraints.
let inline logCreationInfo (x : ^t) =
printfn "Record %s created at %s"
(^t : (member id : string) (x))
((^t : (member createdAt : System.DateTime) (x)).ToString())
F# largely uses nominative typing - this is a natural choice in its runtime environment, as this is what Common Type System specification prescribes. Adherence to that set of rules allows F# code to near-seamlessly interoperate with other .NET languages.
It's worth noting that this follows the same reasoning as to why TypeScript uses structural typing. Since that language builds up on top of dynamically typed JavaScript, it's more natural to express object relationships in terms of their structure rather than nominal types - which are a foreign concept in JS.
F# does have a "backdoor" for structural typing through already mentioned SRTPs, but I would suggest using it very sparingly. SRTPs are resolved and the code using them is inlined by the compiler, making for longer compilation times and reduced interoperability with other languages and the .NET platform in general (simply put, you can't refer to that code from other languages or using reflection API, because it's "compiled away").
Usually there are other solutions available. Interfaces were already mentioned, though the example used was a bit contrived - this is simpler:
type IDBRow =
abstract Id: string
abstract CreatedAt: System.DateTime
type Person =
{
id: string
name: string
age: int
createdAt: System.DateTime
}
interface IDBRow with
member this.Id = this.id
member this.CreatedAt = this.createdAt
let logCreationInfo (row: #IDBRow) =
printf "Record %s created at %s" row.Id (string row.CreatedAt)
let x = { id = "1"; name = "Bob"; age = 32; createdAt = DateTime.Now }
logCreationInfo x
Or using composition and a generic type to capture the generic part of what it means to be a DBRow:
type DBRow<'data> =
{
id: string
data: 'data
createdAt: System.DateTime
}
type Person =
{
name: string
age: int
}
let logCreationInfo (row: DBRow<_>) =
printf "Record %s created at %s" row.id (string row.createdAt)
let x = { id = "1"; data = { name = "Bob"; age = 32 }; createdAt = DateTime.Now }
logCreationInfo x
Here's a version with interfaces:
open System
type DBRow1 = {
id: string
createdAt: DateTime
}
type DBRow2 = {
id: string
createdAt: DateTime
address: string
}
/// The types are defined above without an interface
let row1 = {id = "Row1"; createdAt = DateTime.Now}
let row2 = {id = "Row2"; createdAt = DateTime.Now; address = "NYC"}
type IDBRow<'A> =
abstract member Data:(string * DateTime)
// Object expression implements the interface
let Data1 (x:DBRow1) = {
new IDBRow<_> with
member __.Data = (x.id, x.createdAt)
}
let Data2 (x: DBRow2) = {
new IDBRow<_> with
member __.Data = (x.id, x.createdAt)
}
//pass in both the object expression and the record
let getData (ifun: 'a -> IDBRow<'b>) xrec =
(ifun xrec).Data
// You could partially apply the functions: `getData1 = getData Data1`
getData Data1 row1 //("Row1", 2018/02/05 9:24:17)
getData Data2 row2 //("Row2", 2018/02/05 9:24:17)
You can certainly use an interface (an object expression in this case) to tack on another member, .Data, even if you don'T have access to the original type. You would still need to put together one object expression for each type though, so SRTP might be a more "elegant" solution.

Linq query not working on XML string

i have a web service which returns xml data as string. i am trying to apply linq on xml string which is retunred by the service.
Lets say the xml stirng is some thing like.
string str = "<root xmlns=\"http://tempuri.org/Count.xsd\"> <child> <subchild1>1</subchild1><subchild2>1</subchild2><subchild3>1</subchild3></child></root>";
Below is the c# code i am using.
XDocument xdoc = XDocument.Parse(str);
var item = xdoc.Element("root").Element("child").Element("subchild1");
but the above query is always returning null.
can any one correct me what is wrong in above peace of code.
This solved my problem.
XNamespace xnp = xdoc.Root.GetDefaultNamespace();
var item = xdoc.Element(xnp + "root").Element(xnp + "child").Element(xnp + "subchild1").Value;

Using NoRM to access MongoDB from F#

Testing out NoRM https://github.com/atheken/NoRM from F# and trying to find a nice way to use it. Here is the basic C#:
class products
{
public ObjectId _id { get; set; }
public string name { get; set; }
}
using (var c = Mongo.Create("mongodb://127.0.0.1:27017/test"))
{
var col = c.GetCollection<products>();
var res = col.Find();
Console.WriteLine(res.Count().ToString());
}
This works OK but here is how I access it from F#:
type products() =
inherit System.Object()
let mutable id = new ObjectId()
let mutable _name = ""
member x._id with get() = id and set(v) = id <- v
member x.name with get() = _name and set(v) = _name <- v
Is there an easier way to create a class or type to pass to a generic method?
Here is how it is called:
use db = Mongo.Create("mongodb://127.0.0.1:27017/test")
let col = db.GetCollection<products>()
let count = col.Find() |> Seq.length
printfn "%d" count
Have you tried a record type?
type products = {
mutable _id : ObjectId
mutable name : string
}
I don't know if it works, but records are often good when you just need a class that is basically 'a set of fields'.
Just out of curiosity, you can try adding a parameter-less constructor to a record. This is definitely a hack - in fact, it is using a bug in the F# compiler - but it may work:
type Products =
{ mutable _id : ObjectId
mutable name : string }
// Horrible hack: Add member that looks like constructor
member x.``.ctor``() = ()
The member declaration adds a member with a special .NET name that is used for constructors, so .NET thinks it is a constructor. I'd be very careful about using this, but it may work in your scenario, because the member appears as a constructor via Reflection.
If this is the only way to get succinct type declaration that works with libraries like MongoDB, then it will hopefuly motivate the F# team to solve the problem in the future version of the language (e.g. I could easily imagine some special attribute that would force F# compiler to add parameterless constructor).
Here is a pretty light way to define a class close to your C# definition: it has a default constructor but uses public fields instead of getters and setters which might be a problem (I don't know).
type products =
val mutable _id: ObjectId
val mutable name: string
new() = {_id = ObjectId() ; name = ""}
or, if you can use default values for your fields (in this case, all null):
type products() =
[<DefaultValue>] val mutable _id: ObjectId
[<DefaultValue>] val mutable name: string

String Split()-method

Why am I getting this error?
public class ParameterParser
{
public List<string> ParseParameter(string queryString)
{
queryString = queryString.Replace(" ", "");
string[] strs = queryString.Split(#"(", #"=", #",", #"<>",
StringSplitOptions.None);
List<string> parameters = new List<string>();
foreach (string ss in strs)
{
string s = ss.Trim(')');
if (s.StartsWith("#") && !s.Equals("") && s!=null)
{
parameters.Add(s.Replace(" ", ""));
}
}
return parameters;
}
}
Error 3 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments F:...\ParameterParser.cs
If you want to pass an array of strings, you have to do that explicitly:
string[] strs = queryString.Split(new string[] {"(", "=", ",", "<>"},
StringSplitOptions.None);
I suspect you were modelling your code on something like this:
string[] strs = queryString.Split('(', '=', ',');
This is using a parameter array (the params modifier in C#). Parameter arrays are only applicable for the final parameter, and no overload of String.Split takes a params string[]. That's why it wasn't working for you.
Note that I've changed the strings into simple string literals - I would recommend only using verbatim string literals when you actually need to.
#"(", #"=", #",", #"<>" is not a single string or a char[] array
Try this:
string[] strs = queryString.Split(new string[] {#"(", #"=",#",",#"<>" },StringSplitOptions.None);

Resources