Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c++ Windows Form app: How to grant public access to DataTable object?

c++ Windows Form app: How to grant public access to DataTable object?

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasehelptutorialquestion
17 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 11196121

    Hi All I am wondering if it's possible to create a DataTable object in one button handling event, and use it in another button handling event without getting error C2065: 'Datatable object' : undeclared identifier As a solution of last resort i've made an attempt (see below) which is unsuccessful.

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
    //int row;
    DataTable^db=gcnew DataTable();
    Form1(void)

    Thx

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    You should declare the object in your Form class and initialise it in the constructor or FormLoad event. It is then available to every other event handler or method within that class, whether public or private. This is basic C++ and nothing specific to CLI or Form apps.

    U 1 Reply Last reply
    0
    • L Lost User

      You should declare the object in your Form class and initialise it in the constructor or FormLoad event. It is then available to every other event handler or method within that class, whether public or private. This is basic C++ and nothing specific to CLI or Form apps.

      U Offline
      U Offline
      User 11196121
      wrote on last edited by
      #4

      Cheers Richard. Please very quick question: do you know why the below code compiled but tirggered an error msg box

      Unable to cast object of type System.Data.DataColumn to type System.IConvertible

      lblvol->Text=System::Convert::ToString(sqrt(System::Convert::ToDouble(dbdataset->Columns["DataReturn"])));

      I was just aiming at computing the standard deviation of my return series held in ["DataReturn""]. can't see anything wrong as sqrt () is a built-in function from

      #include "math.h"

      Cheers

      L 1 Reply Last reply
      0
      • D David Crow

        You might receive (better) answers if you asked this in the correct forum.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        U Offline
        U Offline
        User 11196121
        wrote on last edited by
        #5

        alright.cheers.

        1 Reply Last reply
        0
        • U User 11196121

          Cheers Richard. Please very quick question: do you know why the below code compiled but tirggered an error msg box

          Unable to cast object of type System.Data.DataColumn to type System.IConvertible

          lblvol->Text=System::Convert::ToString(sqrt(System::Convert::ToDouble(dbdataset->Columns["DataReturn"])));

          I was just aiming at computing the standard deviation of my return series held in ["DataReturn""]. can't see anything wrong as sqrt () is a built-in function from

          #include "math.h"

          Cheers

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          My first suggestion would be: "don't write compound statements like that". As you can see they are almost impossible to figure out when they go wrong. Use interim values so you can check each stage of the code, like:

          string dataReturn = dbdataset->Columns["DataReturn"]; // assuming this item is a string value
          double doubleValue = System::Convert::ToDouble(dataReturn); // convert to double, but check for errors
          double squareRoot = sqrt(doubleValue);
          lblvol->Text = Convert::ToString(squareRoot);

          Easier and clearer, and simple to step through with the debugger when errors occur.

          U 1 Reply Last reply
          0
          • L Lost User

            My first suggestion would be: "don't write compound statements like that". As you can see they are almost impossible to figure out when they go wrong. Use interim values so you can check each stage of the code, like:

            string dataReturn = dbdataset->Columns["DataReturn"]; // assuming this item is a string value
            double doubleValue = System::Convert::ToDouble(dataReturn); // convert to double, but check for errors
            double squareRoot = sqrt(doubleValue);
            lblvol->Text = Convert::ToString(squareRoot);

            Easier and clearer, and simple to step through with the debugger when errors occur.

            U Offline
            U Offline
            User 11196121
            wrote on last edited by
            #7

            Richard, Thanks for the advice. I will try it and let you know. Any idea why string type does not work from my side as opposed to your post? I am currently going it to see if people have encountered a similar issue. Best,

            L 1 Reply Last reply
            0
            • U User 11196121

              Richard, Thanks for the advice. I will try it and let you know. Any idea why string type does not work from my side as opposed to your post? I am currently going it to see if people have encountered a similar issue. Best,

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              Member 11230372 wrote:

              Any idea why string type does not work from my side

              Not without seeing the context I'm afraid. You can only use a string in the place where a string is valid.

              U 1 Reply Last reply
              0
              • L Lost User

                Member 11230372 wrote:

                Any idea why string type does not work from my side

                Not without seeing the context I'm afraid. You can only use a string in the place where a string is valid.

                U Offline
                U Offline
                User 11196121
                wrote on last edited by
                #9

                might be a missing #include... as something is not logic here. as a matter of fact while typing any type double, float, char they are highlighted in blue therefore you could define any variable anywhere in the code based on those type. however while typing string, the latter does not seem to be recognized at all (not even highlighted). string is considered as plain text and thus unrecognized bizzare... Cheers

                L 1 Reply Last reply
                0
                • U User 11196121

                  might be a missing #include... as something is not logic here. as a matter of fact while typing any type double, float, char they are highlighted in blue therefore you could define any variable anywhere in the code based on those type. however while typing string, the latter does not seem to be recognized at all (not even highlighted). string is considered as plain text and thus unrecognized bizzare... Cheers

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  double, float, char are basic types which are part of the language, while string does not exist, but String (with capital S) is a class. If I spelt it with a lower case s before, then I apologise, as that is C# rather than C++.

                  U 1 Reply Last reply
                  0
                  • L Lost User

                    double, float, char are basic types which are part of the language, while string does not exist, but String (with capital S) is a class. If I spelt it with a lower case s before, then I apologise, as that is C# rather than C++.

                    U Offline
                    U Offline
                    User 11196121
                    wrote on last edited by
                    #11

                    Thanks Richard for the feedback, and no worries.

                    Quote:http://www.cplusplus.com/reference/string/string/?kw=string[^]

                    The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).

                    I will therefore work on your previous post and revert back to you tomorrow. Cheers

                    L 1 Reply Last reply
                    0
                    • U User 11196121

                      Thanks Richard for the feedback, and no worries.

                      Quote:http://www.cplusplus.com/reference/string/string/?kw=string[^]

                      The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).

                      I will therefore work on your previous post and revert back to you tomorrow. Cheers

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Yes, but you are working in C++/CLI (which is why one responder told you to use the correct forum), where the correct class is http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1[^].

                      U 1 Reply Last reply
                      0
                      • L Lost User

                        Yes, but you are working in C++/CLI (which is why one responder told you to use the correct forum), where the correct class is http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1[^].

                        U Offline
                        U Offline
                        User 11196121
                        wrote on last edited by
                        #13

                        Hi Richard Thanks. I've made a simple console app and can confirm that the String class does work and you could create string variables such as string codeproject without problem. However,based on facts it looks like a different story when dealing with c++/cli. Back to the main standard deviation calculation, i think it could not work as a columns can't directly be assigned to a type and treated like a variable (see below). it could have worked in vba by selecting the column range and perform the built-in functions: c++ sqrt() or math::sqrt(). Hence the solution of last resort: iterate through each row and perform the calculation in several steps (get the mean from "DataRet" column first, then carry out ... ) unless a you have a better idea. Cheers

                        Error C2440: 'initializing' : cannot convert from 'System::Data::DataColumn ^' to 'double'

                        DataTable^dbdataset=gcnew DataTable();
                        DataColumn^rt_col=gcnew DataColumn();
                        rt_col->ColumnName = "DataRet";
                        rt_col->DataType=System::Type::GetType("System.Double");
                        dbdataset->Columns->Add(rt_col);

                                         sda->Fill(dbdataset);
                        
                                         double varet = dbdataset->Columns\["DataRet"\]; //already double type, no need System::Convert::ToDouble
                                         double vola = sqrt(varet);
                                         lblvol->Text = Convert::ToString(vola);
                        
                        L 1 Reply Last reply
                        0
                        • U User 11196121

                          Hi Richard Thanks. I've made a simple console app and can confirm that the String class does work and you could create string variables such as string codeproject without problem. However,based on facts it looks like a different story when dealing with c++/cli. Back to the main standard deviation calculation, i think it could not work as a columns can't directly be assigned to a type and treated like a variable (see below). it could have worked in vba by selecting the column range and perform the built-in functions: c++ sqrt() or math::sqrt(). Hence the solution of last resort: iterate through each row and perform the calculation in several steps (get the mean from "DataRet" column first, then carry out ... ) unless a you have a better idea. Cheers

                          Error C2440: 'initializing' : cannot convert from 'System::Data::DataColumn ^' to 'double'

                          DataTable^dbdataset=gcnew DataTable();
                          DataColumn^rt_col=gcnew DataColumn();
                          rt_col->ColumnName = "DataRet";
                          rt_col->DataType=System::Type::GetType("System.Double");
                          dbdataset->Columns->Add(rt_col);

                                           sda->Fill(dbdataset);
                          
                                           double varet = dbdataset->Columns\["DataRet"\]; //already double type, no need System::Convert::ToDouble
                                           double vola = sqrt(varet);
                                           lblvol->Text = Convert::ToString(vola);
                          
                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          Member 11230372 wrote:

                          it looks like a different story when dealing with c++/cli

                          Many things are different if you use C++/CLI, it is not something I have worked with, as C# is so much more intuitive, even though my background is mainly C++.

                          Member 11230372 wrote:

                          Error C2440: 'initializing' : cannot convert from 'System::Data::DataColumn ^' to 'double'

                          Of course you cannot convert a DataColumn to a double, they are totally different things; remember a column is a collection of cells not a single item. You can hold a double value in a DataColumn's cell, but you need to access that with both the row and column indices.

                          U 1 Reply Last reply
                          0
                          • L Lost User

                            Member 11230372 wrote:

                            it looks like a different story when dealing with c++/cli

                            Many things are different if you use C++/CLI, it is not something I have worked with, as C# is so much more intuitive, even though my background is mainly C++.

                            Member 11230372 wrote:

                            Error C2440: 'initializing' : cannot convert from 'System::Data::DataColumn ^' to 'double'

                            Of course you cannot convert a DataColumn to a double, they are totally different things; remember a column is a collection of cells not a single item. You can hold a double value in a DataColumn's cell, but you need to access that with both the row and column indices.

                            U Offline
                            U Offline
                            User 11196121
                            wrote on last edited by
                            #15

                            Thanks Richard. i've therefore coded the calculation of standard deviation (see below). However results differ from Excel from the 3rd decimal point.Do you have any idea, as i have done it meticulously? I remembered someone working on sql server told me that depending on datatype used, computing 1/2 lead to a value close to 0.5 but still not 0.5. Do you think it might be an explanation, here? Cheers

                            int obs;
                            double mu, Ssigma, Svari, sum=0,sumsqr=0;

                                             for (int i=1;i-1<dbdataset->Rows->Count-1;i++){
                                                 dbdataset->Rows\[i-1\]\["DataRet"\]=(System::Convert::ToDouble(dbdataset->Rows\[i-1\]\["Population"\])/System::Convert::ToDouble(dbdataset->Rows\[i\]\["Population"\])-1);
                                                 sum = sum + System::Convert::ToDouble(dbdataset->Rows\[i-1\]\["DataRet"\]);
                                                 obs = (dbdataset->Rows->Count-1);
                                                 mu = sum /obs;
                                                 sumsqr=sumsqr + pow((System::Convert::ToDouble(dbdataset->Rows\[i-1\]\["DataRet"\])-mu),2);
                                                 Svari=sumsqr/(obs-1);
                                                 Ssigma=sqrt(Svari);
                                                 lblvol->Text = Convert::ToString(Ssigma);
                            
                            L 1 Reply Last reply
                            0
                            • U User 11196121

                              Thanks Richard. i've therefore coded the calculation of standard deviation (see below). However results differ from Excel from the 3rd decimal point.Do you have any idea, as i have done it meticulously? I remembered someone working on sql server told me that depending on datatype used, computing 1/2 lead to a value close to 0.5 but still not 0.5. Do you think it might be an explanation, here? Cheers

                              int obs;
                              double mu, Ssigma, Svari, sum=0,sumsqr=0;

                                               for (int i=1;i-1<dbdataset->Rows->Count-1;i++){
                                                   dbdataset->Rows\[i-1\]\["DataRet"\]=(System::Convert::ToDouble(dbdataset->Rows\[i-1\]\["Population"\])/System::Convert::ToDouble(dbdataset->Rows\[i\]\["Population"\])-1);
                                                   sum = sum + System::Convert::ToDouble(dbdataset->Rows\[i-1\]\["DataRet"\]);
                                                   obs = (dbdataset->Rows->Count-1);
                                                   mu = sum /obs;
                                                   sumsqr=sumsqr + pow((System::Convert::ToDouble(dbdataset->Rows\[i-1\]\["DataRet"\])-mu),2);
                                                   Svari=sumsqr/(obs-1);
                                                   Ssigma=sqrt(Svari);
                                                   lblvol->Text = Convert::ToString(Ssigma);
                              
                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #16

                              Sorry, I only ever spent a year doing advanced maths, and that was more than 50 years ago. :sigh:

                              U 1 Reply Last reply
                              0
                              • L Lost User

                                Sorry, I only ever spent a year doing advanced maths, and that was more than 50 years ago. :sigh:

                                U Offline
                                U Offline
                                User 11196121
                                wrote on last edited by
                                #17

                                No worries Richard, and please note that I do thank you for your overall support. Cheers

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups