Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 204609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:28:58+00:00 2026-05-11T17:28:58+00:00

I created a Converter to convert from double to integer. But the line return

  • 0

I created a Converter to convert from double to integer.

But the line “return (int)value;” always gets a “specified cast is not valid.”

What do I have to do so that my Converter successfully converts a double and sends back an integer?

Converter:

namespace TestChangeAngle
{
    [ValueConversion(typeof(double), typeof(int))]
    class DoubleToIntegerConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

XAML:

<Page x:Class="TestChangeAngle.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestChangeAngle"
    Title="Page1">
    <Page.Resources>
        <local:DoubleToIntegerConverter x:Key="DoubleToIntegerConverter"/>
    </Page.Resources>

    <StackPanel HorizontalAlignment="Left" Margin="20">
        <Image Source="images\logo2.png" 
               RenderTransformOrigin="0.5, 0.5"
               Width="100" 
               Margin="10">
            <Image.RenderTransform>
                <RotateTransform Angle="{Binding ElementName=TheSlider, Path=Value}"/>
            </Image.RenderTransform>
        </Image>
        <Slider x:Name="TheSlider"
                Width="200" 
                Minimum="0"
                Maximum="360"
                HorizontalAlignment="Center"
                Margin="10" 
                Cursor="Hand"/>
        <TextBox x:Name="TheAngle"
                 Margin="10"
                 Width="100">
            <TextBox.Text>
                <Binding ElementName="TheSlider"
                         Path="Value"
                         UpdateSourceTrigger="PropertyChanged"
                         Converter="{StaticResource DoubleToIntegerConverter}"
                         Mode="TwoWay">
                    <Binding.ValidationRules>
                        <local:MinMaxValidationRule Minimum="0" Maximum="360"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

    </StackPanel>
</Page>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:28:58+00:00Added an answer on May 11, 2026 at 5:28 pm

    You are attempting to cast (not convert) from double to int, which won’t work. You need to do an implicit conversion or use Convert.ToInt32() — since the argument is actually of type object I think you’ll need the latter to keep the compiler happy. It’s up to you whether you want to include the culture’s format provider or not.

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Convert.ToInt32(value);
    }
    

    You can use the cast operator when the object’s have the same shape, that is, when one object is an instance of the type to which you are casting. For example, if class Foo extends class Bar, then you can cast an object of type Foo to type Bar – because a Foo object has all the methods and properties that a Bar object would. You couldn’t, however, cast an object of type Bar to type Foo because Foo changes (or can change, as far as the compiler is concerned) the shape of Bar, by adding methods or properties that a Bar object doesn’t have.

    In your case you are dealing with primitive types which only share the object interface – there isn’t an inheritance relationship between them except that they both derive from object. There is, however, an implicit conversion between the two. You can assign an object one type to a variable of the other, though you may lose some precision in the value.

    double x = 1.1;
    int y = 0;
    
    y = x;  // implicit conversion, this works, y will be 1
    x = y;  // implicit conversion, this works, x will be 1.0
    

    You can’t, however, cast an object of one type to the other. Casting implies that you will be using the object as if it were of the other type. In this case the shapes differ and it can’t be done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ended up with this: for a, b, c in data:… May 11, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer It sounds like something is using the LocalSqlServer automagic connection… May 11, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer I guess you could do something like this: function MyClass… May 11, 2026 at 7:59 pm

Related Questions

In our app, we currently live with the legacy of a decision to store
I'm trying to write a custom JavascriptConverter for use with a WebService I'm writing.
I'm confused. I've got a Silverlight project that currently runs and displays a list
I'd like to write a MessageConverter class that can wrap another MessageConverter. This MessageConverter
I have a class that I want to use to store properties for another

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.