Author Topic: My conversion from string to double  (Read 8127 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
My conversion from string to double
« on: May 11, 2015, 08:18:17 AM »
Hello guys ,

Is this conversion from string to double correct or is there any better way of rewriting it ? I am using the WPF Application .

The idea is to get the value from Textbox1 and multiply it with 2.75 then automatically set it in Textbox2 .

Code - C#: [Select]
  1. private void Textbox1_TextChanged(object sender, TextChangedEventArgs e)
  2.         {
  3.             double d = 0;
  4.             double.TryParse(Textbox1.Text, out d);
  5.             Textbox2.Text = (d * 2.75).ToString();
  6.         }
  7.  

Thank you .

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: My conversion from string to double
« Reply #1 on: May 11, 2015, 08:25:14 AM »
Also you can use Convert.ToDouble method. But Double.TryParse method is better way, if you correctly use it:
Code - C#: [Select]
  1. private void Textbox1_TextChanged(object sender, TextChangedEventArgs e){
  2.   double d = 0.0;
  3.   Boolean stringIsValidDouble = double.TryParse(Textbox1.Text, out d);
  4.   if(stringIsValidDouble)
  5.     Textbox2.Text = (d * 2.75).ToString();
  6. }
« Last Edit: May 11, 2015, 08:31:10 AM by Andrey Bushman »

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #2 on: May 11, 2015, 08:46:32 AM »
Also you can use Convert.ToDouble method. But Double.TryParse method is better way, if you correctly use it:

Thank you Andrey Bushman for your reply , your modification is really helpful .

The function Convert.ToDecimal is not available in WPF Application and because of that I have mentioned that I am using WPF Application  :-)

I just wondered if there is any better way of writing the codes and nothings more than that .

Many thanks .

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: My conversion from string to double
« Reply #3 on: May 11, 2015, 09:03:37 AM »
The function Convert.ToDecimal is not available in WPF Application
You are not right.

Code - C#: [Select]
  1. using System;
  2. using System.Windows;
  3.  
  4. namespace ConsoleApplication1 {
  5.   class Program {
  6.  
  7.     [STAThread]
  8.     static void Main(string[] args) {
  9.       Application app = new Application();
  10.       Window win = new Window();
  11.       win.Loaded += win_Loaded;
  12.       app.Run(win);
  13.     }
  14.  
  15.     static void win_Loaded(object sender, EventArgs e) {
  16.       Double temp = 123.45;
  17.       String str = temp.ToString();
  18.       Double doubleValue = Convert.ToDouble(str) * 100.0;
  19.       Window win = sender as Window;
  20.       if (null != win) {
  21.         win.Title = doubleValue.ToString();
  22.       }
  23.     }
  24.   }
  25. }

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #4 on: May 11, 2015, 09:18:22 AM »
I am still confused  :-(


Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: My conversion from string to double
« Reply #5 on: May 11, 2015, 09:30:41 AM »
I assume you use the aliases for the System namespace. Something like this:
Code - C#: [Select]
  1. using yourAliase = System;
At this case use this:
Code - C#: [Select]
  1. yourAliase.Convert.ToDouble(str)
or this:
Code - C#: [Select]
  1. System.Convert.ToDouble(str)

The System.Convert is defined in the mscorlib.dll assembly.

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #6 on: May 11, 2015, 09:41:19 AM »
I assume you use the aliases for the System namespace. Something like this:
Code - C#: [Select]
  1. using yourAliase = System;
At this case use this:
Code - C#: [Select]
  1. yourAliase.Convert.ToDouble(str)
or this:
Code - C#: [Select]
  1. System.Convert.ToDouble(str)

Yes that worked after adding the system namespace to the program  :-D

Many thanks .

The System.Convert is defined in the mscorlib.dll assembly.

I tried to add the assembly to the program but gave me a message and it says that the assembly would be automatically added to the .dll file but I needed before  :-D


Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: My conversion from string to double
« Reply #7 on: May 11, 2015, 09:47:23 AM »
the assembly would be automatically added to the .dll file
I know. But I don't know what you did with your project already. It is possible to forbid the automatically loading of the set of common DLL files. Read this book - it will usefull for you.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #8 on: May 11, 2015, 11:12:26 AM »
Hi Coder,

Maybe you can get some inspiration from this little sample. It uses the Autodesk.AutoCAD.Runtime.Converter class to make conversion within a WPF control.
Speaking English as a French Frog

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: My conversion from string to double
« Reply #9 on: May 11, 2015, 01:27:47 PM »
Just bind to a double in your Datacontext. WPF uses the ToString() method by default to display data that doesn't have a DataTemplate.

Code - C#: [Select]
  1. public class MyViewModel
  2.     {
  3.         public double MyDouble { get; set; }
  4.     }

Code - C#: [Select]
  1. <Grid>
  2.         <StackPanel Orientation="Vertical">
  3.             <TextBox Name="TextBoxOne" Text="{Binding Path=MyDouble, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
  4.             <TextBox Text="{Binding ElementName=TextBoxOne, Path=Text, UpdateSourceTrigger=PropertyChanged}"></TextBox>
  5.         </StackPanel>
  6.     </Grid>

Implement INotifyPropertyChanged and you could bind TextBoxTwo to MyDouble also.  Lots of ways to skin this cat without data conversion.
Revit 2019, AMEP 2019 64bit Win 10

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #10 on: May 12, 2015, 01:01:00 AM »
Hi gile

Thank you for your reply , I tried your palette program and it gave me an error in loading the palette design .

I added the references ( Acmgd ..... etc )

Nice design of palettes by the way .  :-)

« Last Edit: May 12, 2015, 01:05:20 AM by Coder »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #11 on: May 12, 2015, 01:50:34 AM »
Hi,

I can't say why it doesn't work for you.
Perhaps you can try to Remove the ConvertPalette_18 project from the solution, Built the solution and Add back the Existing project ConvertPalette_18.

Anyway you can set the DataContext from the code behind.
In ConverterControl.xaml.cs:
Code - C#: [Select]
  1.     public partial class ConverterControl : UserControl
  2.     {
  3.         public ConverterControl()
  4.         {
  5.             InitializeComponent();
  6.             this.DataContext = new ViewModel.ConverterViewModel();
  7.         }
  8.     }
« Last Edit: May 12, 2015, 02:15:09 AM by gile »
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #12 on: May 12, 2015, 02:40:53 AM »
Hi MexicanCustard

I need to study the codes your posted specifically the xaml codes because they look new to me , just bear with me please .

Thank you.

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #13 on: May 12, 2015, 02:46:15 AM »
Hi,

Perhaps you can try to Remove the ConvertPalette_18 project from the solution, Built the solution and Add back the Existing project ConvertPalette_18.

Hi gile,

I removed the palette 18 and built the project successfully and it works in my AutoCAD and it looks the same as in your attached image of the palette as in you thread of the convert palette .

I tried to add the removed palette once again but with no luck , the add Existing project in the add option of the right click on the project is not available  :-( I am sorry.

Many thanks for your help and I need to study the input codes that is about conversions .


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #14 on: May 12, 2015, 05:59:40 AM »
To add back the project: right click the 'ConverterPalette' solution in the solution explorer > Add > Existing project > browse to the ConverterPalette_18 folder and add the ConverterPalette_18.csproj file.
Speaking English as a French Frog