NSPasteboard in Xamarin.Mac - c#

I am trying to get string from NSPasteboard in Xamarin.Mac, here is my code
NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;
string text = "";
Then what method of pasteboard do I use to store the string into text?

If you know you have string-based data on the pasteboard, you can use GetStringForType using the type of NSStringType.
Example:
string text = NSPasteboard.GeneralPasteboard.GetStringForType(NSPasteboard.NSStringType);
Returns a concatenation of the strings for the specified type from all the items in the receiver that contain the type.
Otherwise you can cycle through all the items on in the pasteboard using
PasteboardItems to return an array of NSPasteboardItem and review each item for the type that you are looking for.

Related

Using linq in .net core to parse a string and turn it into a list

I have a string that gets input like say: abcde.
I then want to take that string and do the following:
Make the string uppercase
Parse the string to get each character one by one
Add a prefix to that new string (prefix is H-I)
Return a list of every new string in order.
So using abcde the resulting list should be:
// new list items being returned
H-IA
H-IB
H-IC
H-ID
H-IE
I know there should be an easy way to do this with Linq but don't quite have it yet.
In a line
var list = str.ToUpper().ToArray().Select(c => "H-1" + c).ToList();

Parsing string for object property

How do I convert "Account" in to
<Account>
or "Object Name" into
<Object with matching name>
I'm attempting to parse a string and replace values in a string with object properties.
This is similar to what we do with form letters we send out, you know the Dear [Customer Name], thank you for purchasing [Some Item]. In this case the fields in the letter aren't setup by me and I need a means of converting what is in the text block into an object property.
Currently I'm using a code in the string {value=x; id=y ;property=z} and running that through a switch case to convert it into an object. Then replacing the {} with the object properties.
for example "some random string {value=1; id=1; property=Name} continued random string"
I parse the string to locate {value=, on a hit it runs through a switch case, where on case 1: return Account(ID = 1). The I grab Account.Name
Once I have that I put it in a text box so the user can validate that it is correct before generating a final document to be sent out.
Is there a way to have {Object.Property} in the string then use reflection to convert that string value to the object value?
Get property value from string using reflection
I used the above in other instances, but that requires that I have the object.
public static List<string> GetClassProperties(string className, IEnumerable<string> propertiesToExclude = null)
{
Type theType = Type.GetType(className);
if (theType == null)
{
return null;
}
}
This'll get you the Object type, you do need the full namespace for it to work, can't just be "Account" would need to be Project.Models.Account

Formatting a number string to add commas - c#

I know this question has been answered many times before however, I'm convinced the code I have is correct but isn't working correctly.
string total = ds.Tables[0].Rows[0][0].ToString();
string test = string.Format("{0:N}", total);
lbl_totalValue.Text = test;
This code isn't adding the commas into my value like it desire it to.
Can anyone see why?
When you put
string total = ds.Tables[0].Rows[0][0].ToString();
it means implicit G ("General") format string
string total = ds.Tables[0].Rows[0][0].ToString("G");
Do not format prematurely:
var total = ds.Tables[0].Rows[0][0]; // Value from table
string test = string.Format("{0:N}", total); // Format total with "N" format string
Your code is trying to format a string. If the DataTable contains a number you can pass the format specifier to ToString(), eg
var test=ds.Tables[0].Rows[0][0].ToString("N");
Or store the contents in a local variable and use String.Format :
var total = ds.Tables[0].Rows[0][0];
string test = string.Format("{0:N}", total);
If the datatable contains a string though, you'd have to parse it to a numeric type first
You have to use the string.Format with a number type, instead of string. In this case, the variable total is a string, it must be a number.
There are 8 overloads for the Strig.Format method. You are using this specific one: Format(String, Object) in which you pass a String value as argument of the second parameter. This is because you are using a string variable (total) to assign the value from the dataset in:
string total = ds.Tables[0].Rows[0][0].ToString();
Besides you are using .ToString() to retrieve it as a String.
If you are using SQL Server as data source to your ds dataset and you are certain about the SQL data type then you can assign that value directly to a variable with the corresponding C# type. To put it in a different way, SQL data types are mapped to C# data types.
If you are not sure about the C# data type of ds.Tables[0].Rows[0][0] then you could simply do the following:
Object total = ds.Tables[0].Rows[0][0];
string test = string.Format("{0:N}", total);
lbl_totalValue.Text = test;
And this way you literally use the Format(String, Object) overload of String.Format.

Attempting to get DASL property value from the Outlook table inbox

I'm attempting to read the DASL value PR_LONGTERM_ENTRYID_FROM_TABLE 0x66700102 mentioned in this thread -
get outlook mailitem for message taken from outlook table
The issue I'm having is with the following line in the code below from the full example below-
string ltEntryid = (string)nextRow["http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString()];
It throws an exception "Cannot convert type 'byte[]' to 'string'"
I might be going about this the wrong way so I'm looking for some advice. I can read all the other tables rows fine (Example- "EntryID(short term), MessageClass, Unread, SenderEmailType).
const string unReadfilter = "[UnRead] = true";
Outlook.Table table = folder.GetTable(unReadfilter, Outlook.OlTableContents.olUserItems);
// Remove the default column set.
table.Columns.RemoveAll();
// Add columns to the table
table.Columns.Add("Unread");
table.Columns.Add("EntryID");
table.Columns.Add("MessageClass");
table.Columns.Add("SenderEmailType");
table.Columns.Add("SenderEmailAddress");
// PR_LONGTERM_ENTRYID_FROM_TABLE
table.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString());
// sort table
table.Sort("Unread", true);
while (!table.EndOfTable)
{
Outlook.Row nextRow = table.GetNextRow();
bool unRead = (bool)nextRow["Unread"];
Debug.WriteLine(unRead);
string msgClass = (string)nextRow["MessageClass"];
Debug.WriteLine(msgClass);
string eId = (string)nextRow["EntryID"];
Debug.WriteLine(eId);
string sEaddr = (string)nextRow["SenderEmailAddress"];
Debug.WriteLine(sEaddr);
string sEtype = (string)nextRow["SenderEmailType"];
Debug.WriteLine(sEtype);
// PR_LONGTERM_ENTRYID_FROM_TABLE ***Exception with the following line***
string ltEntryid = (string)nextRow["http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString()];
Debug.WriteLine(ltEntryid);
if (msgClass.Equals("IPM.Note"))
{
//write to string list
dailyMiInboxList.Add(unRead.ToString());
dailyMiInboxList.Add(msgClass);
dailyMiInboxList.Add(eId);
dailyMiInboxList.Add(sEaddr);
dailyMiInboxList.Add(sEtype);
dailyMiInboxList.Add(sEaddr);
dailyMiInboxList.Add(ltEntryid);
}
}
PT_BINARY property is returned as an array of byte, but you are casting it to a string. If you want to convert it to a hex string, use MAPIFolder.PropertyAccessor.BinaryToString().
OK, I figured this out with Dmitry's help.
First when adding this dasl property to the table -
// PR_LONGTERM_ENTRYID_FROM_TABLE
table.Columns.Add("http://schemas.microsoft.com/mapi/proptag/0x66700102".ToString());
I should've not included the tostring so it should be written as follows-
table.Columns.Add(#"http://schemas.microsoft.com/mapi/proptag/0x66700102");
Next in the While loop, to convert the PT_BINARY property from an array of bytes use this to convert the row-
string PR_LONGTERM_ENTRYID_FROM_TABLE = "http://schemas.microsoft.com/mapi/proptag/0x66700102";
string ltEntryId = (string)nextRow.BinaryToString(PR_LONGTERM_ENTRYID_FROM_TABLE);
Debug.Print(ltEntryId);
This link was very helpful
In particular these comments-
• The value returned for a given column representing a binary (PT_BINARY) value depends on whether a built-in property name or a schema name was used to specify the Column. For explicit built-in property names (such as EntryID), the Row(Column) value is returned as a string. For property names referencing a namespace that represent a PT_BINARY property, the Row(Column) value is returned as a byte array. Use Row.BinaryToString to convert the byte array to a string.

Parsing a simple Json string

I have gotten a Json string to parse before that was an array of objects much longer than just a simple string, which makes me think that I'm doing something wrong with the formatting.
Here is word for word what our webservice is outputting as the json string:
{"news":"What is Legal/Awesome Dre"}
the first part is simply what I named the string in the application (news) and the second part is the string that will be changing as the song does which is why I would like to pull in a simple string of it.
When I run the app I'm getting a parse error at these lines:
Console.Out.Writeline (content);
news = JsonConvert.DeserializeObject(content);
The application output will show the Json string as it is on the website, but I get an error right after that's telling me Invalid Token: startPath... which last time meant that my Json string was formatted wrong for how I need to grab the data.
Anyone can help me with this?
(P.S. I am working in Xamarin Studio (mono for android) using C#, if that makes any difference)
The problem is that your serialized JSON object isn't a string, it's an object with the string value you want at the "news" property/key/name. This is a simple way to get the string:
dynamic jsonObj = JsonConvert.DeserializeObject(content);
string news = jsonObj.news;
Or you can use an anonymous type:
var jsonObj = JsonConvert.DeserializeAnonymousType(content, new { news = "" });
string news = jsonObj.news;
Or create a type with a string News property:
MyNewsType jsonObj = JsonConvert.DeserializeObject<MyNewsType>(content);
string news = jsonObj.News;
These all work in the following way:
var content = #"{""news"":""What is Legal/Awesome Dre""}";
// above code
Console.WriteLine(news); // prints "What is Legal/Awesome Dre"
Try to put square bracket in your JSON:
[{"news":"What is Legal/Awesome Dre"}]

Categories

Resources