I'm working on a cross platform app using the GTK toolkit and when I'm trying to access the System.IO namespace to extract Path fields, I'm getting the following error:
Error CS0119: 'Widget.Path(out uint, out string, out string)' is a method, which is not valid in the given context (CS0119) (netmonmd)
I have using System.IO; in the source file and I'm using other namespaces without any issue (eg System.Console and System.Refelection without any issues further down in the code).
I have almost identical code in a console application working without an issue.
Here is the code, if I fully provide the Path it works, I just can't use it within the namespace (but I can use other things that are in the same namespace)
using Gtk;
using System;
using System.IO;
using static System.Console;
public partial class MainWindow : Gtk.Window
{
private netmonmd.iplabel[] ids;
private int fSize = 12;
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
//char filesep = System.IO.Path.DirectorySeparatorChar; // works
char filesep = Path.DirectorySeparatorChar; // error
I'm trying to understand the error (I am new to C#) and think it has to do with calling it within the MainWindow function since it seems to be referring to Widget.Path and not System.Path
Is this a case of just having to fully qualify it each time?
I could move all the code out of the main window and process it elsewhere in the app.cs file, however it is a single window app that I was developing in the one space. If it is better practise to separate it out I will do that.
(as is usually the case in asking on stack, while writing this question out I'm becoming more aware of what's actually happening here, I just don't know how to work around it)
Related
I am getting an error on my code that says "Error CS5001
Program does not contain a static 'Main' method suitable for an entry point"
I am coding in C# using Microsoft Visual Studio and .NET. This is my code.
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
class projectsummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
string strFileName = "C:\\DRAFT.dwg";
DocumentCollection acDocMgr = Application.DocumentManager;
if (File.Exists(strFileName))
{
acDocMgr.Open(strFileName, false);
}
else
{
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName +
" does not exist.");
}
}
}
I am not sure how to go about this error. Thank you!
Looking at this post and your previous question, let's try and break down what's going on.
You created a new Console application in Visual Studio. You did not tick "Do not use top level statements". This gave you a Program.cs file that was essentially empty (there was no "Main" method visible).
You erased the Hello World code given to you, and went to make a static method - the code from your previous question.
Damien_The_Unbeliever commented that based on the error, you put your method inside a "top level statement" file, and to put your method inside a class.
You wrap your method (which is still inside Program.cs) in a class, and now suddenly you get a Can't Find Entry Point error.
User Ryan Pattillo posted a great explanation of the original issue - where your method was "by itself" in the Program.cs file. You should follow their advice, but you should also ensure that this class is in its own file.
You should end up with this:
Program.cs
// this is the entire contents of the file
using ConsoleApp1;
ProjectSummer.OpenDrawing();
ProjectSummer.cs
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
namespace ConsoleApp1
{
public class ProjectSummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
// ...
}
}
}
Change ConsoleApp1 to the name of your project.
The entry point of your application, which right now is the only file that has "top level statements", remains Program.cs, thus you fix the Can't Find Entry Point error.
Another adjustment you can make, which seeing you're new to C# might be useful, is to not use top level statements at all. Modify your Program.cs to this:
namespace ConsoleApp1
{
internal static class Program
{
// this is your program's entry point
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
ProjectSummer.OpenDrawing();
}
}
}
Change ConsoleApp1 to the name of your project.
You cannot use the AutoCAD .NET API out of process. To be able to use the AutoCAD .NET libraries, you have to build a "class library" project (DLL) and NETLOAD this DLL from a running AutoCAD process. See this topic about in-process vs out-of-process and you can start from this other one to see how to create an AutoCAD .NET project.
I have an old line of c# code that looks basically like this:
foo.set_Parent(parent);
It has compiled fine for years. Now in VS2015 I get the error:
CS0571 'Foo.Parent.set': cannot explicitly call operator or accessor
So I can rewrite the line as:
foo.Parent=parent;
This builds fine in VS2015, but in VS2013 it gives the error:
'Foo.Parent' is not supported by the language; try directly calling
accessor methods 'Foo.get_Parent()' or Foo.set_Parent(Foo)'
So the simple fix is to simply ifdef these two lines based upon which version of the compiler is running. But how do you detect which version of the compiler is executing?
And for the record, no, I can't just dictate that everyone on the team simultaneously upgrades to VS2015.
Additional info -
For everyone smelling a rat, I'll go ahead and drag out the ugly truth, although I don't think it will change much of anything. The class Foo is from an ancient Borland assembly that is all bound up in Delphi (and yes, we're migrating away but not there yet). So the actual code, that compiles up to VS2013, looks like this:
using Borland.Vcl;
using RepGen;
using SnapReportsForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace MigrantCOM {
[ComVisible(true)]
[Guid("48245BA3-736B-4F98-BDC5-AD86F77E39F4")]
[ProgId("MigrantCOM.Exports")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MigrantCLRExports { // : MarshalByRefObject
public string Test(string s) { return s+s; }
}
[ComVisible(true)]
[Guid("1154D364-B588-4C31-88B9-141072303117")]
[ProgId("MigrantCOM.SnapRepCOM")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class SnapRepCOM {
TRepGen repGen;
TStringList snapRefs=new TStringList();
TForm parent=new TForm(null);
TMemo designerMemo;
List<TReference> references=new List<TReference>();
TRunAsSnapContext runAsSnapContext=new TRunAsSnapContext();
public SnapRepCOM() {
designerMemo=new TMemo(parent); designerMemo.set_Parent(parent);
...
}
So the class being instantiated is Borland.Vcl.TMemo which is part of the old Delphi assembly.
I'm leaving this as an answer, linking an image will fit better here than in a comment.
So if you want to use VS 2015 but still use the same good ol' version of the C# language that worked for years, you can configure your project to target a specific version:
This adds <LangVersion>5</LangVersion> in the csproj.
If the current (most recent) item in the clipboard is a string, I want to take the string from the clipboard, and put each word in an array (just a simple loop which eliminates spaces and newlines, to take just words from the string). Then I simply want to print each item in the array (let's say each separated by a newline for testing purposes) to the terminal.
This is super easy, simple, etc. My trouble is that I cannot seem to find/use the Clipboard class that is built in to C#! Here is what I am using as my assembly references:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Object;
using System.Windows.Clipboard;
Note that the System.Windows.Clipboard; gets a red underline, because it is not lining up. Why could this be? http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx shows the clipboard class information.
It's probably a fault on my end, but is it otherwise possible I have not set my paths correctly or something? I just installed Visual Studio 2012 For Desktop SP3 (although, I have had VS2012 for Web for quite some time, and it works properly).
System.Windows.Clipboard is a class and not a namespace, there is no point putting a class into a using directive.
The compiler error message should tell you the same.
First of all you must add a reference to System.Windows.Forms in your application. Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened. You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.
using System;
using System.Windows.Forms;
class Program {
[STAThread]
static void Main(string[] args) {
Clipboard.SetText("this is in clipboard now");
}
}
i hope this will help you
http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/
it shows how to use the Clipboard functions in C# console application
You don't need this line: using System.Windows.Clipboard; using statements are for namespaces, and Clipboard is a class. That class is provided from the assembly PresentationCore.dll, and since you have WPF project, your project already has a reference to it.
You should have reference to System.Windows namespace only it has Class Clipboard in it.
You can set and get data from there
System.Windows.Clipboard.SetData();
You'll need a namespace declaration:
using System.Windows.Forms;
OR for WPF:
using System.Windows;
To copy an exact string
var dataObject = System.Windows.Clipboard.GetDataObject();
string text= dataObject.GetData("UnicodeText", true).ToString();
I'm creating an add-on system for a shell I'm developing using C#. I've followed this and this. Here is my function to load an add-on:
public void loadAppFromDLL(string assemblyFile)
{
Assembly a = Assembly.Load(assemblyFile);
Type app = a.GetType("App");
MethodInfo loadMethod = app.GetMethod("load");
object appInstance = Activator.CreateInstance(app);
loadMethod.Invoke(appInstance, null);
}
Here is the add-on:
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace App
{
public class App
{
public void load()
{
MessageBox.Show("Application loaded successfully!");
}
}
}
When I build the add-on, I place it in the same directory as the shell executable and call:
LoadExternalApp lea = new LoadExternalApp();
lea.loadAppFromDLL("SampleApp");
(LoadExternalApp contains the DLL loading function)
When I was debugging my shell, I noticed that:
The app didn't start
There was a System.NullReferenceException
What am I not doing right?
This:
Type app = a.GetType("App");
is looking for a type with a namespace-qualified name of App.
Your type is called App in a namespace of App, so Assembly.GetType is returning null, and then you're dereferencing it. Instead, you should use:
Type app = a.GetType("App.App");
However, you shouldn't give a class the same name as its namespace in the first place. Fix that, so that you end up with something more like:
Type app = a.GetType("App.PlugIn");
You should still check whether GetType (or GetMethod) returns null, in order to fail rather more gracefully and with more information.
Additionally, you should start following .NET naming conventions - give methods names in PascalCase. Oh, and you might want to consider a common interface for your add-ins rather than relying on reflection to call methods.
I have written a simple Form1 (inherit from Form) class in Visual Studio C#. All was good.
Then I wanted to change the name of the class and the namespace to something meaningful instead of the default 'WindowApplicationForm1' . I also changed the file name of the Form1.cs class to match the new class name (IRISReaderGUI and Com.Harmonysoft). First I manually rename the code, then compiled, the compiler gave me lot of error that I could not figure out. So I tried to rename my class and namespace using 'refactoring' menu. My code still didn't compile.
I did some research and change the 'IRISReaderGUI.Designer.cs' class name and namespace to match the new names. C# still didn't give me no joy.
The compiler erros message was :
'Com.Harmonysoft.IRISReaderGUI.Dispose(bool)': no suitable method
found to override'
On the designer view it said : The base class'System.object' can not be designed.
So I guess my IRISReaderGUI did not correctly inherit the System.Windows.Forms.Form class, this was confirmed by putting the mouse over the Form word in the code, Visual Net does not popup the text describing the class, and pressing F12 does not get me to the Form definition.
Here is the code :
"IRISReaderGUI.Designer.cs"
namespace Com.Harmonysoft
{
partial class IRISReaderGUI
{
.......
}
"IRISReaderGUI.cs"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using Com.StellmanGreene.CSVReader;
using System.Windows.Forms;
using System.IO;
namespace Com.Harmonysoft
{
public partial class IRISReaderGUI : Form
{
......
}
I am new to C# and Visual Studio, I have previously worked with Java compiler and Vim editor mostly. Could anyone please help me to compile my code ?
Forms in VS2005 and up are partial classes. You edited the one part but not the other. In the Solution Explorer window, expand the node next to the form and double-click the Designer.cs file to open it.
Using the Refactor + Rename context menu command is the better approach, it doesn't forget to edit the other source files as well. It didn't work when you tried it because the damage was already done, the parts no longer had the same name.