473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Reference on value types ?

Hi,

I have a question dealing to value and reference types, boxing and unboxing.
I'm coming from the C++ world so I don't see how to do the following
feature:

I have several primitive types as members of a class. I would like to pass a
reference to these variables to somewhere else in the code and to be able to
modify the original variables from there.
So basically, how to store in a class C2 a reference to a primitive type of
another class C1 to be able to modify the primitive value from C2 ?

I understood the "ref" keyword but I can't use it because:
1. I can't store the reference. I can only use it in the called method.
2. I can't send a ref double and receive it as a ref object (because in the
receiver I want to be generic and store a reference on any value type).

Thanks a lot

Herve
Nov 16 '05 #1
3 1464
Herve Bocuse <gd******@gmail.com> wrote:
I have a question dealing to value and reference types, boxing and unboxing.
I'm coming from the C++ world so I don't see how to do the following
feature:

I have several primitive types as members of a class. I would like to pass a
reference to these variables to somewhere else in the code and to be able to
modify the original variables from there.
So basically, how to store in a class C2 a reference to a primitive type of
another class C1 to be able to modify the primitive value from C2 ?

I understood the "ref" keyword but I can't use it because:
1. I can't store the reference. I can only use it in the called method.
2. I can't send a ref double and receive it as a ref object (because in the
receiver I want to be generic and store a reference on any value type).


You can't, basically. Now, can you tell us more about what you're
trying to do? Rather than trying to emulate what you'd do in C++, if
you tell us the general problem, we can maybe help you find out how to
approach it in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Maybe C2 should store a reference to C1 rather than C1.prop:
class C2
{
C1 c1;// or CBase cBase;
}

That way, you can use that reference to access the member field of C1.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Herve Bocuse" <gd******@gmail.com> wrote in message
news:cn**********@dns3.cae.ca...
Hi,

I have a question dealing to value and reference types, boxing and
unboxing.
I'm coming from the C++ world so I don't see how to do the following
feature:

I have several primitive types as members of a class. I would like to pass
a
reference to these variables to somewhere else in the code and to be able
to
modify the original variables from there.
So basically, how to store in a class C2 a reference to a primitive type
of
another class C1 to be able to modify the primitive value from C2 ?

I understood the "ref" keyword but I can't use it because:
1. I can't store the reference. I can only use it in the called method.
2. I can't send a ref double and receive it as a ref object (because in
the
receiver I want to be generic and store a reference on any value type).

Thanks a lot

Herve

Nov 16 '05 #3
Thanks,

in the meantime this is the solution I put in place. But since at runtime I
have no information about C1, I must pass an object and use reflection to
find the field.

Herve

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:42********************@news4.e.nsc.no...
Maybe C2 should store a reference to C1 rather than C1.prop:
class C2
{
C1 c1;// or CBase cBase;
}

That way, you can use that reference to access the member field of C1.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Herve Bocuse" <gd******@gmail.com> wrote in message
news:cn**********@dns3.cae.ca...
Hi,

I have a question dealing to value and reference types, boxing and
unboxing.
I'm coming from the C++ world so I don't see how to do the following
feature:

I have several primitive types as members of a class. I would like to pass a
reference to these variables to somewhere else in the code and to be able to
modify the original variables from there.
So basically, how to store in a class C2 a reference to a primitive type
of
another class C1 to be able to modify the primitive value from C2 ?

I understood the "ref" keyword but I can't use it because:
1. I can't store the reference. I can only use it in the called method.
2. I can't send a ref double and receive it as a ref object (because in
the
receiver I want to be generic and store a reference on any value type).

Thanks a lot

Herve


Nov 16 '05 #4

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

Similar topics

13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
19
by: daniel | last post by:
This is a pretty basic-level question, but I'd really like to know, so thanks for any help or pointers you can provide (like what I would google for ;o) Suppose: <code> myFunc() {
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
1
by: az.anonymous | last post by:
Im starting to learn C#, and I made a simple stack class. Inside the stack class I had the following: class StackElement { object info; StackElement below; } Everything works fine cause...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.