attempting to connect to R from C# - c#

I am trying to connect to R using C#. I installed R.Net and referenced it my project. This is my first attempt at C#. Any ideas what I am doing wrong?
This is the sample C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace RNet_Calculator
{
public partial class Form1 : Form
{
// set up basics and create RDotNet instance
// if anticipated install of R is not found, ask the user to find it.
public Form1()
{
InitializeComponent();
string dlldir = #"C:\Users\R\R-2.15.2\bin\x64";
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(dlldir);
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch
{
MessageBox.Show(#"Unable to find R installation's \bin\i386 folder.
Press OK to attempt to locate it.");
}
}
}
}
}
this is the Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Form1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

This is actually nothing to do with R. You have probably overwritten the namespace in one place and not in another. You have the code
namespace RNet_Calculator
in your form code. If you open Form1.designer.cs you will probably see
namespace Form1
Just change the namespace from Form1 to RNet_Calculator and your errors should disappear.
EDIT
In response to your edit, you should either change the single RNET_Calculator namespace back to Form1 or you should (but don't have to) change the Form1 namespace in your Program.cs file (and any other files in your project) as well. Doing this means you also should change the namespace in your project properties. Right-click your project, select Properties, and in the Application tab (should be the first one to open), change the "Default namespace" textbox to RNET_Calculator.

Related

Can't Complete Code over File error how do i fix

I can't seem to figure out what is going on here.
I have posted below my code and a photo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
ChromeOptions options = new ChromeOptions();
options.AddExtensions(new File("pathtoextentions.crx"));
IWebDriver driver = new ChromeDriver(options);
//Test Opening Website
driver.Navigate().GoToUrl("https://google.com");
}
}
}
I have also uploaded a photo here where it show my error is on new File: https://ibb.co/Lk72dsD
Error CS0712 Cannot create an instance of the static class 'File'
What am I doing wrong?
File is a static class, you can't create instances of it like new File().
Looking at ChormeOptions.AddExtensions, it accepts an array or an IEnumerable of string.
So, we could use an array in your case. To do so, replace this:
options.AddExtensions(new File("pathtoextentions.crx"));
with this:
options.AddExtensions(new[] { "pathtoextentions.crx" });

Program opens cmd then closes

Title says it all, it opens a command prompt then closes in 1/2 a second. i dont know why and i havent found anything on this. i just made a new c# app. Might have something to do with Main as it has no reference.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace dumb_thing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void Main(string[] args)
{
}
private void Animatetext_Click(object sender, EventArgs e)
{
Animatetext.Text = "";
Thread.Sleep(700);
Animatetext.Text = "P";
Thread.Sleep(350);
Animatetext.Text = "Pa";
Thread.Sleep(350);
Animatetext.Text = "Par";
Thread.Sleep(350);
Animatetext.Text = "Para";
Thread.Sleep(350);
Animatetext.Text = "Parad";
Thread.Sleep(350);
Animatetext.Text = "Parado";
Thread.Sleep(350);
Animatetext.Text = "Paradox";
Thread.Sleep(350);
}
}
}
It seem your codes is about to run a form application. No console application.
But if you are trying to develop a console application, the console will close automatically because there is no command to make it stay or wait the next key press. So you can add a line to make the application stay before closing the Main() method.
static void Main(string[] args)
{
//
// your coding here
//
Console.ReadLine();
}
It is not a good practice to develop a console application via form application template. Unless to get debug output.
Editted
If you want to develop a Form app, you have to remove the Main method in your form class. The Main method is only called once to start the form but you no need to add in your Form classes.
Full Example for Form
Program.cs (Here is where the Main method is placed and called only once to start the other form.)
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
And now, here is the other form classes.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Make sure to includes all library needed. This example taken from VS2019 .NET Framework Form Application template.
Other Occurs
In VS, you can set to start your project with console or not. You can set to in your Project Properties > Application Tab > Output type set to Windows Application. By default this setting will set to template setting (if form: windows application and if console: console application).
For best practice in form application, use the debugger console and set output using Debug.WriteLine().

AudioVideoPlayback doesn`t work at all. How to fix it?

I am new to C# and testing Microsoft.DirectX.AudioVideoPlayback library function.(using VS2019)
VS said there are no error, but nothing happened when I compiled it.
I have tested below code line by line. and found that Init() function does not work at all. I have tested other paths such as
#"C:\Users\name\Videos\sample.mp4"
"C:\Users\name\Videos\sample.mp4"
"C:\Users\name\Videos\sample.mp4"
but all of them does not work at all too.
How to fix the code and why it did`t work properly?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;
using System.IO;
namespace hello
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Init();
}
public void Init()
{
Video v = Video.FromFile(#"C:\Users\name\Videos\sample.mp4");
v.Owner = this;
v.Size = new Size(1920, 1080);
v.Play();
}
}
}
This might be too simple question, but cannot gain any solution from other answers in this site.

c# the name 'xy' does not exist in the current context

I just started with C# at work and I did read like a lot of the questions that are about my error here at StackOverflow. Unfortunately I still don't get what I'm doing wrong.
So, I followed this (http://qafriend.com/c-ui-automation-tutorial/automate-using-c-tutorial-guide-part-5) tutorial that is about C# and UI Automation. Everything went fine until I had to write actual code. So my code is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Automation;
using Automation = System.Windows.Automation;
namespace AutomateCSharp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
static AutomationElement desktopObject = AutomationElement.RootElement;
static Automation.Condition testWindowNameCondition = new PropertyCondition(AutomationElement.NameProperty, "Demo Window For Csharp Automation");
static Automation.Condition textConditionOne = new PropertyCondition(AutomationElement.AutomationIdProperty, "InputOne");
static Automation.Condition textConditionTwo = new PropertyCondition(AutomationElement.AutomationIdProperty, "InputTwo");
static Automation.Condition textConditionTotal = new PropertyCondition(AutomationElement.AutomationIdProperty, "Total");
static AutomationElement testWindow = desktopObject.FindFirst(TreeScope.Children, testWindowNameCondition);
static AutomationElement textOne = testWindow.FindFirst(TreeScope.Descendants, textConditionOne);
static ValuePattern valuetextOne = textOne.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
valuetextOne.SetValue("4");
}
}
And I get the Error: The name 'valuetextOne' does not exist in the current context.
Could someone please tell me what I'm wrong about? Thanks a lot in advance :)
This problem has to do with the fact that functions can't be called inside class bodies directly. Only fields, properties and methods can be written there. What you need to do, is wrap it inside another function, like so:
public partial class MainWindow : Window
{
// Stuff
public void SetValue() => valuetextOne.SetValue("4");
}
And then you call it as:
public void Foo()
{
MainWindow window = Bar();
window.SetValue();
}
I go into more detail here about this.

C# CefSharp Blank Page

I am trying to learn how to use CefSharp as a browser in c# (unless anyone can suggest any better alternatives).
Initially I set the configuration to Any CPU (and edited the .csproj and app.config) , but the browser didn't display any content, instead it just shows a white/grey screen.
I then tried changing the configuration to x64 (as my PC is x64), but after doing got the following errors:
The type or namespace name 'Winforms' does not exist in the
namespace 'CefSharp'
The type or namespace name 'ChromiumWebBrowser' could not be found
The "FindUnderPath" task was not given a value for the required parameter
"Path".
The OutputPath property is not set for project 'cef.csproj'. Please check
to make sure that you have specified a valid combination of Configuration
and Platform for this project. Configuration='Debug' Platform='x64'. This
error may also appear if some other project is trying to follow a project-
to-project reference to this project, this project has been unloaded or is
not included in the solution, and the referencing project does not build
using the same or an equivalent Configuration or Platform.
My code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chrome;
public Form1()
{
InitializeComponent();
InitializeChromium();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void InitializeChromium()
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
string url = "https://www.google.co.uk/";
chrome = new ChromiumWebBrowser(url);
this.pContent.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
}
private void pContent_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
Any help or suggestions are greatly appreciated.

Categories