No Implicit Conversion - What is the solution?

Is there a quicker or easier way to cast datatypes?

Page 1 of 1

5 Replies - 11571 Views - Last Post: 21 August 2008 - 04:08 AM Rate Topic: -----

#1 Smithy963  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 19-February 08

No Implicit Conversion - What is the solution?

Post icon  Posted 25 May 2008 - 01:11 PM

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.


				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 - 01:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: No Implicit Conversion - What is the solution?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5775
  • View blog
  • Posts: 12,587
  • Joined: 16-October 07

Re: No Implicit Conversion - What is the solution?

Posted 25 May 2008 - 01:34 PM

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:
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.microsof...289(VS.71).aspx

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Smithy963  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 19-February 08

Re: No Implicit Conversion - What is the solution?

Posted 25 May 2008 - 02:14 PM

Thank you for your post, I have a better understanding of implicit now.

View Postbaavgai, on 25 May, 2008 - 01:34 PM, said:

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.

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
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4308
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: No Implicit Conversion - What is the solution?

Posted 25 May 2008 - 03:53 PM

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! :)

This post has been edited by Martyr2: 25 May 2008 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5775
  • View blog
  • Posts: 12,587
  • Joined: 16-October 07

Re: No Implicit Conversion - What is the solution?

Posted 25 May 2008 - 04:47 PM

View PostSmithy963, on 25 May, 2008 - 05:14 PM, said:

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.microsof...5wk(vs.80).aspx ).

Where this notation fails the covert class can be the answer: http://msdn.microsof...em.convert.aspx
Was This Post Helpful? 0
  • +
  • -

#6 gever  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 29
  • Joined: 19-June 08

Re: No Implicit Conversion - What is the solution?

Posted 21 August 2008 - 04:08 AM

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

http://vb.net-inform..._conversion.htm

thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1