473,662 Members | 2,588 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 1319
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("12 34");
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
3087
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, piece types, squares, etc.). To accomplish this, I used implicit conversions. For instance, a color used to be: typedef int Color; // and a few constants... Now a color is (paraphrased):
1
1856
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 implicit conversion for this case, however if instead of class X we just use a pointer then the implicit conversion will work. Anyone have any ideas why it will not do implicit conversion?
3
2071
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
7606
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 Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
9
2076
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 emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
11
12762
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
3
2917
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 std::map<int, std::stringMIS; MIS m1;
3
2468
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
1834
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 it's Selected value like this : SelectedValue='<%# Bind("TaxCondition") %>'. In this case, TaxCondition is an int, and the conversion from int to string and string to int is made automatically.
4
2906
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 is the "next nicest thing"? -- Clive Tooth http://www.shutterstock.com/cat.mhtml?gallery_id=61771
0
8432
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
8344
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
8764
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...
0
8633
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
7367
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
6186
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
5654
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.