Routine call in other namespace - c#

I wanted to use a DLL library code and link / reference it with my c# code. The reference seems to be working, when defining var instance = new ProjectM1() without any problems.
But in the following line appears an error that ProjectM1 does not contain a definition for ProjectM1 and it couldn't find a extension method (Maybe a using directive or assembly reference is missing)
namespace GanttC
{
public class ProjectM1 : ProjectM1<Task, object>
{
}
public class ProjectM1<T, R> : IProjectM1<T, R>
where T : Task
where R : class
{
public ProjectM1() (routine to call !)
{
Now = 0;
Start = DateTime.Now;
TimeScale = GanttC.TimeScale.Day;
}
}
}
Here now the main code to call routine ProjectM1 in public class ProjectM1:
using GanttC
namespace WindowsApp2
{
public partial class Form4 : Form
{
Form2 fh;
public Form4(Form2 caller)
{
fh = caller;
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
var instance = new ProjectM1();
instance.ProjectM1(); (error !!!)
}
}
}

Related

Calling "Form" class method from "A" class without adding a reference to the "Form" class

I have two Projects one is a Winform application another is a Class library. I have added a reference to the class Library in Winform and called a method of the class library. Now I want to call a different method in winform application from class library but I can't add a reference to winform to the class library.
IN CODE:-
public partial class Form1 : Form
{
private void btn_Click(object sender, EventArgs e)
{
A obj = new A();
obj.foo();
}
public string Test(par)
{
//to_stuff
}
}
and in Class library
class A
{
public void foo()
{
//Do_stuff
//...
Test(Par);
//Do...
}
}
You can achieve this by injecting Test into class A.
For example:
public partial class Form1 : Form
{
private void btn_Click(object sender, EventArgs e)
{
A obj = new A();
obj.foo(Test);
}
public string Test(string par)
{
//to_stuff
}
}
class A
{
public void foo(Func<string, string> callback)
//Do_stuff
//...
if (callback != null)
{
callback(Par);
}
//Do...
}
}
While the callback method from David is a sufficient solution, if your interactions gets more complex, I would use this approach
Create an inteface in your class libary
public interface ITester
{
string Test(string value);
}
Rewrite your code so class A expects an ITester interface
public class A
{
public A(ITester tester)
{
this.tester = tester;
}
public string foo(string value)
{
return this.tester.Test(value);
}
}
Implement your interface in Form1
public partial class Form1 : Form, ITester
{
private void btn_Click(object sender, EventArgs e)
{
A obj = new A(this);
obj.foo("test");
}
public string Test(string value)
{
//to_stuff
return value;
}
}

C# WebBrowser control: window.external access sub object

when assigning an object to the ObjectForScripting property of a WebBrowser control the methods of this object can be called by JavaScript by using windows.external.[method_name]. This works without problems.
But how I need to design this C# object when I have a JavaScript function like this (accessing a sub object): window.external.app.testfunction();
I tested it with following C# object assigned to the ObjectForScripting property:
[ComVisible(true)]
public class TestObject
{
public App app = new App();
}
public class App
{
public void testfunction()
{
}
}
But this unfortunately does not work and leads to a JavaScript error saying "function expected".
Any idea on how the C# object has to look like that this JavaScript command is working?
Thank you for any tips on that
Andreas
I suggest you use InterfaceIsIDispatch-based interfaces to expose the object model from C# to JavaScript:
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IApp
{
void testFunction();
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestObject
{
IApp App { get; }
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(ITestObject))]
public class TestObject: ITestObject
{
readonly App _app = new App();
public IApp App
{
get { return _app; }
}
}
[ComVisible(true)]
public class App : IApp
{
public void testFunction()
{
MessageBox.Show("Hello!");
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.webBrowser1.ObjectForScripting = new TestObject();
this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
this.webBrowser1.Navigate("about:blank");
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Navigate("javascript:external.App.testFunction()");
}
}
}

Giving object in dll a click method from another project

is it possible to adding a click method to a class in dll from another project?
I want to create a class (Class1) in a class library and build a dll from it.
I will use that class in a project with references the dll.
This is my class (Class1)
public class Class1
{
public ImageMap map = null;
public Class1(Form f)
{
map = new ImageMap();
map.RegionClick += f.RegionMap_Clicked;
}
}
and this is my form (Form1) in another project.
public partial class Form1 : Form
{
Class1 c = null;
public Form1()
{
InitializeComponent();
c = new Class1(this);
}
void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
}
This is my first time asking here. So, sorry if my english is bad.
Class1 can be made independent from Form1:
public class Class1
{
public ImageMap Map = null;
public Class1()
{
this.Map = new ImageMap();
}
}
And Form1 uses Class1 however it likes:
public partial class Form1 : Form
{
private Class1 c = null;
public Form1()
{
InitializeComponent();
this.c = new Class1();
this.c.Map.RegionClick += this.RegionMap_Clicked;
}
private void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
}
Thus only the Form1 project needs a reference to the Class1 project.
Yes it's possible, don't forget to make your handler public:
public void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
You should do this
public class Class1
{
public ImageMap map = null;
public Class1(Form1 f)
{
map = new ImageMap();
map.RegionClick += f.RegionMap_Clicked;
}
}
then
public partial class Form1 : Form
{
Class1 c = null;
public Form1()
{
InitializeComponent();
c = new Class1(this);
}
public void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
}
and of couse you shold add using for this assembly.
I think now it will be working well

How to access a form function from a different class

public partial class Form1 : Form
{
public disp(string strVal)
{
lbl1.text = strVal;
}
private void button1_Click(object sender, EventArgs e)
{
class1 cl = new class1;
cl.Show1("test",this);
}
}
---------------- Class ------------
public class class1
{
private Form frm1;
public void Show1(string xName , object xfrmObj)
{
frm1 = (form) xfrmObj;
frm1.disp(xName ); // here I am getting error .
}
}
------------------------------------------/
here i am trying to access 'disp' function from class and i have pass 'form1' as a object , but i am getting error
The error message that I am getting is
Error 3
'System.Windows.Forms.Form' does not contain a definition for 'disp'
and no extension method 'disp' accepting a first argument of type 'System.Windows.Forms.Form' could be found
(are you missing a using directive or an assembly reference?)
this syntax in vb.net is working perfect.
please help me.....
Rajesh.
you need to cast frm1 = (Form1)xfrmObj; instead of casting it to form in your Show1(xName,xfrmObj) method.
EDIT: OP has stated in a comment that he needs this to work for several different forms.
You can make all your forms implement the same Interface, like so:
public partial class Form1 : Form, ICanDisplay
{
public void disp(string strVal)
{ //...
}
}
public partial class Form2 : Form, ICanDisplay
{
public void disp(string strVal)
{ //...
}
}
public interface ICanDisplay
{
void disp(string strVal);
}
then, change your method so it casts to ICanDisplay:
public class class1
{
private Form frm1;
public void Show1(string xName , object xfrmObj)
{
frm1 = (ICanDisplay) xfrmObj;
frm1.disp(xName);
}
}
However, as #Heinzi has noted, you should change your Show1-method to the following:
public void Show1(string xName, IDisplayForm xfrmObj)
{
xfrmObj.Disp(xName);
}
this will make the cast entirely unnecessary. The next step is to select meaningful names for your variables, functions and classes.

object oriented: is it possible use a static instance like a variable?

I think I couldnt do this thing, but I try to ask (maybe :)).
Suppose I have this Main class :
public class UiUtils
{
public static MyObject myObject;
public UiUtils()
{
myObject=new MyObject();
}
}
now if I want to try to call this instance from another Context Class (web application), I do this :
public partial class context_eventi_CustomClass : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Console.Write(UiUtils.myObject.Title());
}
}
but what I'd like to do is this :
public partial class context_eventi_CustomClass : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Console.Write(myObject.Title());
}
}
so, use directly myObject and not UiUtils.myObject :)
I think this is not possible, but maybe I wrong and there are any strategies :) Thanks
** EDIT **
my solution for the moment :
public class UiUtils
{
public static MyObject myObject;
public UiUtils()
{
myObject=new MyObject();
iContext.myObject = myObject;
}
}
public class iContext : System.Web.UI.UserControl
{
public static MyObject myObject;
public iContext()
{
}
public iContext(MyObject myObject)
{
myObject = myObject;
}
}
public partial class context_eventi_CustomClass : iContext
{
protected void Page_Load(object sender, EventArgs e)
{
Console.Write(myObject.Title());
}
}
seems to works! What do you think about?
Per MSDN,
A static method, field, property, or
event is callable on a class even when
no instance of the class has been
created. If any instances of the class
are created, they cannot be used to
access the static member. Only one
copy of static fields and events
exists, and static methods and
properties can only access static
fields and static events. Static
members are often used to represent
data or calculations that do not
change in response to object state.
and
"To access a static class member, use the name of the class instead of a variable name to specify the location of the member."
and
The static member is always accessed
by the class name, not the
instance name
#Daniel Earwicker says in his answer on SO here:
...Static members fail to integrate
well with inheritance. It makes no
sense to have them heritable. So I
keep static things in separate static
classes...
So I am not clear on your design why MyObject needs to be static. All you are trying to save is a little typing, but inheritance will not help you here either.
Edit:
I tried to replicate your code in a simple console application. The output is not what you would expect:
namespace ConsoleApplication1
{
public class UiUtils
{
public static int myObject = 1;
public UiUtils()
{
myObject = new int();
iContext.myObject = myObject;
Console.WriteLine("This is UiUtils\n");
}
}
public class iContext
{
public static int myObject = 2;
public iContext()
{
Console.WriteLine("This is iContext\n");
}
public iContext(int myObject)
{
myObject = myObject;
}
}
public class iContext2 : iContext
{
public static int myObject = 3;
public iContext2()
{
Console.WriteLine(myObject.ToString() + "\nThis is iContext2\n");
Console.WriteLine(iContext.myObject.ToString());
}
}
class Program
{
static void Main(string[] args)
{
iContext2 icontext = new iContext2();
Console.In.ReadLine();
}
}
}
The output ends up being this:
This is iContext
3
This is iContext2
If you add a call to iContext.myObject, then it outputs it's number:
This is iContext
3
This is iContext2
2
To access the object without typing the class you can use inheritance.
public class CustomClass : UiUtils
This will share UiUtils properties with CustomClass

Categories