473,507 Members | 8,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implicit conversion

4 New Member
Hello

I need the following code to work:
int a = "1234"

How can I modify the int type to accept string (or in ideal case to accept both string and int)???

Can I somehow modify it?
(I tried to inherite int to change its properties, but the compiler returns an error becouse of inheriting (I can't derive int)).

I would appreciate any help.
May 2 '10 #1
8 1312
tlhintoq
3,525 Recognized Expert Specialist
You don't.
An int holds a number. A string holds text.

You need to convert your text to a number before you can assign it to an int.
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
May 2 '10 #2
azzurit
4 New Member
It's clear. But instead of
int a = Int32.Parse("1234");
I need to write only
int a = "1234";

Maybe the operator overriding could help... What do you think?
May 2 '10 #3
tlhintoq
3,525 Recognized Expert Specialist
I think you can either do it the way the C# language is meant to do it... or you can fight it.

*WHY* do you think you need to write it only as
Expand|Select|Wrap|Line Numbers
  1. int a = Int32.Parse("1234");
What is the reason you can't do
Expand|Select|Wrap|Line Numbers
  1. int a = Int32.Parse("1234");
Why do you need to do it differently than the other 3 million C# programmers?
May 3 '10 #4
azzurit
4 New Member
Becouse my prof wants me to do this if I want A:-D
May 3 '10 #5
tlhintoq
3,525 Recognized Expert Specialist
Your professor wants you to do what isn't defined in the C# language?
Hmmm....
You can't do what the language doesn't support.
Expand|Select|Wrap|Line Numbers
  1. int a = "123";
simply cannot be done in C#.
an int cannot hold a string. Period. That's the laws of physics as far as the C# language is concerned.

MSDN for int
MSDN for string

If you look at how memory is allocated for the different datatypes you can see why. An int is 4 bytes big. "1234" at its smallest is 5 bytes: 4 for the numbers plus one for the null termination.
May 3 '10 #6
azzurit
4 New Member
I don't want int to hold a string.
I want it to convert string to int automatically (without using Parse), when assigned...
May 3 '10 #7
tlhintoq
3,525 Recognized Expert Specialist
I want my VW beetle to haul 20 tons of cargo like an 18 wheeler; but it ain't gonna happen.

What you *want* and what you *can accomplish* are often very different things.
May 3 '10 #8
artov
40 New Member
Have you thought that your answer could be "I cannot do it, since there is no implicit conversion from string to int, since strings can contain characters that are not numbers"?
May 4 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3077
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
1
1847
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do...
3
2063
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
11
7590
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
2062
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
12713
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
3
2881
by: bb | last post by:
Hi, Please could you clarify why 'implicit conversion' does not take place while assigning an iterator to reverse_iterator. However, it happens while initializing/constructing. e.g. typedef...
3
2456
by: Jess | last post by:
Hello, I can perform implicit conversion through constructor, like class A{ public: A(int x):a(x){}; int a; };
1
1821
by: drop | last post by:
Hi all, I'd like to know if it's possible to declare an implicit type conversion for when I use declarative Synthax in html. For example, when I have the DropDownList, I can declatively set...
4
2897
by: The Last Danish Pastry | last post by:
If I have two types, t1 and t2, say, I can write an implicit conversion operator from t1 to t2. Can I write implicit conversion operator from t1 to t2? I don't think I can... but, if not, what...
0
7223
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,...
0
7110
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
7314
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,...
1
7030
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...
0
7482
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...
1
5041
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...
0
4702
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...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.