473,385 Members | 1,927 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,385 software developers and data experts.

Tricky boxing question

I want to do something along the lines of this:

class someclass {
static void Main() {
int i = 5;
int j = 3;
changevalue(ref i, j);
Console.WriteLine(i.ToString()); //I want this to output 3
}
//it's important that this parameter is an object, and not an int or
something
static void changevalue(ref object i, object j) {
i = j
}
}

I'd like this to work for any type, not just ints. Is there any way to
accomplish something like this?

Chris
Nov 15 '05 #1
3 3217
100
Hi Chris
The best you can do, I believe, is to revise your program logic

B\rgds
100
"Chris Capel" <ch***@ibanktech.net.zerospam> wrote in message
news:eS**************@TK2MSFTNGP10.phx.gbl...
I want to do something along the lines of this:

class someclass {
static void Main() {
int i = 5;
int j = 3;
changevalue(ref i, j);
Console.WriteLine(i.ToString()); //I want this to output 3
}
//it's important that this parameter is an object, and not an int or
something
static void changevalue(ref object i, object j) {
i = j
}
}

I'd like this to work for any type, not just ints. Is there any way to
accomplish something like this?

Chris

Nov 15 '05 #2
100
Hi Subash,
I don't thing this is exactly what Chris wants to do. a and b are declared
as a references to object. I think Chris wants to have changevalue method
regardles of the type of variables. It is kind a silly to keep value types
in boxing state. At least it is error prone.

B\rgds
100
"Subash" <su************@nospam.com> wrote in message
news:o$**************@cpmsftngxa06.phx.gbl...
class Class1
{
static void Main()
{
// Declaration
object a, b;
// Integer stuff
a = 1;
b = 2;
changevalue(ref a, b);
Console.WriteLine(a.ToString()); //This will output 2

// string stuff
a = "string 1";
b = "string 2";
changevalue(ref a, b);
Console.WriteLine(a.ToString()); //This will output "string 2"
}

static void changevalue(ref object i, object j)
{
i = j;
}
}

Nov 15 '05 #3
Yeah, that sounds ok. It's not an earth-shaking matter.

Chris

"100" <10*@100.com> wrote in message
news:#9**************@TK2MSFTNGP12.phx.gbl...

Hi Chris,
will change oA, and not a. In other words, I don't know how to create an
object that references the original value type. That is the point. You can't. Unless you make *changevalue* method to be
changevalue (ref int a, int b);

or you have to return back the value to its original varable after

changing it.
int a = 1, b = 2
object oA = (object) a;
changevalue(oA, b);
a = (int)oA; //What about if it is not *int*

B\rgds
100

Nov 15 '05 #4

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

Similar topics

14
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
4
by: Alistair Welchman | last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following: foreach ( DataRow row in dsWorkDays.Tables.Rows ) { Guid PersonId = (Guid)row; DateTime Day = (DateTime)row;
5
by: Craig | last post by:
Hi everyone, As a relative new-comer to the wonderful world of .NET Framework and the C# langauge I have come across something that I would like to clarify (hopefully with the help of kind...
1
by: Tom | last post by:
Couple of questions relating to boxing. Firstly, I already know that boxing is the processing of temporarily copying a ValueType (e.g. struct, enum) to the heap so that the system can treat a...
7
by: J.Marsch | last post by:
Hello all: I am trying to introduce the concept of boxing (and some of the hang-ups) to some developers that are coming onto a project. A while back, I read a really cool article that was...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
94
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. ...
19
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.