|
Lodeclaw wrote: I've looked through the documentation already
Did you find this?[^]
I'm not saying that's a good way to go about this, however it does seem to have some possibilities doesn't it?
|
|
|
|
|
I don't seen anything there that will help me in the copying process of an entire datagridview. I am filling my initial dgv in that manner.
In any case, I was hoping to find an easy way to transfer the data for easy viewing on an invoice form, but since as you said there are no examples of code... I'm now using a for loop to collect the data from my rows and populate the child form's dgv. It's probably better this way in the long run.
Thanks for the help, Mike.
|
|
|
|
|
Hello everyone,
1- Assuming the following DLL
<br />
namespace test_dll<br />
{<br />
public class test<br />
{<br />
public static int test_add(int a, int b)<br />
{<br />
return (a+b);<br />
}<br />
}<br />
}<br />
2- After compiling, I get (test_dll.dll)
3- I copy the dll and paste it in (test_program) (bin/debug) -test_program is a different project-
4- From the test_program program logic I do the following
<br />
namespace test_program<br />
{<br />
<br />
public class test_dll_user<br />
{<br />
[DllImport("test_dll.dll",EntryPoint="test_add")]<br />
extern static int test_add(int x, int y);<br />
<br />
public int MyAdd(int a, int b)<br />
{<br />
return test_add(a,b);<br />
}<br />
}<br />
}<br />
Now! The code compiles fine!
Once I would like to use the MyAdd function
<br />
Line 1: test_dll_user obj = new test_dll_user();<br />
Line 2: obj.test_add(1,2);<br />
I get a compiler Error (Unable to find an entry point named 'test_add' in DLL 'test_dll.dll')!
I been looking all over the place for a solution with no luck!
Moreover, for business reasons, I don't want to add the dll as a reference! What can I do? Any help would be exteremly appreciated. Thanks
|
|
|
|
|
Hi,
you are mixing to incompatible things:
1. DllImport is needed when you want to access native code in some DLL file, e.g. a Win32 API function in user32.dll
The DLL you want must be found by the normal Windows search, i.e. it should be in a folder that is listed in the environment variable PATH.
2. However, when compiling managed code, as you did in namespace test_dll { public class test...
the result is a managed DLL (unless you made it create an EXE), which you can reference from another project, and just use the types it contains. You typically need two things:
- add the reference in the solution pane
- add a using statement to import your namespace
Now start creating types as in test t=new test(); Console.WriteLine(t.test_add(1,2));
BTW this is elementary; have you studied a tutorial on C# at all? your naming conventions are non-
existing. At least read some of the CodeProject articles!
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:58 AM
|
|
|
|
|
Hi,
I am new to c#, my reading on the subject is not that great! But, it looks like you didn't read my question. At least well!
1- I am aware of both advices! But... thanks anyway.
2- I do not want to use the REFERENCE statement or add the dll as a reference
(it was very clear in my question)
using test_dll; // this is out of the picture!
3- The naming conventions, what difference does it make!
4- I don't see the incompatibility at all! If you are familiar with using C++ dlls from c#, then,
the question goes like this:
I want to do the same thing in c# without A: referencing B: using the "using" statement
Thanks for the reply.
|
|
|
|
|
Hi,
as I said DllImport will not work for managed code.
If you don't want a static reference, you need reflection.
It's not really beginner's stuff though.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:58 AM
|
|
|
|
|
Thanks for the reply.
could you provide me a code example please!
|
|
|
|
|
Thanks folks. Reflection was the way to go: here is an example (Hopefully, it will benefit someone)
public class Example
{
public string GetFileName()
{
Assembly assem = Assembly.LoadFrom(@"C:\Test.dll");
Type typClsX = assem.GetType("Test.Class1", true);
object oClsX = Activator.CreateInstance(typClsX);
MethodInfo miGncn = typClsX.GetMethod("ReturnFileName", new System.Type[0]);
string intAddFunc = (string)miGncn.Invoke(oClsX,null);
return intAddFunc;
}
public int DoAdd(int x, int y)
{
object[] Parms = {x,y};
Assembly assem = Assembly.LoadFrom(@"C:\BasicMath.dll");
Type typClsX = assem.GetType("BasicMath.Operations", true);
object oClsX = Activator.CreateInstance(typClsX);
MethodInfo miGncn = typClsX.GetMethod("DoAdd");
int intAddFunc = (int)miGncn.Invoke(oClsX, Parms);
return intAddFunc;
}
}
|
|
|
|
|
Lookup reflection - thats the way to dynamically instantiate .NET classes without the need to reference them.
|
|
|
|
|
Could you provide a code example please?
|
|
|
|
|
I know .NET doesn't like Generics or serialization based on anything but XML but I would like an explanation of the following behavior given the code pasted at the bottom.
When SelectNotClassified is set to null the code executes fine with no errors. The GetValue method returns null. In fact, changing info.GetValue to typeof(object) still returns null which is expected.
When SelectNotClassified is not null the code throws an InvalidCastException. info.GetValue(..., typeof(bool)) would work fine and returns a Boolean results but interestingly info.GetValue(..., typeof(object)) returns a string value of "true" (see my post in the Lounge about things that really p*** me off about .NET). This means that (bool?)info.GetValue(..., typeof(object)) is not a valid solution since a string cannot be cast as a Boolean, only converted.
There are many workarounds to the issue but I am interested in the why and how to write this code as it should be written. Double casting, ternary operators, and parsing from what should be a binary stream all seem ridiculous to me. Am I missing the obvious info.GetValue statement that should be working?
GetObjectData(SerializationInfo info, StreamingContext context){
...
info.AddValue("selectNotClassified", (bool?)SelectNotClassified);
...
}
public Foo(SerializationInfo info, StreamingContext context){
...
SelectNotClassified = (bool?)info.GetValue("selectNotClassified", typeof(bool?));
...
}
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Just checking the obvious, but you do realise that bool? is shorthand for the struct System.Nullable< bool> yeah?
The complier is probably selecting the AddValue(string, bool) overload, and putting a bool in the ourput stream.
I would suggest that info.GetValue(..., typeof(object)) is actually returning a boxed bool, which in the watch window looks a lot like a string (as .ToString() is called). You can't cast directly from a boxed boolean to a bool? - you will have to go with new bool?(GetValue(bool)).
If you really need a bool? in your serialized object, then use the AddValue(string, object, Type) overload, and specify typeof(bool?).
|
|
|
|
|
Yes, I do and I also realize that .NET serialization does not support generics requiring custom serialization and it just really irks me that something as simple as a Nullable type requires custom code.
Mark Churchill wrote: Just checking the obvious, but you do realise that bool? is shorthand for the struct System.Nullable< bool> yeah?
I don't think so because null comes back just fine.
Mark Churchill wrote: The complier is probably selecting the AddValue(string, bool) overload, and putting a bool in the ourput stream.
Check the code as I did. GetValue is returning null when AddValue has added a null and typeof(object) is actually returning a string not a boxed type. I attempted info.GetValue(..., typeof(object)).GetType() to locate.
Mark Churchill wrote: I would suggest that info.GetValue(..., typeof(object)) is actually returning a boxed bool, which in the watch window looks a lot like a string (as .ToString() is called). You can't cast directly from a boxed boolean to a bool? - you will have to go with new bool?(GetValue(bool)).
This thing is really just messed up beyond recognition. I can write code to account for it without difficultly but I shouldn't have to. My post is with the hopes of finding some expert in serialization that can go aha, Ennis, you missed this hidden special feature in .NET that does it like we all expected it to work.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Also, along with the joys of boxing, the statement object foo = new bool?(false); will unwrap the nullable object and give you a plain boxed boolean in foo.
Other that that it sounds like you are probably using the XML/SOAP formatter and everything is getting munged. Any reason you need it serialized as XML?
|
|
|
|
|
When using the Serialization constructor and GetData interface One would assume no knowledge of the particular serialization method in use. And, this particular project is using Binary Serialization over .NET remoting.
Again, back to my original post, wtf. None of this should be an issue. GetValue should return the value assigned from SetValue without any other consideration.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Hrmm, this works with the BinaryFormatter (but not the SoapFormatter) - so I'd suggest the problem exists in your code somewhere.
It would be nice if the SoapFormatter could be made to work completely transparantly. Unfortunately this isn't possible because of the impedance mismatch between the SOAP standards and .Net model (a simple example is SOAP will round off your timestamps). This is just something a user will have to understand when using the API.
When I've had to use XML serialization seriously I've generally worked from and xsd, and used the xsd tool.
[TestFixture]
public class RandomTests
{
[Serializable]
public class Foo : ISerializable
{
public bool? bn = true;
public bool? bt = null;
public bool? bf = null;
public Foo()
{
bn = null;
bt = true;
bf = false;
}
protected Foo(SerializationInfo info, StreamingContext context)
{
bn = (bool?)info.GetValue("bn", typeof(bool?));
bt = (bool?)info.GetValue("bt", typeof(bool?));
bf = (bool?)info.GetValue("bf", typeof(bool?));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("bn", bn);
info.AddValue("bt", bt);
info.AddValue("bf", bf);
}
}
private SerializationInfo _info;
private Foo foo;
[TestFixtureSetUp]
public void Setup()
{
var stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, new Foo());
stream.Seek(0, SeekOrigin.Begin);
foo = (Foo) formatter.Deserialize(stream);
}
[Test]
public void BoolFalse()
{
Assert.IsFalse(foo.bf.Value);
}
[Test]
public void BoolTrue()
{
Assert.IsTrue(foo.bt.Value);
}
[Test]
public void BoolNull()
{
Assert.IsNull(foo.bn);
}
}
|
|
|
|
|
I am allegedly using the BinaryFormatter as well
<channels>
<channel ref="tcp" />
<serverProviders>
<formatter href="binary" />
</serverProviders>
</channels>
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Hi Folks,
Just using a DataGridBoolColumn; allowing Nullvalue as DBNull.Value.
All three states are shown als it schould be (explained by msdn). true=checked,false=unchecked and DBNull= grey and checked. For my app. it is necessary to have the DBNull-State shown as greyd and unchecked.
How to?
Version: Studio 2003 and Framework 1.1
Thanks for advance
|
|
|
|
|
I'm creating a function that should return the max value of a field within the database..
1. Any idea why I'm getting the following compilation errors?
2. I need to return a value of type System.Data.Sqltypes.SqlInt64, whilst the return value is a string, any idea how to do the conversion? An explicit cast does not work i.e.
if (rawValue != DBNull.Value) {
return {System.SqlTypes.SqlInt64.rawValue.ToString();
Errors:
1. Warning 1 Unreachable code detected C:\Inetpub\wwwroot\TaskEntry\TaskEntry.aspx.cs 163 13 C:\...\TaskEntry\
2. Error 2 'TaskEntry.nextTaskRef()': not all code paths return a value C:\Inetpub\wwwroot\TaskEntry\TaskEntry.aspx.cs 149 43 C:\...\TaskEntry\
private System.Data.SqlTypes.SqlInt64 nextTaskRef() {
try
{
string getTaskRef = @"SELECT MAX([taskID_PK]) FROM [teamwiki].[dbo].[task];";
SqlCommand getTaskRefSQL = new SqlCommand(getTaskRef, conAppDB);
object rawValue = getTaskRefSQL.ExecuteScalar();
if (rawValue != DBNull.Value) {
return (System.Data.SqlTypes.SqlInt64) rawValue;
}
}
catch (Exception ex) {
throw ex;
return 0;
}
finally {
conAppDB.Close();
}
}
Environment notes
Visual Studio 2005 Team edition / .NET 2.0
ASP.NET
SQL Server 2005
Win XP SP3
Jon
modified on Wednesday, January 28, 2009 10:00 AM
|
|
|
|
|
Yep,
you are only returning a value if the rawValue is not null.
Put a return null statement at the end of the method if you like.
Regards
Sebastian
|
|
|
|
|
Hi,
private System.Data.SqlTypes.SqlInt64 nextTaskRef()
{
try
{
string getTaskRef = @"SELECT MAX([taskID_PK]) FROM [teamwiki].[dbo].[task];";
SqlCommand getTaskRefSQL = new SqlCommand(getTaskRef, conAppDB);
object rawValue = getTaskRefSQL.ExecuteScalar();
if (rawValue != DBNull.Value) {
return (System.Data.SqlTypes.SqlInt64) rawValue;
}
}
catch (Exception ex) {
throw ex;
return 0;
}
finally {
conAppDB.Close();
}
return 0;
}
This code can be best written as follows
private System.Data.SqlTypes.SqlInt64 nextTaskRef()
{
object rawValue;
try
{
string getTaskRef = @"SELECT MAX([taskID_PK]) FROM [teamwiki].[dbo].[task];";
SqlCommand getTaskRefSQL = new SqlCommand(getTaskRef, conAppDB);
rawValue = getTaskRefSQL.ExecuteScalar();
}
catch (Exception ex) {
throw ex;
}
finally {
conAppDB.Close();
}
if (rawValue != DBNull.Value) {
return (System.Data.SqlTypes.SqlInt64) rawValue;
}
return 0;
}
Happy Coding,
Karmendra
|
|
|
|
|
Thanks, I was having issues with explicit casting the value to SqlInt64:
return (System.Data.SqlTypes.SqlInt64) rawValue;
Jon
|
|
|
|
|
To give a little bit different approach.
Now you're using a meaningful value as a primary key. Could you consider using surrogate key[^] instead.
In order to do that you would define your primary key column as IDENTITY . After that SQL Server takes care of the numbering so one less thing to worry about. Especially if you use foreign keys between tables, I really encourage you to consider this option.
|
|
|
|
|
The finally block will always be executed so your comment against the if statement is wrong. The purpose of the finally block is to ensure that code is run no matter how the try block is exited even if an exception is thrown.
Your comment in the catch block is spot on - the return is unreachable and completely redundant.
However your solution is good.
An alternative would be to add an else statement that returns 0 after the if.
I.e do this
if (rawValue != DBNull.Value) {
return (System.Data.SqlTypes.SqlInt64) rawValue;
}
else {
return 0;
}
Regards
David
|
|
|
|
|
Hi all,
I have to validate a textbox for giving ratio to a value, i.e. i 've two boxes in which i enter ratio. i have to validate these box that values shud be integer & between range 0-100 only
can any body have the code or any help
|
|
|
|
|