Author Topic: My conversion from string to double  (Read 8095 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

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #15 on: May 12, 2015, 06:48:04 AM »
Hi gile

There is not any option in Add -> existing Project and the other option that is Existing item can find the project .


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #16 on: May 12, 2015, 07:08:36 AM »
Right click the solution rather than the project (you can add a project to a solution, not to another project).
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #17 on: May 12, 2015, 07:30:49 AM »
I am sorry , my mistake with the name of icons and buttons because I am still searching for my way in that dark and huge world of codes to have a step on its deck to start and you guys helped me a lot .

Many thanks for your great work and time .

Finally I got it loaded as you have advised me to do , but I see no difference in adding this palette to the first one , both work the same unless I am missing something important here .

Keep the great work gile , you are very famous in the world of AutoCAD programming and lots of friends of mine reading your French book in Autolisp and they very happy with it .

Thank you .  :-)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: My conversion from string to double
« Reply #18 on: May 12, 2015, 09:30:38 AM »
and lots of friends of mine reading your French book in Autolisp
English version?
I'd buy it.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #19 on: May 12, 2015, 10:55:48 AM »
English version?
I'd buy it.

It's free !!!

But in French...
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #20 on: May 12, 2015, 12:48:39 PM »
Finally I got it loaded as you have advised me to do , but I see no difference in adding this palette to the first one , both work the same unless I am missing something important here .

Sorry if I didn't clearly explain this.
The ConverterPalette_18 project was added to the solution to target AutoCAD versions prior to 2013.

Assuming the original ConverterPalette projects targets AutoCAD 2013 libraries, the generated assembly will work with A2013 to 2016 but not with prior version due to the 2013 "big split" in the AutoCAD .NET libraries.
To resolve this compatibilty issue, the ConverterPalette_18 project have been added to the solution. It references AutoCAD libraries of a prior to 2013 version (A2012 for example which is the first version supporting the Autoloader mechanism, another feature shown by this sample).
The ConverterPalette_18 project has the same folder structure as the original project which shares all source code and resources with it so that there's only one source to maintain for two different built projects. As you can see, in the ConverterPalette_18 project folders there're only links to the original ConverterPalette project source files.
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: My conversion from string to double
« Reply #21 on: May 13, 2015, 01:17:56 AM »
English version?
I'd buy it.

It's free !!!

But in French...
Probably worth learning french, would help picking up girls also I would imagine.


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: My conversion from string to double
« Reply #22 on: May 13, 2015, 01:24:46 AM »


But in French...
Probably worth learning french, would help picking up girls also I would imagine.

And swearing sounds a little more graceful  ...
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #23 on: May 13, 2015, 01:42:51 AM »
Finally I got it loaded as you have advised me to do , but I see no difference in adding this palette to the first one , both work the same unless I am missing something important here .

Sorry if I didn't clearly explain this.
The ConverterPalette_18 project was added to the solution to target AutoCAD versions prior to 2013.

Assuming the original ConverterPalette projects targets AutoCAD 2013 libraries, the generated assembly will work with A2013 to 2016 but not with prior version due to the 2013 "big split" in the AutoCAD .NET libraries.
To resolve this compatibilty issue, the ConverterPalette_18 project have been added to the solution. It references AutoCAD libraries of a prior to 2013 version (A2012 for example which is the first version supporting the Autoloader mechanism, another feature shown by this sample).
The ConverterPalette_18 project has the same folder structure as the original project which shares all source code and resources with it so that there's only one source to maintain for two different built projects. As you can see, in the ConverterPalette_18 project folders there're only links to the original ConverterPalette project source files.

Thank you for taking the time to explain the differences , it is very clear to me now .

Many thanks

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #24 on: May 13, 2015, 02:22:29 AM »
Hi gile

A question come up to mind about loading a project in different Autocad releases .  :-)

My current program should work on all Autocad releases start from 2013 up to latest version , correct ?

Suppose that I want to run my program in AutoCAD 2012 or before , how to make it working with these old releases ?

Many thanks.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My conversion from string to double
« Reply #25 on: May 13, 2015, 04:09:51 AM »
This is exactly the purpose of my upper description.

The ConverterPalette_18 project have to reference AutoCAD 2012 libraries (acdbmgd.dll and acmgd.dll from ObjectARX 2012 or AutoCAD 2012 install folder).
The ConverterPalette_18 project have also to target the corresponding Framework (4.0 for AutoCAD 2012).
The ConverterPalette_18 project must have the same folder structure as the original one but each folder contains links to the source files in the original project (right click on a folder > Add > Existing item... > browse to the corresponding folder in the original project > select the files and click "Add as link" in the Add popup).
This way you have the code source files really exists only once and are shared by the two projects. When you build the solution, both projects generates their own assembly. So, for A2012 you have to load the ConverterPalette_18.dll and for A2013+ the ConverterPalette_19.dll.

If you have a look to each project properties, you'll see that with the Release build generated assemblies are automatically directed to the ConverterPalette.bundle\Contents folder.
If you use the Autoloader mechanism with the ConverterPalette.bundle folder, the PackageContents.xml file takes care to load the right assembly for the target version.


Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #26 on: May 13, 2015, 04:32:16 AM »
Great ,

I just wanted to know how to make my program working with Autocad releases 2012 or before, when I build the program to have the .dll file to send to friends to use ?
Should I download the ObjectARX 2012 and make a reference to them to be able to run the program on older releases than Acad 2012 ?
Would the Framework number should be changed accordingly ?

I am sorry for asking you a lot :-)

Regards

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: My conversion from string to double
« Reply #27 on: May 13, 2015, 05:01:50 AM »
I just wanted to know how to make my program working with Autocad releases 2012 or before, when I build the program to have the .dll file to send to friends to use ?
Sometimes the quality of the software can turn friends into enemies... :) Therefore qualitatively test your code before giving it to your friends.

Quote from: Andrey Bushman
AutoCAD 2009 and newer versions are interesting for me. Therefore usually I compile the same code sources for AutoCAD 2009, 2013 and 2015. These assemblies can be loaded successfully in any AutoCAD newer than 2008. Thus it is necessary to create three projects in a solution. One of them has original code source files, other two projects has the links to these code source files. Three projects is not a problem. But I want to test my code, because quality my software is important for me. So, it is necessary to add three additional projects with unit tests.

But can I be 100% sure what my code will works fine in AutoCAD 2010, 2011, 2012, 2014 and 2016 without testing in these AutoCAD versions? No, I can't and nobody can't. Therefore it is necessary to create the testing  projects for each AutoCAD newer than 2008. At this case my projects count is 3 + 8 = 11. Oh, it is big count... if it is required copy these projects to other computer, which has own locations of SDK (not the same like mine), then I must update all referenses of SDK manually. It is tiresome operation. Of course, as the decission of this problem it is possible to store local copies of the necessary DLL SDK files in each project: at this case your project will be portable [between computers].

I want to have two projects only [an extension and its unit tests] and compile it for all necessary AutoCAD versions through "a single click of mouse".  I want to have opportunity of debugging [breackpoints, etc] my code in any AutoCAD version newer than 2008 and quickly switching between them. Also, compilation result must to have a "magic button" for my tests launching in each AutoCAD version [on the local or remote machine] and getting the testing results (summary and individually) in HTML format. Besides, I want to have opportunity to compile and launch unit tests without any changes for different Testing Frameworks: Gallio and NUnit.

Should I download the ObjectARX 2012 and make a reference to them to be able to run the program on older releases than Acad 2012 ?
Would the Framework number should be changed accordingly ?
Each AutoCAD has own SDK version and target .NET Framework. About compatibility of AutoCAD and .Net Framework versions you can read here (you can translate it Russian to English through any online translator). SDK old versions you can download here. Last three versions SDK you can download from the Autodesk site. Often your extensions can be launched in other AutoCAD versions which can use the same .NET Framework version. For example, my extensions which are compiled for AutoCAD 2009 work fine for AutoCAD 2010, 2011 and 2012 (the same DLL file). Big changes was done in AutoCAD 2013.

Advice: be not lazy for reading the documentation and using of Google before your questions asking.
« Last Edit: May 13, 2015, 05:47:43 AM by Andrey Bushman »

Coder

  • Swamp Rat
  • Posts: 827
Re: My conversion from string to double
« Reply #28 on: May 13, 2015, 07:57:35 AM »
Hello Andrey Bushman

Frankly , I am lost with your reply , I am sorry .  :-(