|
Are you trying to make m_i into a pointer to i? If so, then I understand what you're trying to do.
In explanation, the code is taking the value if i and assigning it to m_i, then taking the value of 10 and assigning it to m_i. The attempt to make m_i into a pointer to i is where your expectations have met the problem.
In C#, all class objects are handled "by reference". In other words, every time you to a "new Test()", you are getting a pointer to the real object. This means that whenever you put "myvar = something", you are setting pointers -- NOT copying objects. (Copy constructors only come into play when you explicitly use the "new" keyword.)
That said, you may already have a better technique to achieve your goal. However, you could do something like this:
public class Storage
{
int value;
}
public class Test
{
public Test(Storage val)
{
m_val = val;
m_val.value = 10;
}
private Storage m_val;
}
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Ahhhhhhhh... I did not know that.
So, just to make sure that I understand...
Everytime that I pass parameters to functions etc., I am always passing a pointer to the object and not invoking a full copy of it.
If this is indeed the case, then I do not need the ref keyword.
Thanks,
Pankaj
Without struggle, there is no progress
|
|
|
|
|
As Nick explained, though, value types (such as int, long, byte, enum, structs, etc.) are just that - value types. You pass a value to a method (not function - methods are "functions" that belong to an entity such as a class, interface, or struct). In almost all cases, this is just fine. When you pass a reference (everything else), a reference to the original object is stored in the stack (internal to the CLR). Using ref and out with value types is common, however, when P/Invoking native functions.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
With .NET you have value and reference types. Classes are always reference types. You don't need to declair int m_i as a reference type because it is a member of an object of type Test, so you are using it via an instance. Just make sure when you use methods inside your class that change that value that you mark the parameter as ref . Does that help.
- Nick Parker My Blog
|
|
|
|
|
Hi,
Can you show a small example, if you do not mind.
The things is I want to change the object in the calling function from this instance. I actually want a reference because I do not want to call the copy constructor on a large object.
I miss C++
Thanks,
Pankaj
Without struggle, there is no progress
|
|
|
|
|
Here is a quick example:
public class Test
{
public Test(){}
public int MyNumber
{
get{return m_i;}
set{m_i = value;}
}
public int m_i;
}
public class SomeOtherClass
{
public SomeOtherClass(){}
public void ChangeValue(ref int i)
{
i = 5;
}
}
Test t = new Test();
SomeOtherClass soc = new SomeOtherClass();
t.MyNumber = 8;
soc.ChangeValue(ref t.m_i);
Console.Write(t.MyNumber);
- Nick Parker My Blog
|
|
|
|
|
What are the differences between them?
How do I make decision on choosing on of them in my project?
(ASP.NET (must be thin client)
-> {?? WebSvc/Remoting}
-> Data Access Layer(ADO.NET)
-> DB)
Is there any link on MSDN or anywhere on the web which explains in detail.
TIA
Promise only what you can do. And then deliver more than what you promised. This signature was created by "Code Project Quoter".
|
|
|
|
|
Web Services by nature are one-way communications since they use HTTP. .NET Remoting allows you to use TCP or HTTP channels, so communications can be two-way (i.e., the server can also talk to the client). .NET Remoting also allows you to chain sinks together (know as Aspect-Oriented Programming, or AOP) that allows you to work on the messages and add or change things as the formatted messages go through the sink chain.
.NET Remoting also allows you to use a binary formatter as opposed to a SOAP formatter (which Web Services use) which is much faster when serializing object graphs and uses less bandwidth to transmit.
For a comparison of ASP.NET/Web Services vs. .NET Remoting, see http://msdn.microsoft.com/library/en-us/cpguide/html/cpconchoosingcommunicationoptionsinnet.asp[^]. This also contains links to more information regarding each of the technologies.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I was hoping that you would reply.
Thanks for clarifying me.
Meanwhile I found a link[^] at MSDN but that's old link, that too related VB.
Promise only what you can do. And then deliver more than what you promised. This signature was created by "Code Project Quoter".
|
|
|
|
|
|
Hi everyone,
I'm using VS.NET 2003 to develop WIndows Forms applications - my question is, when I add a user control to a control library project that is part of my solution, it automatically appears in the "My User Controls" section of the VS toolbox. Is it possible to have VS automatically recognize my custom controls as well or not?
Thanks
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
In my experience that happens automatically when you compile your control.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
For me it happens only for user controls upon compilation, but not for custom controls Is it possible to configure it somewhere?
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
I didn't understand that there was a difference between the two. What do you mean by "custom control"?
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Sorry for the confusion and thanks for the response, when you choose to "Add User Control" to the project, and select the "Custom Control" template, the new control is derived from System.Windows.Forms.Control , whereas when you choose the "User Control" template (this one is preset by default), the new control is derived from System.Windows.Forms.UserControl .
The Heath's response below provides a detailed answer to my question and explains why only the controls derived from UserControl appear in the toolbox.
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
Ah, yes. Basically, if you simply want a new control to use on your forms, inherit from UserControl. If you want a control that is not designable, then inherit directly from Control. (There are other technicalities, but the intended goal of the control is what matters most.)
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Thank you for your response. But when I want to create something like an owner drawn button, I guess the best way would be to derive from System.Windows.Forms.Button to inherit most of the button functionality, which will make my new button control not designable. Is this correct, or is there a better approach?
Thanks again,
Rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
Since Button is already designable, I believe that a class inheritting from Button will be as well. Go ahead and try it if you haven't already. (Just add a new UserControl to your project, then change "UserControl" to "Button". See what happens.)
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Radoslav Bielik wrote:
Is it possible to have VS automatically recognize my custom controls as well or not?
Specifically, what do you mean?
- Nick Parker My Blog
|
|
|
|
|
The documentation states that this behavior only works on controls derived form System.Windows.Forms.UserControl .
If you want to create a custom designer, however, you can have other controls show up for designed components (that use that designer) through various means. You can, for instance, request the IToolboxService in your Initialize override in your designer and add ToolboxItem s and categories. You can also do this with your control without using a designer by override the Site property. If you look at the documentation for the System.Drawing.Design.ToolboxItem class in the .NET Framework SDK, you can see an example of this latter method.
There's many other ways to do something like this, too. I'd recommend you look over the System.ComponentModel , System.ComponentModel.Design , System.Drawing.Design , and System.Windows.Forms.Design namespaces. There's a lot of great classes and interfaces in there you can use. There's also topics in the .NET Framework SDK that talk about designers, editors, and other ways to extend design-time development for your controls.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thank you for the detailed information and references, I will check this out.
I have just one more question: what is the "best practice" when creating my own control? I guess that when I want to create something like owner drawn button it is the best to derive from System.Windows.Forms.Button , but when I want to create some simple brand new control (like an etched line) should I derive from System.Windows.Forms.Control or from System.Windows.Forms.UserControl ? What are the differences?
Thanks again,
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
It depends. Deriving from Control is good when you want to encapsulate a native control or create a custom control that is a single entity (probably not the best choice of words, but let me explain...). A Button , for example, is just a button (encapsulating a Button common contorl). A ListView encapsulates a List-View common control. While the ListView has a column header, a scroll bar, etc., it still is a single control.
UserControl s, however, encapsulate functionality provided by many controls. It also derivces from ContainerControl which derives from ScrollableControl . Thus, it is a container of other controls that can, if configured, scroll when the controls it contains are hidden due to the size of their container (i.e., the UserControl -derivative).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks for a very good explanation! Sometimes I wonder how do you manage to provide such a great help to all the people in the message boards - your help is priceless! Thanks again
Rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
Actually, 2!
1- If we use InternetExplorer object instead of AxWebBrowser object, does it fetch pages any faster because it's not required to fetch the images on the HTML pages too?
2- Is there a way to set the properties of the AxWebBrowser used in the project without affecting the global settings? For example: I don't want the browser in my project to display images, or for instance, I want it to be offline, but not the other IEs in Windows. Possible?
Thank you.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
- Internet Explorer uses the WebBrowser object, so there really is no difference. The InternetExplorer automation server is just an out-of-process container for the WebBrowser control (which hosts and provides services to MSHTML). So no, it won't fetch pages any faster.
- Settings for the WebBrowser control (and many affect MSHTML in general) are global. Only a few things like printer settings can you override on a per-instance basis. You can use the hosting interfaces, however, to control a few aspects of the WebBrowser control, such as the context menus, URL resolution when a link is clicked, and several other things. See Using MSHTML Advanced Hosting Interfaces[^] for more information.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|