|
Wow, thanks for the quick reply. The link you provided is sure to be a good read, though after scanning through it I noticed that (in true CP tradition) the examples are simple, and nothing like what I am looking at.
using System;
namespace Net.SourceForge.Koogra.Excel
{
public interface IExcelObject
{
Workbook Workbook { get; }
}
} The first thing I notice with this code is public interface IExcelObject , I'm not sure exactly what it is but I see it mentioned a few times throughout the code and is treated much like a base class (in C++). Also it is returning a workbook reference, which I am sure is a no-no with com.
From what I can tell this is the main class
namespace Net.SourceForge.Koogra.Excel
{
public class ExcelObject : IExcelObject
{
private Workbook _wb;
public ExcelObject(Workbook wb)
{
if(wb == null)
throw new ArgumentNullException("wb");
_wb = wb;
}
public Workbook Workbook
{
get
{
return _wb;
}
}
}
} I see here there is no default constructor, so I added one thinking everything would be good and dandy, but nada, didn't fix the problem.
Now I am thinking, rather than trying to change this code, to create a wrapper instead. But with it being C# and my exeptionaly low IQ I'm not sure where to start.
-- modified at 14:15 Thursday 13th April, 2006
I just realised, I didn't state that this code is not my own, and belongs to Superaso[^].
|
|
|
|
|
Hi WalderMort (nice nick - by the way )
I believe you are right, that for COM interop to work you need a default constructor. And the short description of an interface is a contract. It defines the methods of a class that any derived class must implement. The interface itself contains no implementation (no code), just a description of what you have to code in (in this case an IExcelObject) the class that promises to implement the interface. The neat thing about an interface is that you can treat any class that fulfills the needs of the interface as that specific type, regardless of what the parent class is. (It's another way of overcoming the shortcomings of single inheritance in C# as opposed to C++).
The syntax (as you've noticed) is the same as for inheritance:
[classname] : [interface]
The ExcelObject looks as if it's just a wrapper around the WorkBook - is that the wrapper you are talking about?
The "no-no in COM" you are talking about is that the reference. Is it only valid with value-type parameters in COM?
- You're welcome to paste a bit more code and/or errormessages, I think it will help.
Kind regards - Jakob
*********************************************
Three kinds of people in the world:
- Those who can count..
- Those who can't!
10 kinds of people in the world:
- Those who understand binary
- Those who don't
|
|
|
|
|
Hi, thanks for your help with this matter. I would post more code but to be honest, there is far too much of it to post.
I now understand the meaning of interface thanks to your explanation.
The way this code differs from examples is that there is more than one class. ExelObject is a base class for many others (font, worksheet, cell..). I'm not sure how this works in C# but but in C++ creating an object of a derived class would also create a base class. The other problem is that most of the classes have no default constructor (i.e. constructors take arguments) which cannot be used in com.
So far I have tried creating a default constructor and renaming the previous to be a function. This is allowing me to create an instance of ExcelObject from C++, but I am now unable reference the renamed function. From the producer:
com visibility is not set on any type
Isn't it enough to declare a function as public?
It looks like I am going to have to make many changes to the sources of this project in order for it to be com compatible. I'm starting to think it would be easier to port the functions I need to C++.
|
|
|
|
|
Hi Waldermort
By default nothing is visible to COM from .net.
But it is easy to make it the default. This can be done at three different levels:
- the Assembly (basically the codepackage, or in many cases, the DLL)
- a class
- a method
So if you declare the Assembly ComVisible, then all the classes inside, and their methods are by default visible.
Declaring something visible to COM is done using attributes in .net (a form of metadata).
You set this in the AssemblyInfo.cs file by:
- importing the namespace System.Runtime.InteropServices by declaring
using System.Runtime.InteropServices at the top of the file.
- somewhere add the line
[assembly: ComVisible(true)]
Hope this helps
Kind regards - Jakob
*********************************************
Three kinds of people in the world:
- Those who can count..
- Those who can't!
10 kinds of people in the world:
- Those who understand binary
- Those who don't
|
|
|
|
|
Hi All,
Could any of you kind folk give me some tips on this quandry?
I'm designing an application that will need to access a database. Having learnt about the benefits of OOP I've decided to manage the database with a DataAccess class, rather than have each object access the database independently. However, I'd only want to have one DataAccess object instantiated within the application - so my question is "how can I make the DataAccess object visible to each object within the application".
Any advice appreciated!
-- modified at 13:28 Thursday 13th April, 2006
|
|
|
|
|
This design pattern is commonly called the Singleton, so you can look up some more information if you want. The easiest way is to make the class Public Static. That way all the functions inside the class can be called without creating an instance of the class, emulating one global, Application-wide instance.
|
|
|
|
|
|
In VB6 we have app.path which returns the path of the application, how do we do this in c#????
|
|
|
|
|
In C# 2.0 you can use:
Application.StartupPath
And this will work in 1.1 and 2.0
private void button1_Click(object sender, System.EventArgs e)
{
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
MessageBox.Show( path );
}
Cheers,
Martin
-- modified at 13:45 Thursday 13th April, 2006
|
|
|
|
|
|
hi i need to call
doPostback(...).
how to do that
|
|
|
|
|
call the function like any other function.
What is the problem?
--------------------------------------------------------
My development blog
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
I have created a new tool that uses reflection to find some subcontrols. When loading the new tool into a window in designer mode, I get errors from the control that I think are related to finding the subcontrols.
Is there a good way to debug code being used in designer mode? The control functions correctly when I run it directly. Also, when a control is added to the tool box, what is a good way to refresh it without closing and reopening the project.
gsquared
|
|
|
|
|
Try to separate code for design and code fore runtime.
You can use constuction:
if (this.DesignMode)
{
// Design actions
}
else
{
// Runtime actions
}
You can save info to logfile.
Also you can see callstack, when you see error - analize this info.
Best regards, Alexey.
|
|
|
|
|
How can I write in the fields of a form outside of its class? For example, I have a Form1 and a DoWork class. I want the DoWork class write something in a text field in Form1. I can't access the fields directly, so there should be another way.
|
|
|
|
|
This question is asked so many times in the forum....you really ought to search the forum before asking.
Basically, create a property in your form that exposes the variable:
string myVar;
...
public string MyVar
{
get
{
return myVar;
}
}
And your DoWork class just needs an instance of Form1 to access its public members:
class DoWork
{
private Form1 form;
public DoWork(Form1 formInstance)
{
this.form = formInstance;
string theVar = this.form.MyVar;
}
}
|
|
|
|
|
adol_w wrote: I can't access the fields directly, so there should be another way.
What exactly do you mean with that?
Some first guesses:
Change their visibility to public or better declare a public property with get- and/or set-body (as needed) to access them.
Hand over an instance of your Form1 class when you construct the DoWork class instance, store it in a private field and use it to access the fields when needed.
www.troschuetz.de
|
|
|
|
|
The quick and proper way:
Create public properties for the fields in DoWork and access them that way.
The slow and sneaky way: Use Reflection, like this:
FieldInfo fi = myDoWork.GetType().GetField("someFieldInDoWork", BindingFlags.NonPublic | BindingFlags.Instance);<br />
fi.SetValue(myDoWork, someValue);
Regards,
Alvaro
The bible was written when people were even more stupid than they are today. Can you imagine that? - David Cross
|
|
|
|
|
This has been asked so many times I wrote an article about it: See Passing values to forms[^]
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
My: Website | Blog
|
|
|
|
|
Sorry, I can't believe I didn't see that article. Thanks for the answers. I just hope nothing significant has changed in .net 2.0.
|
|
|
|
|
Hi
I am doing a division and the result gives me an answer with decimal places. How do I remove all the characters after the decimal place?
|
|
|
|
|
double d = 13/7;
int i = Convert.ToInt32(d);
|
|
|
|
|
|
That depends.
Do you want to truncate or round the value?
Are you going to process the value further, or is it only to be displayed?
If you want to truncate the value (remove decimal part), cast the value to int or use Math.Truncate.
If you want to round the value (to nearest integer), use Math.Round.
If you want to round it only for displaying it, do the rounding when you convert it to a string, using ToString("n0").
---
b { font-weight: normal; }
|
|
|
|
|
hi
How to invoke post back.I need some function like invalidRect in win app.
thank you for help
best regards
|
|
|
|