473,698 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ref paramater made ugly

I have a method that takes a ref string parameter. This is because it
will be called in loops and most of the time it will not need to modify
the paramater at all, so I preferred not to return a newly allocated
string object.

I need to pass items of an array list (can't use specialized collection
in compact framework) to this method. I can't pass the array item
directly because I get a compiler error (a ref or out argument must be
an lvalue). So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?
Nov 16 '05 #1
2 1280

"Brad Wood" <bradley_.wood_ @ndsu_.nodak_.e du> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
I have a method that takes a ref string parameter. This is because it will
be called in loops and most of the time it will not need to modify the
paramater at all, so I preferred not to return a newly allocated string
object.

I need to pass items of an array list (can't use specialized collection in
compact framework) to this method. I can't pass the array item directly
because I get a compiler error (a ref or out argument must be an lvalue).
So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?


No. ArrayList only looks like an array because of C# indexers.

string temp = (string)arrayLi st[i];

is just syntactic sugar for

string temp = (string)ArrayLi st::get_Item(i) ;

and
arrayList[i] = temp
is
ArrayList::set_ Item(i, temp);

There's no better way to write this.

Even if the C# compiler somehow allowed you to write

obj.refMethod ( ref arrayList[i]);

It would still have to call get_Item/set_Item, and would also need a
temporary variable to hold the result.

David
Nov 16 '05 #2
Not sure its better. But you could propably get rid of the ref method with
something like:

string temp = (string)array[i];
string result = obj.RefMethod( temp ); // Returns null if no change needed.
if ( result != null )
array[i] = result;

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Brad Wood" <bradley_.wood_ @ndsu_.nodak_.e du> wrote in message
news:#i******** ******@TK2MSFTN GP12.phx.gbl...
I have a method that takes a ref string parameter. This is because it
will be called in loops and most of the time it will not need to modify
the paramater at all, so I preferred not to return a newly allocated
string object.

I need to pass items of an array list (can't use specialized collection
in compact framework) to this method. I can't pass the array item
directly because I get a compiler error (a ref or out argument must be
an lvalue). So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?


Nov 16 '05 #3

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

Similar topics

9
2182
by: Pedro Graca | last post by:
<?php function ugly_array() { return array(1, 2, 3, 4, 5); } $arr = ugly_array(); echo $arr; ?> not so ugly :) now ... I want to get rid of the $arr temporary variable.
6
3936
by: Quick Function | last post by:
Hi, I developed a site and used css. I put almost all style information in the css and used a lot of "id=my_css_class" in the html. They is little style specification in the html. I found that on some computers with slow internet links this seems to be a small problem. Looks like the html page was loaded first and displayed in a very ugly layout because the css has not be loaded yet. Then the css was loaded and the page became beautiful....
6
4519
by: WertmanTheMad | last post by:
Ok, This sounds dangerous (and yes I know it is) But its in a controlled enviroment and I really need to know how to do this. How can I pass a Subquery for an Exist or In clause as a paramater Something like this CREATE procedure dbo.mytry
2
1628
by: Max Adams | last post by:
Question about using delegates with a string paramater to return a ListViewItem object... All, I have a thread and I want this thread to post messages to the main GUI thread using a delegate. This is my code fragment: ListViewItem lview = m_form.Invoke( m_form.m_DelegateAddItem, new object {"hello"} );
0
1027
by: Matt Swift | last post by:
Anyone have experience of doing this? My aim is thus. I'd like to supply my SP with datetime related information, but possibly break entries down into **/**/**** and have the interface actually offer DDLs in place of the standard TextBox fields that ReportingServices places for paramater entry as default. If there's a way anyone knows of, a url to get me started, would be well appreciated!
5
2355
by: yxq | last post by:
Hello I am using VS.Net 2002, my icons of XP type look ugly, and i have saw the article. i have added the manifest file. Sometime the icons show very good, but sometime the icons look very ugly. Must i use the VS.Net 2003? Is The Framework v1.1 necessary to run VS.Net 2003 program?
7
1374
by: teo | last post by:
I have to validate the user input to prvent HTML injection. I use validateRequest=True and when a potentially malicious input occurs AspNet immediately sends its ugly page about the 'System.Web.HttpRequestValidationException', a difficult to understand page and that compels the user to go back to the previous page with his browser's button.
5
7275
by: Frank Jovi | last post by:
I am working on a website with ASP.NET 2.0 I started with a SQL database .mdf. After further discussions with my client, we decided to switch to an Access database. I created a Select query that has a parameter that is a comma delimited string brought in through a paramater in to the <selectcommand=...> The query worked perfectly before, but I ran into a problem in the switch over because: WHERE charindex(',' + CustomerID + ',', ','...
2
1892
by: Jim Langston | last post by:
In my function/method paramater list I try to keep constant correctness. I.E. I would use: int Foo( const char* a, int b ) instead of int Foo( char* a, int b ) My question reguards int b. Would it be more correct to make it int Foo( const char* a, const int b ) instead? I do know the effect of this would be that I can't change b in the function, which I normally never do anyway.
0
8674
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
8604
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
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
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
8861
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
7728
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
6518
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
4369
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.