|
I want to convert an object to a string and if it is a float, double, etc,,, then I would want to set the precision like I can with a printf statement.
What would be an efficient way to test if a System::Object is some decimal type and then set the precision? Thanks
//I've tried the following, but it does not work properly
switch (System::Type::GetTypeCode(columnYValues[n]->GetType())) {
case System::TypeCode::Decimal:
case System::TypeCode::Double:
case System::TypeCode::Single:
X=System::String::Format( "{0:f15}", columnYValues[n]->ToString() );
break;
default:
X = columnYValues[n]->ToString();
}
I am trying to create pivot tables using decimal headers and half of the time it does not work because of C++ .NET or ADO .NET's string conversions and zero tolerance.
modified on Friday, April 9, 2010 2:38 AM
|
|
|
|
|
try with the following:
System::Convert::ToInt32
System::Convert::ToDouble
System::Convert::To......
|
|
|
|
|
I'm not sure if this is possible, since I haven't been able to find much of anything on it, but I'm looking to call a control's event handlers programmatically. What I have is an application with multiple windows. Across the top of the app in a tool bar and one of those buttons, lets say copy, must preform that action on whichever window is currently active. They only way I can think to do this (as for various reasons the only way I can get these windows is as an instance of System::Windows::Forms::Control) is to trigger its Key Pressed event handler with 'Ctrl+C', but I can't seem to get access to it. Is this even possible? And if so how?
Thanks!
|
|
|
|
|
So you press the button and want for example that in windows#1 it copy something, in windows#2 it copy something and so on....
if the toolbar is the 'creator' of the windows# you can use a list to have the pointer of all of the window you have created....then you can loop through the list and see which windows is active, and then call the specified function on that window....do i understand your problem?
|
|
|
|
|
Right, if the button is pushed the copy event is called in whatever window is currently active. I have the a active window part already, from the tool bar I have an 'active Window' pointer, so thats not a problem, but since that pointer is just a System::Windows::Forms::Control pointer, I don't have access to any of the controls specially written "Copy" functions, just the standard Forms::Control functions. All of the controls which have some sort of copy functionality already can do it through "Ctrl+C" so I wanted to know if I could programmatically trigger its KeyPress event handler to use their copy functionality. I'm just not sure how to trigger an event like keypress via code.
|
|
|
|
|
loaak at
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx[^]
also...you black boxes give you the possibilities only to use the "control+c" key or they provide also some public function to do the operations? if they give also public function you can always try to static_cast or dynamic_cast the Control pointer...if this is your case this is far better than trigger the ctr+c command
|
|
|
|
|
Hi,
IMO you should not attempt to call an event handler explicitly; one reason is you would have to provide fake parameters, sender wouldn't be a problem, the particular EventArgs may prove more difficult to fake. Unless of course you're absolutely sure the handler doesn't (and never will) use its parameters.
There are two alternatives I would recommend:
1. some events can be triggered by calling an appropriate method, e.g. Button.PerformClick().
2. when the functionality of an event handler is also useful outside the normel event handling, refactor it into a separate method, call that from the event handler, and from anywhere else you'd like to call it.
|
|
|
|
|
Thanks for the reply! My problem I have with you're second suggestion is that some of the windows contain third party controls, and are basically black boxes, so I cannot move that functionality into a seperate method. You're first suggestion seems promising, is there a way to do something like that, but with key stokes instead of Button.PerformClick()? Thanks!
|
|
|
|
|
yes, you can send key strokes and mouse actions to any window you like, whether in your own app or another one. However may be tricky, and error prone; the user (assuming there is one) may well interfere with your automation (or vice versa), so I do not recommend it in general.
If you must, there is the SendKeys class; its SendWait() method is useful. What I have been doing for mouse automation mostly is based on Win32 functions such as SetCursorPos() and SendInput(), both in user32.dll
WARNING: localization changes the shortcut letters that can be used to steer menu's and buttons.
|
|
|
|
|
We have been bringing our old C++ code that uses MFC into the .NET world by compiling it into a mixed-mode assembly. We have found that when this assembly is loaded in an AppDomain e.g. by NUnit or by ASP .NET, the dll is not unloaded when the AppDomain is unloaded. The host application can load the dll many times, and may eventually run out of memory.
I have found that this is due to static data. If I make a simple mixed mode assembly with no static data, or with a static object with a default constructor, the dll is unloaded correctly. If the object has a non-trivial constructor, then the dll is not unloaded.
Even the simplest MFC dll has a static CWinApp derived object, so I don't think it would be possible to remove this kind of static data from our mixed-mode dll without a removing its dependency on MFC (a task that is too big to contemplate at the moment). Is there any other way to force the mixed-mode dll to be freed when the AppDomain is unloaded?
|
|
|
|
|
Hi,
I am able to call the funtion successfully, when linked the dll implicitly.
But when I am linking explicitly using LoadLibrary and GetProcAddress, GetProcAddress returning NULL value.
what mightbe the problem, I am using the same function name as I have used while implicit linking.
regards,
Charan
|
|
|
|
|
Are you saying it works when you link statically? Have you exported the functions that are eligible to call?
Best wishes,
Navaneeth
|
|
|
|
|
I'm quite new to programming C++ and I ran into a problem. I'm working on a program in which some objects are being used by different functions, in separate files. I created the objects in a header file and can access them from a source file. However, I would like to have an instance of the object available for access from all of my functions. How can I create such objects / instances?
Any help is highly appreciated.
|
|
|
|
|
I'm not sure I fully understand your question, but the usual way of doing this is to pass a reference to the object to each function that requires it. Making objects globally visible is generally less safe. For example:
int main()
{
CType* object = new CType();
BOOL result = MyFunc(object);
}
BOOL MyFunc(CType* anObject)
{
int rc = OtherStuff(anObject);
}
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Thanks for the quick answer Richard!
I tried to do this, but the problem I ran into is the following:
I have a sourcefile "program.cpp" with header "program.h" in which my classes are defined. I want to pass an object from "program.cpp" to "function1.cpp", by:
double^ function1result = function1(CType^ object)
In function1 I should now write the function as:
double^ function1(CType^ object){}
When I do that, I get an "undeclared identifyer" error. When I include the "program.h" header where the class is defined I get a "type redefinition" error. So my question is how to pass these objects in a way that such errors are avoided.
|
|
|
|
|
Arjen Tjallema wrote: When I include the "program.h" header where the class is defined I get a "type redefinition" error.
This means you are defining the type (presumably CType ) in more than one place in your source code. Without seeing more of your header and source file it's difficult to be more explicit.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Thanks again! I'll put some bits of my code here to clarify my question.
The "program.h" header contains a class clGlobals:
public ref class clGlobals
{
public:
double^ airDensity;
double^ waterDensity;
double^ gravitation;
};
In my "program.cpp" source I create an instance, put some values in it and (try to) call function1:
clGlobals^ globals = gcnew clGlobals();
globals->airDensity = Convert::ToDouble(1.025);
globals->waterDensity = Convert::ToDouble(1025);
globals->gravitation = Convert::ToDouble(9.81);
double^ load = function1();
The "function1.h" header contains a declaration of the function:
double^ load(clGlobals^ global);
And the "function1.cpp" source contains the actual function:
double^ load(clGlobals^ global)
{
return load;
}
I hope this clarifies my question.
|
|
|
|
|
I don't quite get some of this syntax. What does function1() do and where is it defined? Why does function load() not return a double, or does it?
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
O, excuse me, I made a mistake in my previous post.
The "function1.h" header code should be:
double^ function1(clGlobals^ global);
And the "function1.cpp" source code should be:
double^ function1(clGlobals^ global)
{
double^ load;
return load;
}
I hope it is understandable now.
|
|
|
|
|
Arjen Tjallema wrote: I hope it is understandable now.
Yes, and after correcting this line from:
double^ load = function1();
to:
double^ load = function1(globals);
it compiles and runs fine. If you still have a problem then it is not within this code.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
That's strange, I get the following error when trying to compile:
function1.h(1) : error C2065: 'clGlobals' : undeclared identifier
I have put the four files in a new project to isolate it from the rest of my code, but still I get this error message. Do yo have any clue where to look for the problem?
|
|
|
|
|
The best way to resolve this is to add the following lines:
#include "program.h"
#pragma once
at the beginning of function.h . And add:
#pragma once
at the beginning of program.h .
This should ensure that the class definition for clGlobals is found in any file that includes function.h . Also the #pragma once statements ensure that the header files are processed once only per compilation unit, even if they are found in #include statements more than once.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
That indeed solves the problem.
Thanks a lot for your help!
|
|
|
|
|
Arjen Tjallema wrote: Thanks a lot for your help!
You're welcome; it's good to find the solution occasionally!
txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Hi,
I have developed a small application. But it gives the above error
It is declared in another class named "CPassword"
SqlConnection ^connect = gcnew SqlConnection();
connect->ConnectionString = ConfigurationManager::ConnectionStrings["SQLConnection"]->ConnectionString;;
SqlCommand ^cmd = gcnew SqlCommand();
cmd->CommandType = CommandType::StoredProcedure;
cmd->CommandText = "GetUserPassword";
DataSet ^dtSet = gcnew DataSet();
try
{
connect->Open();
SqlDataAdapter ^adp = gcnew SqlDataAdapter();
adp->SelectCommand = cmd;
cmd->Parameters->Add(gcnew SqlParameter("@user_id",SqlDbType::VarChar));
cmd->Parameters["@user_id"]->Value = userID;
adp->Fill(dtSet, "Password");
return dtSet;
}
And I am calling this function from another class
CPassword ^objPassword = gcnew CPassword();
DataSet ^dtSet = gcnew DataSet();
dtSet = objPassword->fn_ReturnPassword(txtUserId->Text);
Plese help....
Thanks to all
|
|
|
|