|
hi ,
i wrote this code so that i can understand delegates...
code works fine ....good
but when i changed the code a bit ie when i wrote the main function inside another class if i need to call the method using delegates then i have to call like
""mydelegate obj = new mydelegate(class.dog);""
ORIGINAL CODE::
class Program
{
public static void dele()
{
Console.WriteLine("the delegate will call this function");
Console.ReadLine();
}
static void Main(string[] args)
{
mydelegate obj = new mydelegate(dog);
obj();
}
}
}
ALETRED CODE WITH TWO CLASSES::
class Program
{
public static void dele()
{
Console.WriteLine("the delegate will call this function");
Console.ReadLine();
}
class Program2
{
public static void anotherdele()
{
static void Main(string[] args)
{
mydelegate obj = new mydelegate(Program.dog);
obj();
}
}
}
SO My doubt here is i thought using delegates we need not know the name of the class but here if the main is enclosed in another class i need to use the class name....
any help is welcome .......
thanks
C#
|
|
|
|
|
IF you are referencing a static method the class must be known, if you are using an instance method, the instance should be known (and definately NOT null).
xacc.ide
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."
|
|
|
|
|
Hi ,
I've read that objects are reference types and also that 'ref' keyword is used to pass the reference of a value ...
Now plz look into the following examples...
<br />
namespace ExampleConsoleApp<br />
{<br />
public class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
Manager emp = new Manager();<br />
<br />
changevalue(emp);<br />
<br />
Console.Out.WriteLine(" " + emp.name);<br />
<br />
Console.Read();<br />
}<br />
<br />
<br />
public static void changevalue(Manager man)<br />
{<br />
<br />
man.name = "Grade1Employee";<br />
<br />
}<br />
}<br />
<br />
<br />
public class Manager<br />
{<br />
public string name = string.Empty;<br />
<br />
}<br />
}<br />
The program gives the output as "Grade1Employee"<br />
....<br />
Example2 <br />
<br />
namespace ExampleConsoleApp<br />
{<br />
public class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
<br />
Manager emp = new Manager();<br />
emp.Name="Grade1Employee";<br />
changevalue(emp);<br />
<br />
Console.Out.WriteLine(" " + emp.name);<br />
<br />
Console.Read();<br />
}<br />
<br />
<br />
<br />
<br />
<br />
public static void changevalue(Manager man)<br />
{<br />
<br />
Manager emp2 = new Manager();<br />
emp2.name = "TopLevelEmployee";<br />
<br />
man = emp2;<br />
<br />
<br />
}<br />
}<br />
<br />
<br />
<br />
<br />
public class Manager<br />
{<br />
public string name = string.Empty;<br />
<br />
}<br />
}<br />
..The above still gives the output as "Grade1Employee" why is it so? When object is a reference type then the value should be changed right?...when use a ref keyword for the parameter name in the method like "changevalue(ref Manager man)" then the output is
"TopLevelEmployee"...
Why is it behaving differently in two cases..?
Koushik
|
|
|
|
|
Spunky Coder wrote: public static void changevalue(Manager man)
{
Manager emp2 = new Manager();
emp2.name = "TopLevelEmployee";
man = emp2;
}
This is correct. Objects are passed as reference, but that means that instead of copying whole object, reference to object is copied.
changevalue(Manager man) <- copy of "pointer" to instance Manager
{
Manager emp2 = new Manager(); <- local variable
emp2.name = "TopLevelEmployee"; <- you do changes to local variable
man = emp2; <- you assign the copy of pointer to instance to local instance
} <- all is forgotten when method returns</code>
Why is that called passing by reference? If it was passed by value, following code:
changevalue(Manager man) <- copy of instance of Manager
{
man.name = "TopLevelEmployee"; <- you do changes to copy
}
would NOT change name of man outside the method. As you can try, it indeed does change the name, because objects are passed by reference.
If you really want to change the reference to man, use keyword ref. (think about pointer to pointer)
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Hi..
changevalue(Manager man) <- copy of "pointer" to instance Manager
{
Manager emp2 = new Manager(); <- local variable
emp2.name = "TopLevelEmployee"; <- you do changes to local variable
man = emp2; <- you assign the copy of pointer to instance to local instance
} <- all is forgotten when method returns
...Now so what's the difference between passing an int datatype and object when its scope is just limited to the called function ?...
Koushik
|
|
|
|
|
Spunky Coder wrote: ...Now so what's the difference between passing an int datatype and object when its scope is just limited to the called function ?...
No, scope of the reference is limited to called function. Actual instance of object you change via the reference will keep the changes outside that function. Is this what you mean?
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
I'm totally confused... ...i was taught that both in java and c# objects are referenced types so when we do any operations on the object they get reflected to entire object wherever you access it ..but now its behaving differently...
Koushik
|
|
|
|
|
Spunky Coder wrote: i was taught that both in java and c# objects are referenced types so when we do any operations on the object they get reflected to entire object wherever you access it
But you are not doing any operation on object, you are changing the reference itself.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Spunky Coder wrote: The above still gives the output as "Grade1Employee" why is it so?
You're passing a reference to a Manager object to the method. If you change the object via this reference the changes reflect outside the method (first example). If you create a new object and assign the reference to it to the method parameter, then this change does not reflect outside the method (second example), because changes to parameters (values or references) do not reflect outside a method unless you use the ref or out keyword.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
When you take (Manager man) as input, you are actually taking a copy of the pointer to the actual manager. that means it points to the same object, but you cannot change the pointer adress declared in main, to do that you need the ref.. dunno if you get what i mean tho, im not good at explaining ^^
Manager man <- pointer to a manager object
= new Manager() <- returns adress to the new object
fafafa, ringakta icke sådant som bringa ack så naggande högönsklig välmåga å baronens ära.
|
|
|
|
|
Suppose we alter the first example as following...
public static void changevalue(Manager man)
{
man.name = "Grade1Employee";
man = null;
}
still the emp.name in the main function gives the value "Grade1Employee"....Now when we are assigning a value to the member of the object (to the reference copy of the object) , the value is retained and when are setting the reference copy to null the value is not retained....
Koushik
|
|
|
|
|
hello all!
i have a dataGridView that i add a context menu to each row when adding the row:
dataGridViewOrder.Rows.Add(itemRow);
dataGridViewOrder.Rows[dataGridViewOrder.Rows.Count - 2].ContextMenuStrip = contextMenuStripProducts;
on run time, if i left click the row then right click it and choose the menu, every think is ok.
but i have a problem when the currentRow is for exemple the first row, and i right click another row, it appear the menu of the second row but the changes made for the CurrentRow are applyed for the first Row, because it is the CurrentRow.
i was tryed to set the currentRow on mouse down event but a compilation error say that currentRow is read only...
please if some one have a solution or an idea for this problem, tell me about it.
thank u all!
regards.
jimmy.
jim MAA
|
|
|
|
|
You can use the CurrentCell property to set (or get) the currently selected cell.
xacc.ide
The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."
|
|
|
|
|
Hi,
Is there any way i can get a list of items i want added to a ToolStripMenu from SQL.
I have a database which contains records that i enter using the application im developing.
However, i want to be able to use the SELECT DISTINCT SQL command, to retrieve the names of something from a column, and put it into my toolstrip.
Is there any way of doing this?
Id be grateful of anybodys help
|
|
|
|
|
why don't you create the control when the sql load completed?
|
|
|
|
|
|
Create a function call "GenerateToolStripMenu()" or something, when you filled the data to the dataset, fire the function.
e.g.
for (int i = 0; i < database1DataSet.Table1.Rows.Count; i++)
{
menuStrip1.Items.Add(database1DataSet.Table1.Rows[i][%your_column_name_here%].ToString());
}
|
|
|
|
|
Hello everyone,
I am using C# to develop DLL using Visual Studio 2005 and .Net 2.0, and I have no idea of how to make my DLL work with applications on 64-bit platform. Above all, I do not utilize any new features in my DLL of 64-bit. So, I want to check the general rules,
1. For C#, is there a need to make two separate builds (32-bit and 64-bit) according to the application (32-bit or 64-bit) which uses the DLL? i.e. provide 64-bit application my 64-bit C# DLL, and provide 32-bit application runs on 64-bit platform my 32-bit DLL?
2. If we have to make two separate builds, how to do it in Visual Studio 2005? I only find a setting names for Any CPU in project --> properties.
thanks in advance,
George
|
|
|
|
|
I had the same questions, and got some great advice in the .Net forum which was:
1. Yes you can choose to target x86 and x64 processors individually. Doing so requires that you have two builds. Alternatively, you can choose the Any CPU option, which will run on both.
2. From: http://blogs.msdn.com/tolong/archive/2007/03/21/use-visual-studio-to-build-64-bit-application.aspx[^]
To configure your projects target on 64-bit platform, you can utilize the configuration manager on visual studio.
Here are the steps:
1. Go to Build menu
2. Select Configuration Manager
3. In the Active solution platform list, select the 64-bit platform for the solution to target.
4. If you do not see the option, select New
5. In the New Solution Platform dialog box, select the 64-bit platform for the solution to target.
6. Now the build of the project will be optimized for 64-bit platforms.
Me: Can you see the "up" arrow?
User:Errr...ummm....no.
Me: Can you see an arrow that points upwards?
User: Oh yes, I see it now!
-Excerpt from a support call taken by me, 08/31/2007
|
|
|
|
|
hi,
in c language we use \n for nextline
but what is the method of nextline in c#.
textBox1.Text = "A";
textBox1.Text += "B";
i want
A
B
not AB
I AM WORKING ON "PLOTTER ROBOT"(FYP).
|
|
|
|
|
Erhm, you can still use the \n and \r characters, but what would be better is :
<br />
Environment.NewLine<br />
|
|
|
|
|
Use the Environment.NewLine property. It gives you the proper character combination depending on the platform that the code is running on.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
but how i can use Environment.NewLine
what would be updation on following code
textBox1.Text += "A";<br />
textBox1.Text += "B";
I AM WORKING ON "PLOTTER ROBOT"(FYP).
|
|
|
|
|
textBox1.Text += Environment.NewLine;<br />
textBox1.Text += "A";
Is it right?
I AM WORKING ON "PLOTTER ROBOT"(FYP).
|
|
|
|
|
zeeShan anSari wrote: Is it right?
Did you try it? Did it work?
|
|
|
|