Hi, I want to put a type conversion in my class, but I don't want the
conversion to be usable in a passed parameter because it makes no sense.
class cData
{
string s;
public cData(string s)
{
this.s=s;
}
public static implicit operator cData(string s)
{
cData data=new cData(null);
data.s=s;
return data;
}
public void f(cData data)
{
data.s=s;
}
} // class cData
void test()
{
string s="123";
cData data1=new cData("456");
data1=s; // this is OK, it's why I want the type conversion, data1
changes to a new cData with s="123"
// but I don't want this
data1.f(s); // C# passes reference types by reference - no type
converted parameters should be allowed
// it should be a compiler error - IT DOES NOTHING
cData data2=new cData("789");
data1.f(data2); // this is OK, it's why I want the function f, data2.s
changes to "456"
} 4 1674
Mark,
I can see your problem. Since you implemented the implicit operator for
cData the string being passed to the f() method does a converstion.
Here is one idea, what if you changed the f() methods parameter to be of
type object. You could then, inside the method, make sure the type is of
type cData. If it's not you could always throw an exception.
Just a though.
--
Glen Jones MCSD
"Mark Oliver" <ma*********@co mcast.net> wrote in message
news:00vJb.2027 98$8y1.709502@a ttbi_s52... Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense.
class cData { string s;
public cData(string s) { this.s=s; }
public static implicit operator cData(string s) {
cData data=new cData(null); data.s=s; return data; } public void f(cData data) { data.s=s; } } // class cData void test() { string s="123"; cData data1=new cData("456"); data1=s; // this is OK, it's why I want the type conversion, data1 changes to a new cData with s="123" // but I don't want this data1.f(s); // C# passes reference types by reference - no type converted parameters should be allowed // it should be a compiler error - IT DOES NOTHING
cData data2=new cData("789"); data1.f(data2); // this is OK, it's why I want the function f,
data2.s changes to "456" }
I would suggest that you use an explicit cast, rather than an implicit
class. I know it's more work in places, but it fits the requirements of how
to use implicit and explicit casting styles.
Chris R.
"Glen Jones MCSD" <gl********@mai lhot.com> wrote in message
news:hZ******** ************@co mcast.com... Mark,
I can see your problem. Since you implemented the implicit operator for cData the string being passed to the f() method does a converstion.
Here is one idea, what if you changed the f() methods parameter to be of type object. You could then, inside the method, make sure the type is of type cData. If it's not you could always throw an exception.
Just a though. -- Glen Jones MCSD
"Mark Oliver" <ma*********@co mcast.net> wrote in message news:00vJb.2027 98$8y1.709502@a ttbi_s52... Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense.
class cData { string s;
public cData(string s) { this.s=s; }
public static implicit operator cData(string s) {
cData data=new cData(null); data.s=s; return data; } public void f(cData data) { data.s=s; } } // class cData void test() { string s="123"; cData data1=new cData("456"); data1=s; // this is OK, it's why I want the type conversion, data1 changes to a new cData with s="123" // but I don't want this data1.f(s); // C# passes reference types by reference - no type converted parameters should be allowed // it should be a compiler error - IT DOES
NOTHING cData data2=new cData("789"); data1.f(data2); // this is OK, it's why I want the function f, data2.s changes to "456" }
Mark Oliver <ma*********@co mcast.net> wrote: Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense.
You can't - but personally I believe that implicit conversion operators
are a bad idea. However, I also notices this in your post:
data1.f(s); // C# passes reference types by reference - no type converted parameters should be allowed
C# does *not* pass anything by reference unless you explicitly say so.
There is a difference between passing a reference by value and passing
a value by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi, I took you idea, it sure beat looking through each of the 550 times I
used the type conversion in my code.
Thanks Very Much,
Mark
"Glen Jones MCSD" <gl********@mai lhot.com> wrote in message
news:hZ******** ************@co mcast.com... Mark,
I can see your problem. Since you implemented the implicit operator for cData the string being passed to the f() method does a converstion.
Here is one idea, what if you changed the f() methods parameter to be of type object. You could then, inside the method, make sure the type is of type cData. If it's not you could always throw an exception.
Just a though. -- Glen Jones MCSD
"Mark Oliver" <ma*********@co mcast.net> wrote in message news:00vJb.2027 98$8y1.709502@a ttbi_s52... Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense.
class cData { string s;
public cData(string s) { this.s=s; }
public static implicit operator cData(string s) {
cData data=new cData(null); data.s=s; return data; } public void f(cData data) { data.s=s; } } // class cData void test() { string s="123"; cData data1=new cData("456"); data1=s; // this is OK, it's why I want the type conversion, data1 changes to a new cData with s="123" // but I don't want this data1.f(s); // C# passes reference types by reference - no type converted parameters should be allowed // it should be a compiler error - IT DOES
NOTHING cData data2=new cData("789"); data1.f(data2); // this is OK, it's why I want the function f, data2.s changes to "456" }
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Batista, Facundo |
last post by:
Here I send it.
Suggestions and all kinds of recomendations are more than welcomed.
If it all goes ok, it'll be a PEP when I finish writing/modifying the code.
Thank you.
.. Facundo
|
by: Tongu? Yumruk |
last post by:
I have a little proposal about type checking in python. I'll be glad
if you read and comment on it. Sorry for my bad english (I'm not a
native English speaker)
A Little Stricter Typing in Python - A Proposal
As we all know, one of the best things about python and other
scripting languages is dynamic typing (yes I know it has advantages...
|
by: Kapt. Boogschutter |
last post by:
I'm trying to create a function that has at least 1 Argument but can also
contain any number of Arguments (except 0 because my function would have no
meaning for 0 argument).
The arguments passed to the function are strings or must be (automaticly
converted to a string e.g. the number 10 should become the string "10".
My problem is that I...
|
by: buda |
last post by:
Let me see if I got this :)
1. I know the rules for type conversions in arithmetic expressions
2. I know that an implicit type conversion is done at assignment, so
float x = 1.23;
int t = (int) x;
is equivalent to
int t = x;
(could the latter produce a warning on some complier?)
3. I know that implicit conversions take place with function...
|
by: christer-sandberg |
last post by:
When I typecast a function and call it trough the casted pointer (se
code below) I get the warnings:
warning: function called through a non-compatible type
if this code is reached, the program will abort
and the gcc generates abort code for the call. This happens when I use
gcc 3.4 but not when I use 3.3. Is there any flag that can change...
| |
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"
}
|
by: pgconnolly |
last post by:
/* foreach does implicit type conversion on elements of a params argument or
Generic.List.
* This is not good.
* Examples of evil follow...
*/
using System;
// I love it when C# is strict with me...
using System.Collections.Generic;
|
by: Enekajmer |
last post by:
Hi,
1 int main()
2 {
3 float a = 17.5;
4 printf("%d\n", a);
5 printf("%d\n", *(int *)&a);
6 return 0;
7 }
|
by: Harold Howe |
last post by:
I am running into a situation where the compiler complains that it
cannot infer the type parameters of a generic method when one of the
function arguments is an anonymous method.
Here is a complete code example:
//-----------------------------
using System;
using System.Collections.Generic;
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |