Copy object using Clipboard and paste it as object or text - c#

I want to copy object ( List ) to clipboard and use it in my application and outside of it.
In my application, I want to be able to "paste" data without any additional operation. Outside I need CSV data.
I tried to achieve it by using DataObject. I can convert this list to formatted string - so i think it's easy to get CSV. I stored random string in DataObject, (just for test). The problem is storing List object in Clipboard. DataObject contains specific type ( checked using DataObject.GetFormats() ), but when i try to get this object from clipboard, I get null.
I've found this, while it solves my problem, I need to serialize/deserialize.
Is there any way to store and retrieve object the normal way ?

Clipboard.SetDataObject(list);
var deslist = Clipboard.GetDataObject().GetData(list.GetType());

Related

Encog C#, VersatileMLDataSet from CSV, how to get original data?

I want to use CSV reader from Encog library, like this:
var format = new CSVFormat('.', ' ');
IVersatileDataSource source = new CSVDataSource(filename, false, format);
var data = new VersatileMLDataSet(source);
Is it possible to get original data from variable data? I have to show records from CSV to user in dataGridView, before I use it for neural network. I want to be able to modify original data as well. According documentation there is property Data, but it doesnt work for me. If I try something like:
data.Data[1][1]
I get null pointer exception. There is another problem with using data before normalization. I want to get count of records by:
data.GetRecordCount()
But I get error You must normalize the dataset before using it. So even if I have not used data yet I have to normalize it? If this is true, then is probably better to use my own CSV reader and then load it into encog from memory, right?
So I just looked at the Encog source code on GitHub. Thankfully your question is well defined and narrow in scope, so I can provide an answer. Unfortunately, you probably won't like it.
Basically, when you pass in your IVersatileDataSource into the constructor for VersatileMLDataSet, it gets placed into a private readonly field called _source. There is no abstraction around _source, so you cannot access it from outside of VersatileMLDataSet.
The Data property indeed will only be populated during the normalization process. There also doesn't appear to be any fields within CSVDataSource that are public of any value to you (again, all private).
If you just wanted to look at a single column of data, you could stay within Encog and look at Encog.Util.NetworkUtil.QuickCSVUtils. There are methods within this class that will help you pickup a file and get a single column of data out quickly.
If you wanted to get the full CSV data out of a file within Encog, you could use the Encog.Util.CSV.ReadCSV class to get the data. This is the underlying implementation anyways utilized by your code when you instantiate a QuickCSVUtils. You will have to provide some wrapper logic around ReadCSV, similar to QuickCSVUtils. If you go this route, I'd recommend peeking in that class to see see how its using ReadCSV. Essentially ReadCSV reads a single line at time.
But if you really need to read the RAW csv data from within the VersatileMLDataSet class, your best bet would be to provide your own implementation inside a custom class derived from VersatileMLDataSet.
There is a couple of steps you need to do after reading in your file:
You have to define your column types
Analyze data
Map output
Set up normalization strategy
Get your data count
Optionally clone the data.Data to keep originals
The code is below with appropriate comments.
var filename = #"iris.data.csv";
var format = new CSVFormat('.', ',');
IVersatileDataSource source = new CSVDataSource(filename, false, CSVFormat.DecimalPoint);
var data = new VersatileMLDataSet(source);
// Define columns to read data in.
data.DefineSourceColumn("Col1", 0, ColumnType.Continuous);
data.DefineSourceColumn("Col2", 1, ColumnType.Continuous);
data.DefineSourceColumn("Col3", 2, ColumnType.Continuous);
data.DefineSourceColumn("Col4", 3, ColumnType.Continuous);
ColumnDefinition outputColumn = data.DefineSourceColumn("Col5", 4, ColumnType.Nominal);
// Analyze data
data.Analyze();
// Output mapping
data.DefineSingleOutputOthersInput(outputColumn);
// Set normalization strategy
data.NormHelper.NormStrategy = new BasicNormalizationStrategy(-1, 1, -1, 1);
data.Normalize();
// Get count
var count = data.GetRecordCount();
// Clone to get original data
var oiginalData = data.Data.Clone();
For more details check the quickstart paper.
Sample data I'm using comes from here.

How to set drop down list control in Word document using c#?

I am having issues with setting a drop down list control in ms word document using Novacode-Docx. e.g. Gender from drop down list.
I have got some text replacement but that's easy. I have done that. Just the drop down list I am struggling with.
If there is any solution in c# other than using Novacode-Docx I am happy to implement that.
I haven't included any code for the reason that I don't know how to set or get the drop down list values from Ms word document using c#. I have tried google but didn't get any thing related to drop down list.
Here is a sample DropDown.
I have used Drop down list from Developer option and used the properties to add items to it.
Here is the actual List
To access the list of a content control dropdown use the Add method. I have no idea whether the library you're using provides access to that. The following code sample is standard C#:
object oLstEntryValue = "1";
object oLstEntryIndex = 1;
ContentControl.DropdownListEntries.Add("entry caption", ref oLstEntryValue, ref oLstEntryIndex);
The last two parameters are optional, so you can also pass Type.Missing if you don't want/need to specify them.

Microsoft.Office.Interop.Word with C# - Identify Existing Table of Contents Object

I'm working off a template document that has an existing Table of Contents object. I am having trouble identifying it and assigning it to a TableOfContents object programatically for further manipulation/updating.
I have examined the documentation for the TablesOfContents interface, and the only way I can see to return an object of the proper type is through the TablesOfContents.Add() function. I don't want to add a new TOC though. I just want to identify the one that already exists.
This seems to be explained in the documentation:
"The Document.TablesOfContents Property returns a TablesOfContents collection that represents the tables of contents in the specified document."
Once you have that, "Use TablesOfContents(index), where index is the index number, to return a single TableOfContents object. The index number represents the position of the table of contents in the document."

How best to pass Values back from UI to Custom SSIS Transform

I'm in the process of creating a custom SSIS Transform. Something along the lines of the Derived column Transform that comes with SSIS. I have a list of the Input columns in a DataGridView:
The user will be able to supply a value to replace in the even that the value of the item is null.
In the past I have used CustomPropertyCollection to save single values to the ComponentMetadata. My question is. In this case where I will be storing a value for each of the input columns how should I go about that?
I have considered creating a xml string where I store the Inputcolumn id and the value I want to used to replace it with. But this seams like over kill to me. Does anyone know of a better way of going about this?
I have created, Dataflow tasks, and connection managers before. This is my first data transformation task.

How to return a custom object or double[,] to a cell in Excel from ExcelDna?

Ideally my UDF would return some double results, either in a form of double[,], double[] or as a custom object. I would like it to be all stored in a single cell in Excel and then use another UDF to extract them. It is similar like cache the result of a calculation and display it later on demand.
Is it possible?
One approach is to have an internal 'object store' in your Excel-DNA add-in, and then have a function that returns some kind of 'handle' to Excel, together with accessor functions that take the 'handle' and access the object as required. Cleaning up the objects you can create is a possible issue, but can be dealt with using some of the RTD features.
This discussion on the Excel-DNA group has an example of implementing this idea in F# - https://groups.google.com/group/exceldna/browse_frm/thread/138bc83923701e6d.
It certainly is possible, but it is difficult to give you a detailed answer without knowing how you are using ExcelDNA. What I mea by that is whether you are wrapping your C# methods in vba code, or are getting a handle on the Excel application in C# and writing to the workbook directly from there.
In both cases you can do what you want, but the way to do it would be slightly different. The wrapper method would also be slightly less flexible, since you have to first pass out your values to the vba UDF and then write them to the cell from there, so you will be restricted in the data type you can return (as far as I know anyway).
The way to do this would be to write the results to a specific cell, maybe on a hidden sheet, so it can't be tampered with, and then retrieve those using another UDF. You would have to hardcode the cell address, or maybe parse the sheet to find the values you want.
In case of a method returning a double[,], you would probably have to write the values to two different cells, or in one cell as text with a separator and then convert from text to double when you retrieve the cell value, something like Double.Parse(cell.value.ToString().Split(',')[0]) to get the first values (assuming that you are storing the values as a comma-separated string) or similar code in vba if you use a pure vba UDF to get the values...
If you want to do this, I think you should definitely use a hidden sheet with a well-defined structure to store your values. If you only need to store the values for the duration of the session, then I think you should store them in global variables in a vba module.
UPDATE
Since you are just writing functions, I don't think you will be able to pass out a custom object (unless you implement your own converter, convert it to text and then read it back in that way).
You pass back the double or double[,] to a variable in your UDF and write it to a cell from there. Then read it back again from that cell with any other UDF.
The rest is the same as I wrote above. If you store two values in the same cell, you will have to do that as text, so you will have to split and parse the values first in your UDF before passing them to your C# method (or you can do the parsing in the method).
In practice there should be no problem at all with what you are trying to do.

Categories

Resources