473,625 Members | 3,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inherited object as ref

Hey,

Can we send an inherited object into a function which requires its
base class as REF?

class y
class x : y
func DoSomething(ref y)
DoSomething(ref x);

gives an error. If I send object without Ref, then modifications on
object get lost when DoSomething func ends.

Can someone describe this for me? Thanks.

Apr 18 '07 #1
6 1793
No to the first, since otherwise you could get the following:

void DoSomething(ref Y y) {
y = new Y();
}

X x = new X();
DoSomething(ref x);
// WTF! x isn't null, and doesn't hold an x (or subclass)... major
error!

However! I suspect you don't need "ref" here; as long as your object
is a class and not a struct, changes made to *properties* of the
object should persist after the call. But if you reassign the variable
entirely, then no: this won't be carried back over.

If you must use ref, then you'll need an appropriately typed variable:
Y y = x;
DoSomething(ref y);
x = (X) y; // pray that DoSomething returned an appropriate object

Alternatively, generics may be useful:

void DoSomething<T>( ref T t) where T : Y, new() {
t = new T(); // for some reason
}

DoSomething(ref x); // <Xis inferred from variable type
Hope this helps,

Marc
Apr 18 '07 #2
Sorry everyone, I had a silly mistake when applying this onto source.

So, it's solved now.

On 18 Nisan, 13:33, bark...@gmail.c om wrote:
Hey,

Can we send an inherited object into a function which requires its
base class as REF?

class y
class x : y
func DoSomething(ref y)
DoSomething(ref x);

gives an error. If I send object without Ref, then modifications on
object get lost when DoSomething func ends.

Can someone describe this for me? Thanks.

Apr 18 '07 #3

Thanks Marc, it was a silly error or my source so I had solved before
you wrote such detailed reply for me. :)

On 18 Nisan, 13:49, "Marc Gravell" <marc.grav...@g mail.comwrote:
No to the first, since otherwise you could get the following:

void DoSomething(ref Y y) {
y = new Y();

}

X x = new X();
DoSomething(ref x);
// WTF! x isn't null, and doesn't hold an x (or subclass)... major
error!

However! I suspect you don't need "ref" here; as long as your object
is a class and not a struct, changes made to *properties* of the
object should persist after the call. But if you reassign the variable
entirely, then no: this won't be carried back over.

If you must use ref, then you'll need an appropriately typed variable:
Y y = x;
DoSomething(ref y);
x = (X) y; // pray that DoSomething returned an appropriate object

Alternatively, generics may be useful:

void DoSomething<T>( ref T t) where T : Y, new() {
t = new T(); // for some reason

}

DoSomething(ref x); // <Xis inferred from variable type

Hope this helps,

Marc
Apr 18 '07 #4
Can we send an inherited object into a function which requires its
base class as REF?
Yes, as long as you initialize the object passed before passing it. I'm sure
the exception told you that.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<ba*****@gmail. comwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
Hey,

Can we send an inherited object into a function which requires its
base class as REF?

class y
class x : y
func DoSomething(ref y)
DoSomething(ref x);

gives an error. If I send object without Ref, then modifications on
object get lost when DoSomething func ends.

Can someone describe this for me? Thanks.

Apr 18 '07 #5
On Apr 18, 12:16 pm, "Kevin Spencer" <unclechut...@n othinks.com>
wrote:
Can we send an inherited object into a function which requires its
base class as REF?

Yes, as long as you initialize the object passed before passing it. I'm sure
the exception told you that.
Well, you can use a variable of the appropriate parameter type which
happens to hold a reference to an instance of the derived class, but
you *can't* use a variable which is of a derived type itself. So for
this method signature:

void MethodCall (ref object o)

This is valid:

object o = "foo";
MethodCall (ref o);

But this isn't:

string s = "foo";
MethodCall (ref s);

Jon

Apr 18 '07 #6
Ah yes. Thanks for clearing that bit up.

--

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** *************@l 77g2000hsb.goog legroups.com...
On Apr 18, 12:16 pm, "Kevin Spencer" <unclechut...@n othinks.com>
wrote:
Can we send an inherited object into a function which requires its
base class as REF?

Yes, as long as you initialize the object passed before passing it. I'm
sure
the exception told you that.

Well, you can use a variable of the appropriate parameter type which
happens to hold a reference to an instance of the derived class, but
you *can't* use a variable which is of a derived type itself. So for
this method signature:

void MethodCall (ref object o)

This is valid:

object o = "foo";
MethodCall (ref o);

But this isn't:

string s = "foo";
MethodCall (ref s);

Jon

Apr 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2150
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now, when recomposing these objects, I first read the base class data, and can create a base object. When I find the inherited class marker, can I call have the inherited class use the prior base object to population the inherited class's base...
13
2222
by: Lorne Smith | last post by:
Hi, First, sorry for the crosspost, but it seemed appropriate... :) I've come accross what I consider to be a bug, but I don't know if it's already known or not. (VS .Net 2003 Pro - VB.Net) Whilst playing with inherited forms, I created a simple base form containing a single button. I set this buttons' Click event to be public overridable and put "me.Close" in the code.
2
1199
by: chetsjunk | last post by:
I am having problems with an inherited class not handling an event for the class it is inherited from. But it's not constistent, so I'm guessing the problem is elsewhere in my code. I created the following small program to test my problem, and this WORKS FINE: ----- Class BaseClass Public Event SaidSomething(ByVal WhatWasSaid As String) Public Sub Talk(ByVal WhatToSay As String) MsgBox(WhatToSay)
10
5098
by: John M. Gabriele | last post by:
The following short program fails: ----------------------- code ------------------------ #!/usr/bin/python class Parent( object ): def __init__( self ): self.x = 9 print "Inside Parent.__init__()"
0
1746
by: porter_wss | last post by:
I am a bit confused on serializing objects and inherited objects. What I am trying to accomplish is creating a base object and an inherited object with additional properties. The sender of these objects will have always have in memory the inherited versions (all properties). However, when sending messages, the sender may only send a scaled back version (base object) or the full version (inherited object) to the remote receiver depending...
4
1939
by: asad.naeem | last post by:
hi to all this is the problem about inheritence. I have designed a form with some essential controls which are required for every form which will inherited from it. for example i have Button1 on parent form and this button is visible to me on inherited form. The problem is: I have written a click event of the button1 on both of the forms. tell me the way if i click the button on inherited form only parents' click event will be called and...
14
2625
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
19
2225
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
12
2160
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
13
1717
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came up with this: //base private object constructor function priv(){ this.a = 1; this.b = 2;
0
8256
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.