473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(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 3232
100
Hi Chris
The best you can do, I believe, is to revise your program logic

B\rgds
100
"Chris Capel" <ch***@ibanktec h.net.zerospam> wrote in message
news:eS******** ******@TK2MSFTN GP10.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.WriteLi ne(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$******** ******@cpmsftng xa06.phx.gbl...
class Class1
{
static void Main()
{
// Declaration
object a, b;
// Integer stuff
a = 1;
b = 2;
changevalue(ref a, b);
Console.WriteLi ne(a.ToString() ); //This will output 2

// string stuff
a = "string 1";
b = "string 2";
changevalue(ref a, b);
Console.WriteLi ne(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******** ******@TK2MSFTN GP12.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
323
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
4
1715
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
1764
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 people such as yourselves). To quote, "Everything in C# is an object" (including "primitive" value types, primitive being in quotations because this is what my question concerns).
1
2228
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 value type like a reference type. However, I have some questions relating to implicit boxing: 1. If I add custom instance method on a struct, will it box that type each time the method is called? For example, suppose I have the following: public...
7
1486
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 formatted as a quiz. It introduced a number of the weird little nuances that you run into when implicit boxing occurs in your code (like values not being changed when you expected them to -- do to the fact that a copy was made during the box or unbox...
24
2633
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 you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
94
5698
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. Thanks.
19
13731
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
7884
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 biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every other platform in every other language. Normally a value type is the actual data itself stored in...
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
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
10310
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
9138
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
7613
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
5515
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
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
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.