Universal App VS2015 C++ How to convert String^ to float? - win-universal-app

I am using Visual studio 2015 to build an app Universal by C++;
When I want to convert Textbox->Text to float type. I search google but don't have solve. I can't use TryParse or Convert:: or float.parse.
Here is the using of my Mainpage.cpp
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
and I can't use System::. When I write it show a syntax error

Try the code below.
String^ text = "3.14";
std::wstring textToConvert = std::wstring(text->Data());
float value = std::stof(textToConvert);

Related

How can I use the same namespace names in cpp-winrt idl file and a normal/standard cpp class?

The scenario is new and I believe it might be a bug in cpp-winrt module or the vc compiler.
The Problem
1. Create a "windows runtime component" using the cpp-winrt template for universal windows.
2. Note the namespace name defined in the idl file. By default the idl file is named as class.idl.
3. Add a "new standard cpp class" to the project using the class wizard.
4. Put the new class under the same namespace name as defined in the idl file.
5. Build the project.
a. The build should succeed.
6. Use the standard cpp class in the runtime implementation class.
7. Now try to build the project again.
a. The build will fail this time.
The Reason
1. cppwinrt.exe generates source files from the idl file.
2. The runtime implementation class namespace is prefixed by "winrt".
3. The standard cpp class is not prefixed by the namespace "winrt".
4. The vc compiler expects the standard cpp class namespace to start from "winrt"
5. Otherwise the name resolution fails even if you try to use the fully qualified namespace name.
Example runtime idl file
namespace NMLevel1.NMLevel2
{
[default_interface]
runtimeclass Class
{
Class();
void RuntimeMethod1();
Int32 RuntimeMethod2(Int32 arg1);
String RuntimeMethod3(String arg1);
Int32 MyProperty;
}
}
Example standard cpp file
#pragma once
#include <string>
using namespace std;
namespace NMLevel1::NMLevel2
{
class StdCPPClass
{
public:
StdCPPClass();
virtual ~StdCPPClass();
void method1();
int method2(int arg1);
wstring method3(wstring arg1);
};
}
Example runtime class implementation
#include "pch.h"
#include "Class.h"
#include "NMLevel1.NMLevel2.Class.g.cpp"
#include "StdCPPClass.h"
using namespace winrt;
using namespace Windows::Foundation;
namespace winrt::NMLevel1::NMLevel2::implementation
{
void Class::RuntimeMethod1()
{
NMLevel1::NMLevel2::StdCPPClass stdcls;
Uri uri(L"http://aka.ms/cppwinrt");
//printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
printf("RuntimeMethod1(): Hello, %ls!\n", stdcls.method3(uri.AbsoluteUri().c_str()).c_str());
}
}
NMLevel1::NMLevel2::StdCPPClass stdcls; is not allowed without prefixing the namespace with winrt.
Is this a bug or a design limitation that cannot be overcome?
or How can I use the same namespace 'NMLevel1::NMLevel2' in idl and cpp files?
Name lookup in C++ is fairly involved, especially when namespaces get essentially merged through using declarations. To help the compiler identify the name you want, you'll have to be explicit, and use a fully qualified name (as opposed to a qualified name, as in the sample).
You'll have to change
void Class::RuntimeMethod1()
{
NMLevel1::NMLevel2::StdCPPClass stdcls;
// ...
}
to
void Class::RuntimeMethod1()
{
::NMLevel1::NMLevel2::StdCPPClass stdcls;
// ...
}
Note the leading :: scope resolution, which limits lookup to the global scope (or namespaces introduced into the global namespace through a using declaration). You'll find additional information here: Qualified name lookup.

How to use system.drawing in visual studio 2012 express?

I am trying to do image manipulation in c# in windows 7 using VS 2012 express desktop. I made a console app with this code, and its giving errors about Bitmap cannot be found. Does anyone know whats wrong?
I already imported the system.drawing, but still cannot be found...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace ImageSplit
{
class Program
{
static void Main(string[] args)
{
System.Drawing.Rectangle cropRect = new Rectangle(0, 0, target.Width, target.Height);
Bitmap src = Image.FromFile(fileName) as Bitmap;
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using(Graphics g = Graphics.FromImage(target))
{
g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
}
}
}
}
Make sure you include a reference to the System.Drawing assembly in the project. Referencing/importing the namespace is not enough for the linker to find the assembly in question. This is what the error message is referring to by "Are you missing an assembly reference?"
Right-click the project, select "Add Reference..." and choose the appropriate file. This will add a reference to the assembly in the /References virtual project folder.

how to add main method to Visual C++/CLR/Class Library?

Last 10 years I was only using C#/Java so sorry about my simple questions about c++.
Now I've to add one c++ project to my solution. I do not need to be it standalone application, I need it to do some work and to transfer result to my another c# project. So I've created "Visual C++ / CLR / Class Library".
By default such project doesn't contain too much code. Just this:
// CliProject.h file
#pragma once
using namespace System;
namespace CliProject {
public ref class Class1
{
// TODO: Add your methods for this class here.
};
}
// CliProject.cpp file
#include "stdafx.h"
#include "CliProject.h"
Now for debugging I want to add "main" method so I can launch my library as standalone application. How to do that? Should I create one another class or I should use existent classes?
Create a 'CLR Console Application' project with a reference to your library. Or even better, for debugging, use a unit test framework.

ObjectQuery extensions from managed C++/CLI

I'm trying to move a project over to using Entity Framework, but to make it more fun, the project is in C++/CLR.
I've got a query
ObjectQuery<myData::Facility^>^ facQ = myContext->FacilitySet;
and I want to do this
int n = facQ.Count()
But I can't because c++ doesn't recognise extension methods using C# syntax. facQ->Count() doesn't work.
Using C# extension methods from managed C++/CLI shows the answer for user-defined extensions; but in this case, the extension is part of the .NET framework http://msdn.microsoft.com/en-us/library/bb349034%28v=vs.90%29.aspx.
Any ideas?
(I'm using visual studio 2008, and .NET 3.5).
System::Data::Objects::ObjectQuery implements IEnumerable<T>. The Count() method you see in C# is from the System::Linq::Enumerable class.
using namespace System::Linq;
int n = Enumerable::Count(facQ);
Also see this answer, which shows a couple examples of calling other extension methods in that class.

Namespace nesting in C++/CLI

I know nesting of namespaces is allowed in C++/CLI. So my question is if we have something like this:
...
namespace one
{
// blah blah blah
// ...
namespace two
{
// another set of blah blah blah
// ...
}
}
and I need to use some functions and variables in namespace two, do I use:
one::two
or
one.two
Another question is that if I have a statement like
using namespace one;
do I have access to variables and functions in any nested namespaces like
namespace two
I'm asking because, in some programs I have seen (and written), there's something like:
using namespace System;
using namespace System::Text;
using namespace System::IO;
Isn't the System namespace supposed to cover the System::Text and System::IO namespaces?
You need to use
one::two::some_variable_or_function
The . operator is for accessing non-static struct/class members. Static members can then again be accessed through the scope resolution operator ::.
If you only would use using System;, you could access the System::Text functions/variables by
Text::some_function_or_variable
using the parent namespace does not imply importing all sub-namespaces.
Yes, you need use one::two instead of one.two to access symbols in a nested namespace.
If you just using namespace one; you don't automatically have the access to the nested namespace. You'll have to use two::
Example:
namespace one
{
int i;
namespace two
{
int j;
}
}
If you use:
using namespace one;
Your code looks like:
i = 1;
two::j = 2;
If you use:
using namespace one;
using namespace one::two;
Your code looks like:
i = 1; //Compile fails if no "using namespace one".
j = 2;
one::two
or
one.two
You need to use one::two. one.two is the syntax for package access in Java
Another question is that if I have a
statement like
using namespace one;
do I have access to variables and
functions in any nested namespaces
like
namespace two
No, you can use them as two:: instead of one::two::
I'm asking because, in some programs I
have seen (and written), there's
something like:
using namespace System;
using namespace System::Text;
using namespace System::IO;
Isn't the System namespace supposed to
cover the System::Text and System::IO
namespaces?
No, they aren't the same. You have to specify usage of each child namespace.
In general, you use the dot only when you're referring to a member of an instance, and :: everywhere else. So to access stuff in your namespace two, you'd call it one::two::whatever.
As for using namespace System;, it'd import stuff from the System namespace. While System::IO and System::Text are in System, it doesn't import them directly into the current namespace. AFAIK you'd be able to say using namespace System; and then refer to a class within a nested namespace as, say, IO::Stream. But that would get confusing fast, if you use a bunch of namespaces.
1) one::two
2) using a parent namespace doesn't automatically expand any nested namespace.

Resources