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 1659
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*********@comcast.net> wrote in message
news:00vJb.202798$8y1.709502@attbi_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********@mailhot.com> wrote in message
news:hZ********************@comcast.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*********@comcast.net> wrote in message news:00vJb.202798$8y1.709502@attbi_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*********@comcast.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.com> 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********@mailhot.com> wrote in message
news:hZ********************@comcast.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*********@comcast.net> wrote in message news:00vJb.202798$8y1.709502@attbi_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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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...
|
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,...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |