|
hi.
How can I return IEnumerable<t> az a method parameter.
In fact I want to return a table records. but I must return as IEnumerable<t>;
How can I fill IEnumerable and return it?
|
|
|
|
|
Hi,
you have to write a class that will implement the IEnumerable interface. Within the implementation you can move through your records.
Have a look here:
http://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28VS.80%29.aspx[^]
By the way, if you are using the System.Data.Table then the Rows-Property offers a GetEnumerator() method which will return an enumerator for the records within the table.
Regards
Sebastian
|
|
|
|
|
a world of thanks.
I 'll try to do it
|
|
|
|
|
Other than that, you can also use any class implemented from IEnumerable like Array, ArrayList, List etc... to return IEnumerable object.
|
|
|
|
|
Use a yield[^] keyword to return records as they go.
IEnumerable<int> GetNumbers()
{
for (int i = 0; i < 100; i++)
yield return i;
} Now you can do:
foreach (int number in GetNumbers())
Console.WriteLine(number);
Greetings - Jacek
|
|
|
|
|
I am getting an error message like this 'Object reference not set to an instance of an object' when I try to invoke an assembly method dynamically. Please refer to the attached code snippets.
I need forum's help to solve this problem.
The main code snippet.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using myClassLibrary;
namespace DynamicallyLoadAssembly
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Assembly assembly;
MethodInfo method;
Type type;
string sPath = @"F:\VS2005 Projects\myClassLibrary\myClassLibrary\bin\Debug\myClassLibrary.dll";
int val1 = 7;
int val2 = 5;
try
{
assembly = Assembly.LoadFile(sPath);
type = assembly.GetType("myClassLibrary.myClass");
Type[] p = new Type[3];
p[0] = Type.GetType("System.Int32");
p[1] = Type.GetType("System.Int32");
p[2] = Type.GetType("System.Int32&");
method = type.GetMethod("Method1", p);
Object[] ps = new Object[3] { val1, val2, 0 };
method.Invoke(null, ps);
MessageBox.Show("ps = " + ps[2].ToString());
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
Assembly assembly;
MethodInfo method;
Type type;
string sPath = @"F:\VS2005 Projects\myClassLibrary\myClassLibrary\bin\Debug\myClassLibrary.dll";
int val2 = 5;
try
{
assembly = Assembly.LoadFile(sPath);
type = assembly.GetType("myClassLibrary.myClass");
Type[] p = new Type[3];
p[0] = Type.GetType("System.Enum");
p[1] = Type.GetType("System.Int32");
p[2] = Type.GetType("System.Int32&");
method = type.GetMethod("Method1", p);
Object[] ps = new Object[3] { myClass.myEnum.One, val2, 0 };
// The error 'Object reference not set to an instance of an object' occurs when invoking this method.
method.Invoke(null, ps);
MessageBox.Show("ps = " + ps[2].ToString());
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
}
}
The library code snippet.
using System;
using System.Collections.Generic;
using System.Text;
namespace myClassLibrary
{
public class myClass
{
public enum myEnum : int
{
One,
Two,
Three
};
public static void Method1(int a, int b, out int c)
{
c = a + b;
}
public static void Method2(myEnum a, int b, out int c)
{
c = 0;
switch (a)
{
case myEnum.One:
c = 1 + b;
break;
case myEnum.Two:
c = 2 + b;
break;
case myEnum.Three:
c = 3 + b;
break;
}
}
}
}
Thanks
Chris
|
|
|
|
|
Hi,
you are passing "null" as the first parameter to method.Invoke. But the first parameter is the required instance that the method will be executed on. The instance must be an object of the type you loaded dynamically. So before calling invoke you have to create an object from the type "myClassLibrary.myClass". This can be done by reflection also, using the constructor.
Have a look here:
http://msdn.microsoft.com/en-us/library/system.type.getconstructor.aspx[^]
Regards
Sebastian
|
|
|
|
|
Hi Sebastian,
Thanks for the answer, but I still do not understand how to fix.
I am not a pro programmer. Please explain with example...
thanks
|
|
|
|
|
Hi,
rewrite the invocation like this:
method = type.GetMethod("Method1", p);
object myObject = Activator.CreateInstance(type);
Object[] ps = new Object[3] { val1, val2, 0 };
method.Invoke(myObject, ps);
This will create an instance named myObject and invokes the method "method1" on that instance.
Regards
Sebastian
|
|
|
|
|
Thanks Sebastian for your quick response. Anyhow I managed to solve this problem by changing a single line at the button2_Click method, as shown below.
Previous code:
p[0] = Type.GetType("System.Enum");
Now changed to:
p[0] = assembly.GetType("myClassLibrary.myClass+myEnum");
Thanks
Chris
|
|
|
|
|
You try to invoke a non-static method on a null object. Create an instance of the class the Type represents by using Activator.CreateInstance, and pass that instead of null.
|
|
|
|
|
I already have a code for my linked list. How do I connect notepad to c# to make it as a simple database of my program?
|
|
|
|
|
Hi,
do you mean notepad? The windows notepad? This is just an application to edit files. Do you want to store your data within a file? Then think about using a format like csv. How is the linked list connected to this? What do you want to store exactly?
Regards
Sebastian
|
|
|
|
|
Yessir. The windows notepad. I'll convert it .txt to xml so i can use it to store some data. So when I close my program, if i hit the load button, the data stored in the notepad would come up in the gridbox.
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Xml;
namespace Machine_Problem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private LinkedList<StudentInfo> Stud = new LinkedList<StudentInfo>();
private void Form1_Load(object sender, EventArgs e)
{
}
private void AddEntry_Click(object sender, EventArgs e)
{
StudentInfo Temp = new StudentInfo();
Temp.ID = int.Parse(textBox2.Text);
Temp.LastName = textBox1.Text;
Temp.FirstName = textBox4.Text;
Temp.EMail = textBox3.Text;
Temp.CInfo = long.Parse(textBox5.Text);
Stud.AddLast(Temp);
MessageBox.Show(" Data entry succesful! ");
}
private void Clear_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
private void button7_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(" C:\Users\dbrylle\documents\db.xml ");
for (int i = 0; i < Stud.Count; i++)
{
StudentInfo temp;
temp = Stud.ElementAt<StudentInfo>(i);
string data;
data = temp.ID.ToString() + " -- ";
data += temp.LastName + ", ";
data += temp.FirstName + " -- ";
data += temp.CInfo + " - ";
data += temp.EMail;
}
}
that's my code for now. i'm going to use gridbox for this program but I can't seem to make notepad save the data that my program will accept.
Regards,
Dexter
|
|
|
|
|
Hello Everyone!!
I am really in some serious problem, can you ppl please help me!
Is there any PKCS11 wrapper in C# or .Net so that it can be used from C# applciation.
What I mean is that, there are typical function in PKCS11 (cryptoki.dll) like C_Login, C_Encrypt, or C_GetSlotInfo,
I am trying to write myself a Wrapper for PKCS11 but it seems the task would be eronomous, and I am already facing many problems.... I could not understand how to write cryptoki definied variables, structures and classes in C#.
Please any help would be appreciated..
Thanks in Advanced
Regards
|
|
|
|
|
|
hi everyone!
i have the uri object with following url:
http://localhost/res/index.php?time_id=1&a=search&search=yes&course_id=1
i want to edit this object, ex edit the time_id variable from '1' to '2'
and do this with other vaiables in this url
would u tell me how to do this and share example codes ?
thanks in advance.
|
|
|
|
|
That is only a string.
I use Replace for it :
string s = "..." // <- this is your uri address
s = s.Replace("time_id=1", "time_id=2");
and so on...
|
|
|
|
|
Thank you!
but the "time_id" available just is a example.In my situation,i just only edit the value of each available and neednt know what is available for a URL.
|
|
|
|
|
|
I,m looking for a code in Visual Studio to active keyboard's key instead of pressing any button in windows form. For example, when I want to Exit from windows form instead of pushing Exit button, I enter the Escape key. Hopefuly that was enough clear!!!
|
|
|
|
|
Set the form's "Cancel Button" property to the button that closes the form.
Then, when you press Escape, it will activate the code behind that button.
|
|
|
|
|
|
How can i make online calls to mobile phones? (That is, how can i write a program to make phone calls through the internt to my mobile phone)
|
|
|
|