Welcome to Dream.In.Code
Become a C# Expert!

Join 137,176 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,400 people online right now. Registration is fast and FREE... Join Now!




No Implicit Conversion - What is the solution?

 
Reply to this topicStart new topic

No Implicit Conversion - What is the solution?, Is there a quicker or easier way to cast datatypes?

Smithy963
25 May, 2008 - 12:11 PM
Post #1

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 5



Thanked: 1 times
My Contributions
Hi, I just moved over from vb.net to c#.net...

I'm under the impressin Implicit conversion (in this example anyway) is the ability to cast one datatype to another, and since c# does not primarily support this. I'm a little confused as to how this is done, in my programs I am constantly getting this:
"Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?"

failing that, I get an error that implicit conversion is not possible.

EDIT: this is a code snippet of a working conversion i bodged together.


CODE

                if (arraySelecter < key.Length - 1)
                {
                    int letterNo= text[letterCount -1];
                    int keyNo = key[arraySelecter -1];
                    int spareInt = letterCount * letterNo;
                    string spare = spareInt.ToString();
                  
                    text[letterCount] = char.Parse(spare);
                    arraySelecter += 1;
                }


text[] and key[] are both Char arrays, and as you can see my code is long and not efficient.

To my point:
Does anyone know of any resources that will aid me in my understanding of this problem,
or of a simple tip that i could do with?

many thanks in advance,

~Smithy

This post has been edited by Smithy963: 25 May, 2008 - 12:15 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: No Implicit Conversion - What Is The Solution?
25 May, 2008 - 12:34 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,046



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Geeky computer definitions: implicit and explicit. Implicit means that it's a given, it's understood. If you put an int value into a float variable you just can, because you don't loose anything, even though the container changes. However, if something changes the nature of a value, the complier will choke on it. When you get an implicit conversion issue what's you're really being told is that you have to explicitly convert it. Explicit meaning you have to spell it out.

This may work:
CODE

int letterNo = (int)text[letterCount -1];


Note the cast value. Sometimes you really are significantly changing the value and you need the Convert suit. Read more here: http://msdn.microsoft.com/en-us/library/aa691289(VS.71).aspx

Hope this helps.
User is online!Profile CardPM
+Quote Post

Smithy963
RE: No Implicit Conversion - What Is The Solution?
25 May, 2008 - 01:14 PM
Post #3

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 5



Thanked: 1 times
My Contributions
Thank you for your post, I have a better understanding of implicit now.

QUOTE(baavgai @ 25 May, 2008 - 01:34 PM) *

int letterNo = (int)text[letterCount -1];


The bolded, and underlined snippet(I've never seen until now), is used when I wish to pass a datatype, to a variable that is not the said datatype, eg.

CODE

int theLetterA = 65;
char myChar = [b](char)[/b]theLetterA;


And this can be used in a variety of ways and with different datatypes?

Thanks,

~Smithy
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: No Implicit Conversion - What Is The Solution?
25 May, 2008 - 02:53 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,237



Thanked: 221 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well only data types that are similar in structure and considered "safe" to convert. For example you can cast a double to an int and an int to a double. You can't cast anything to just something else. If you create an object of type "person" and you want to convert 3 to this person, what does that mean? You can't convert the number 3 to a person class unless otherwise spelled out by the person class. 3 and Person are two dissimilar items and thus are not directly casted to one another typically unless defined to do so.

So in short you can only cast between datatypes if a cast operation has been defined. One has been designed for int and double, but that is why you must define one for your Person object.

You can start your reading on the subject at the following page...

Casting (C#) - MSDN

Look into the "reference" links for this MSDN page and check out the () operator to learn how to create your own casting operations for objects.

Enjoy! smile.gif

This post has been edited by Martyr2: 25 May, 2008 - 02:54 PM
User is online!Profile CardPM
+Quote Post

baavgai
RE: No Implicit Conversion - What Is The Solution?
25 May, 2008 - 03:47 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,046



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(Smithy963 @ 25 May, 2008 - 05:14 PM) *

And this can be used in a variety of ways and with different datatypes?


Martyr's link covers it. There has to be some kind of built in logic to handle it. The notation is also used for "unboxing" objects ( http://msdn.microsoft.com/en-us/library/yz2be5wk(vs.80).aspx ).

Where this notation fails the covert class can be the answer: http://msdn.microsoft.com/en-us/library/system.convert.aspx

User is online!Profile CardPM
+Quote Post

gever
RE: No Implicit Conversion - What Is The Solution?
21 Aug, 2008 - 03:08 AM
Post #6

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 17

Lets take a look at in brief about Implicit and explicit
conversions.

http://vb.net-informations.com/language/vb..._conversion.htm

thanks.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 10:56AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month