|
If you have the source code, you edit one of the classes (or add a new one) and create a method that does this. This is pretty basic stuff.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Member 590310 wrote: i want to add a method to dll which could return a value.hw can i do that?
In DLL:-
namespace my
{
class ABC
{
private int _intValue=10;
public int ReturnValue()
{
return _intValue;
}
}
In your Executable:- First add reference of DLL , then following code will
suffix
my.ABC oABC = new my.ABC();
Console.WriteLine(oABC.ReturnValue());
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
0k thats fine but i dnt want to add dll reference instead i want to load it on runtime like that.
Assembly a = Assembly.LoadFrom(Application.StartupPath + @"\"+strLangName+".dll");
then hw to call its function?
|
|
|
|
|
|
as calla suggested, bellow is the simple way to do it.
string asseblyId = @"NewAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=531174506d670b8c";
ObjectHandle Handle=Activator.CreateInstance(asseblyId, "NewAssembly.ClassName");
object obj = Handle.Unwrap();
object[] parm = new object[] { 2, 4 };
object[] parm1 = new object[] { "Hello" };
Type t = obj.GetType();
t.InvokeMember("DoIt", BindingFlags.Public | BindingFlags.NonPublic|BindingFlags.InvokeMethod|BindingFlags.Instance ,null , obj ,null);
int i=(int)t.InvokeMember("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj,parm);
Console.WriteLine(i);
t.InvokeMember("Name", BindingFlags.Public | BindingFlags.SetProperty|BindingFlags.Instance , null, obj, parm1);
string strname=(string)t.InvokeMember("Name", BindingFlags.Public|BindingFlags.GetProperty| BindingFlags.Instance, null, obj, null);
Console.WriteLine(strname);
and bellow is the methods for class in dll
public class Employee
{
public string _name;
public int _age;
public string _address;
public string _id;
public Employee()
{
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
public int Age
{
get
{
return this._age;
}
set
{
this._age = value;
}
}
public string ID
{
get
{
return this._id;
}
set {
this._id = value;
}
}
public int Add(int i, int j)
{
return i + j;
}
public void DoIt()
{
Console.WriteLine("Welcome from DO it");
}
public override string ToString()
{
return String.Format("ID :{0} Name:={1} Age{2}", this._id, this._name, this._age);
}
}
hope this helps!
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
Good to see developer from Afghanistan... welcome brother!
|
|
|
|
|
How to get the List of groups in active directory and check its Enabled or not
|
|
|
|
|
What do you mean by enabled? Do you want to know if a user is a member of a particular group? Or do you want to know if the group exists in AD? If you want to check membership, have a look at the WindowsPrincipal[^] class. If you want more general control of AD, then you should use Active Directory Components[^]
|
|
|
|
|
Hi,
I need help please on the following issue. I have got a combo box on the form and I am trying to show the member of the access data field collumn called 'user'. When i run the program, I can't see any thing in the combobox. I don't know what I am doing wrong here. Here are my codes.
Thanks in advance.
<br />
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)<br />
{<br />
OleDbConnection conn = new OleDbConnection((@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\riad\c#\test\test.mdb"));<br />
conn.Open();<br />
OleDbCommand cmd = new OleDbCommand();<br />
cmd.Connection = conn;<br />
OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM test",conn);<br />
DataSet ds = new DataSet();<br />
adp.Fill(ds);<br />
comboBox1.DataSource = ds.Tables[0].DefaultView;<br />
comboBox1.DisplayMember = "user";<br />
comboBox1.ValueMember = "user";<br />
<br />
cmd.ExecuteNonQuery();<br />
<br />
conn.Close();<br />
<br />
<br />
}<br />
|
|
|
|
|
Hi,
welcome to CodeProject.
You have a chicken-and-egg problem here: you are populating the ComboBox inside its SelectedIndexChanged handler, however without any content, the index can't change. You probably want to populate the CB only once, maybe in the Form.Load handler; and not in the SelectedIndexChanged handler: if it takes 1 second to execute, the user would have a hard time scrolling through the CB list.
PS: next time please choose an informative subject line, and put your code snippet in PRE tags (not CODE tags) to get better readability.
Cheers
|
|
|
|
|
I dont want to have to type this out every time I want to connect to the database. Can I put this in a class or something?
What would be the best way to do this so I can call myConnection from anywhere inside my application?
SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=password;server=serverurl;" +
"Trusted_Connection=yes;" +
"database=database; " +
"connection timeout=30");
Thanks in advanced as I am pretty much learning.
|
|
|
|
|
Write a class that encapsulates it, like my DatabaseAccessor[^].
Then create a Data Access Layer that your application can use.
Furthermore: Your application shouldn't know what database is in use. You should not find yourself instantiating Connections, Commands, etc. in your application at all.
modified on Sunday, May 2, 2010 4:04 PM
|
|
|
|
|
A fairly common way to do this is to store the connection string in a static class. Here's a sample that might give you an idea of what I mean:
public static class Settings
{
private static string _connectionString = string.Empty;
public static string ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
_connectionString = ConfigurationManager.ConnectionStrings["connection_string_stored_in_config_file"].COnnectionString;
return _connectionString;
}
}
} This supposes that you put the connection string in a configuration file rather than hard coding in your application. Then, in any place you want to call it, you would use it like:
using (SqlConnection myConnection = new SqlConnection(Settings.ConnectionString))
{
} BTW - you should always look at trying to wrap disposable classes in a using block; hence the wrap around the SqlConnection .
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Monday, May 3, 2010 8:33 AM
|
|
|
|
|
And when you have several different connection strings? And various database engines to use? And desire database agnosticism?
|
|
|
|
|
I wouldn't be tackling this as a beginner. I'd also use something like nhibernate.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: wouldn't be tackling this as a beginner
Why not? I did. Well, an ADO.net beginner anyway, with ten years experience with embedded SQL and scars from ODBC.
|
|
|
|
|
Yeah, but you've got the yellow diamond.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
|
Ok so this is my class called settings.cs I dont know where I am going wrong but it does not like the code it gives me the following errors:
Error 1 Invalid token '{' in class, struct, or interface member declaration
Error 2 Invalid token '{' in class, struct, or interface member declaration
Error 3 Invalid token '.' in class, struct, or interface member declaration
there is a total of 15 errors any help would be greatly appreciated!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApplication
{
public static class Settings
{
private static string _connectionString = string.Empty;
public static ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
_connectionString = ConnectionManager.ConnectionStrings["connection_string_stored_in_config_file"];
return _connectionString;
}
}
}
}
|
|
|
|
|
Terence van Schalkwyk wrote: public static ConnectionString
should read
public static string ConnectionString
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
thanks that fixed it!
Last question:
_connectionString = ConnectionManager.ConnectionStrings["connection_string_stored_in_config_file"];
where do you specify ConnectionManager?
Thanks again!
|
|
|
|
|
Sorry, that should have read ConfigurationManager.ConnectionStrings; add a reference to System.Configuration . This is what happens when I write code directly on the Code Project web site. I'll edit the original code to show what it should be.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I am using visual studio 2008....
Thanks for that!!! I hope this is the last question!!! Its still not liking ConfigurationManager. Does not exist in current context. Am I supposed to initialise this somewhere?
Also in this part of the code "connection_string_stored_in_config_file" is this the section in the config file?
Here is how my code looks:
using System.Configuration;
namespace testapp
{
public static class Settings
{
private static string _connectionString = string.Empty;
public static string ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
_connectionString = ConfigurationManager.ConnectionStrings["connection_string_stored_in_config_file"];
return _connectionString;
}
}
}
}
Here is my App.config file. Is this correct so far and how do I put my connection string in?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnection" connectionString=""></add>
</connectionStrings>
</configuration>
modified on Monday, May 3, 2010 7:00 AM
|
|
|
|
|
Terence van Schalkwyk wrote: Its still not liking ConfigurationManager
You need to add a reference to System.Configuration. It's not included in your list of references - right click on your reference, select Add reference and choose it from the .NET tab.
Terence van Schalkwyk wrote: _connectionString = ConfigurationManager.ConnectionStrings["connection_string_stored_in_config_file"]
Now that we know what you are going to call your connection string, change this line to:
_connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; The connection string you add in here largely depends on the database type and connection details. There is a handy online resource that shows many different types of connection string - it's available here[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
hi
i want write one program in c#
i have very images in this program and i want user can not copy my pictures load in my project ,
with print screen button or other softwares like snagit
please help !!!!
thanks
|
|
|
|