PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
New to C# and not sure how to cast this?-VBForums
Results 1 to 12 of 12

Thread: New to C# and not sure how to cast this?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    New to C# and not sure how to cast this?

    I'm getting a cast error in design time and not sure how to fix this. I've attached a screenshot.
    Blake

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,251

    Re: New to C# and not sure how to cast this?

    Quote Originally Posted by blakemckenna View Post
    I'm getting a cast error in design time and not sure how to fix this. I've attached a screenshot.
    #1 images of code are the worst, this forum has [highlight=c#][/highlight] tags for a reason.

    #2 you have x defined as an integer (by definition can't have decimal values) and you're multiplying it by 0.3 (a decimal value) so really all you need is to use a float or double (two types that can hold decimal values) and move the "* 0.3" part up to the end of the img.Width and it'll do the math storing it in the "x" variable in one swoop.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: New to C# and not sure how to cast this?

    That was it...thanks you!
    Blake

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,783

    Re: New to C# and not sure how to cast this?

    That code doesn't really make sense. You have an int value and you multiply it by a double and then assign the result back to the same int variable. What is the likelihood that any integer multiplied by 0.3 will yield another integer? The result of 'x * 0.3' is going to be a double, so you're going to have to round that in some way (Math.Round, Math.Ceing, Math.Floor, Math.Truncate) and then convert the result to an int in order to assign back to an int variable, e.g.
    csharp Code:
    1. x = Convert.ToInt32(Math.Round(x * 0.3));
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,783

    Re: New to C# and not sure how to cast this?

    Quote Originally Posted by JuggaloBrotha View Post
    #1 images of code are the worst, this forum has [highlight=c#][/highlight] tags for a reason.
    Personally, I prefer the result of [highlight=csharp][/highlight] to [highlight=c#][/highlight], e.g.

    c# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4.  
    5. namespace Wunnell.Extensions
    6. {
    7.     /// <summary>
    8.     /// Contains methods that extend the <see cref="IEnumerable{T}"/> interface.
    9.     /// </summary>
    10.     public static class EnumerableExtensions
    11.     {
    12.         /// <summary>
    13.         /// A random number generator.
    14.         /// </summary>
    15.         private static Random rng;
    16.  
    17.         /// <summary>
    18.         /// Randomises the items in an enumerable list.
    19.         /// </summary>
    20.         /// <typeparam name="T">
    21.         /// The type of the items in the list.
    22.         /// </typeparam>
    23.         /// <param name="source">
    24.         /// The input list.
    25.         /// </param>
    26.         /// <returns>
    27.         /// The items from the input list in random order.
    28.         /// </returns>
    29.         public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
    30.         {
    31.             EnsureRandomInitialized();
    32.  
    33.             return source.OrderBy(o => rng.NextDouble());
    34.         }
    35.  
    36.         /// <summary>
    37.         /// Initialises the random number generator if it is not already.
    38.         /// </summary>
    39.         private static void EnsureRandomInitialized()
    40.         {
    41.             rng = rng ?? new Random();
    42.         }
    43.     }
    44. }

    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4.  
    5. namespace Wunnell.Extensions
    6. {
    7.     /// <summary>
    8.     /// Contains methods that extend the <see cref="IEnumerable{T}"/> interface.
    9.     /// </summary>
    10.     public static class EnumerableExtensions
    11.     {
    12.         /// <summary>
    13.         /// A random number generator.
    14.         /// </summary>
    15.         private static Random rng;
    16.  
    17.         /// <summary>
    18.         /// Randomises the items in an enumerable list.
    19.         /// </summary>
    20.         /// <typeparam name="T">
    21.         /// The type of the items in the list.
    22.         /// </typeparam>
    23.         /// <param name="source">
    24.         /// The input list.
    25.         /// </param>
    26.         /// <returns>
    27.         /// The items from the input list in random order.
    28.         /// </returns>
    29.         public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
    30.         {
    31.             EnsureRandomInitialized();
    32.  
    33.             return source.OrderBy(o => rng.NextDouble());
    34.         }
    35.  
    36.         /// <summary>
    37.         /// Initialises the random number generator if it is not already.
    38.         /// </summary>
    39.         private static void EnsureRandomInitialized()
    40.         {
    41.             rng = rng ?? new Random();
    42.         }
    43.     }
    44. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,251

    Re: New to C# and not sure how to cast this?

    Quote Originally Posted by jmcilhinney View Post
    Personally, I prefer the result of [highlight=csharp][/highlight] to [highlight=c#][/highlight], e.g.
    Learn something new everyday, good to know there's a difference. Actually I didn't know there was a 'csharp' option on this forum for the highlight tag, all the other forums I use will use the visual studio code coloring when using 'c#' in a code, xcode, or highlight tag. I wonder why this forum doesn't.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: New to C# and not sure how to cast this?

    The only reason I used an image was to show the error that was being produced. I normally include the raw code.
    Blake

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,783

    Re: New to C# and not sure how to cast this?

    Quote Originally Posted by blakemckenna View Post
    The only reason I used an image was to show the error that was being produced. I normally include the raw code.
    Given that an error message is text, I'd suggest just posting that as text. I tend to use [quote]error message here[/quote].
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: New to C# and not sure how to cast this?

    Thanks jmc...I'll do that from now on!
    Blake

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,783

    Re: New to C# and not sure how to cast this?

    Quote Originally Posted by jmcilhinney View Post
    That code doesn't really make sense. You have an int value and you multiply it by a double and then assign the result back to the same int variable. What is the likelihood that any integer multiplied by 0.3 will yield another integer? The result of 'x * 0.3' is going to be a double, so you're going to have to round that in some way (Math.Round, Math.Ceing, Math.Floor, Math.Truncate) and then convert the result to an int in order to assign back to an int variable, e.g.
    csharp Code:
    1. x = Convert.ToInt32(Math.Round(x * 0.3));
    It's worth noting that that code would produce the same result without the Math.Round call.

    Math.Round:
    Return Value
    Type: System.Double
    The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that the method returns a Double type rather than an integral type.
    Convert.ToInt32:
    Return Value
    Type: System.Int32
    value, rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.
    It's only if you wanted to round using different rules that you'd need to add an extra call.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: New to C# and not sure how to cast this?

    As I said, I'm learning C#...so thanks for the tip!
    Blake

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,783

    Re: New to C# and not sure how to cast this?

    Quote Originally Posted by blakemckenna View Post
    As I said, I'm learning C#...so thanks for the tip!
    There's no difference between C# and VB here, if you had Option Strict On in VB. This code:
    vb.net Code:
    1. Dim x As Integer = img.Width
    2.  
    3. x = x * 0.3
    would give you an equivalent error message. An Int32 multiplied by a Double will produce a Double in both languages and you can't assign a Double value to an Int32 variable without a narrowing conversion. With Option Strict Off, VB would implicitly round and convert the Double, while with Option Strict On you would require the same explicit conversion and optional explicit rounding as in C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width