locked
Variables Declaration RRS feed

  • Question

  • Hi .im new in c++, i dnt want to code CMD application, i want to code Windows forms, with buttons and lists and label and all the other controls, which CMD cant display... 

    now since i cant find ANY books on how to code c++ in the windows form way,,,i decided to do it myself. learn as i do..

    but i found that the code in windows form c++ and CMD c++ are actually as different from eachother as c++ and c#...

    my question... in the windows forms c++, HOW DO I DECLARE VARIABLES

    in vb its:
    dim Mystring as String
    dim Myint as integer

    and CMDc++:
    int Myint
    #include <string>
    string MyString 

    BUT how do i declare variables in windowsforms c++ coz i tried 
    System::String
    Cstring
    String

    None of which wants to declare a simple string variable tobe used later... 

    can someone please refer a list or something that shows the declarations of variables in the windows forms c++...cz not even the f1 help can help me

    thank u

    Wednesday, August 10, 2016 5:36 AM

Answers

  • Hi RickSmit123,

    thanks for posting here.

    >>BUT how do i declare variables in windowsforms c++

    Variable declaration is not related to project type, like windows form or console application. System::String, Cstring, String those types you use have their own header files. You should first include the header file where declare these types, then you could use it in your code.

    Here is a tutorial about Variables and types for you as a reference.

    http://www.cplusplus.com/doc/tutorial/variables/

    Here also has a tutorial about C++ Language you need to learn.

    http://www.cplusplus.com/doc/tutorial/

    >>i dnt want to code CMD application, i want to code Windows forms

    When you finish learning about c++ language, you could refer to this document about Window Form project.

    https://msdn.microsoft.com/en-us/library/dd30h2yb%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Hope these could be help of you.

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    • Marked as answer by RickSmit123 Friday, August 12, 2016 4:21 AM
    Wednesday, August 10, 2016 6:44 AM

All replies

  • Hi RickSmit123,

    thanks for posting here.

    >>BUT how do i declare variables in windowsforms c++

    Variable declaration is not related to project type, like windows form or console application. System::String, Cstring, String those types you use have their own header files. You should first include the header file where declare these types, then you could use it in your code.

    Here is a tutorial about Variables and types for you as a reference.

    http://www.cplusplus.com/doc/tutorial/variables/

    Here also has a tutorial about C++ Language you need to learn.

    http://www.cplusplus.com/doc/tutorial/

    >>i dnt want to code CMD application, i want to code Windows forms

    When you finish learning about c++ language, you could refer to this document about Window Form project.

    https://msdn.microsoft.com/en-us/library/dd30h2yb%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Hope these could be help of you.

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    • Marked as answer by RickSmit123 Friday, August 12, 2016 4:21 AM
    Wednesday, August 10, 2016 6:44 AM
  • okay so you are saying in c++ i cannot use string as in "this is a string"  unless i have made an .h class that puts charachters together to make a string? if thats the case, then fine... seems powerful.... 

    BUT in the tutorials and documentations u gave, and that ive seen on the web,(coz this place is my last resort, i first ALWAYS look for other tutorials and docs online, before coming here), they use the string   #include <string>  NOT   #include string.h   and Cstring

    in my mind it means its then a pre-existing class like the CMD c++ "#using std"    which in the windows forms c++ u DON'T use... so no one can tell me there is not a difference between the two.... in tutorials where people use Cstring or System::string i have NOT yet seen tht people make the seperate class or header file in order to use it... and yet i can't use it, while others can... it seems to only work when u make a console application, where i REFUSE to make a console application because i dont need it now,,, i avoid it like a virus....ive done enough console tutorials when i started to learn c++ bcoz apparently all the c++ books are only about the console... i have yet to discover a book for the c++ forms....

    i realize that i'm new to c++, but every language has its logical way... even the laguage "assembly"....  CMD c++ also(i DID study that up to and including the pointers{well past variables}), but windows forms c++ so far has no logic, im missing something,  and its the variables.... 
    string
    int
    long
    dec
    char
    and all the controls which u can declare the same way. without adding it
    exmple:VB:   dim Mylist as new listbox   (adds a new lisbox with all the events and variables in memory to be used in code as if it was real)
    (that must be possible in c++ also, NO?)

    in your links i did not see how the people made and used a seperate clase in order to use a full length string like in VB or C#,,,, i did see however that your tutorials are for CMD c++, coz it has a MAIN() function... where i use events like load,button click,etc....so if u can do it,,,then show me please.... 


    • Edited by RickSmit123 Wednesday, August 10, 2016 5:21 PM
    Wednesday, August 10, 2016 4:56 PM
  • Because my coding requires me to be able to use full strings... meaning i got to read the string from a listbox selection, and annotate text before and after it...example:
    VB:   Mystring ="My name is " +  listbox.items(0).text
    c++: String TheItem = "Item Clicked" + MyList->GetItemAt(0,0) + " - Total Files: " + MyList->GetItemAt(0,1);

    that i found i cannot do in c++ windows forms,,, i can read the text from the list and pass it to a message box just fine,,, but to make a full string like that, i cannot...it gives errors,,, and i tried all, Cstring,  System::string, #include <string>.... doooooeeesn't work... i'm not an idiot,,, i'm just miss something...and i need help finding it

    hope someone can help....

    P.S   your links have been saved Sera, for study...thank u so much :-) u made studying easier


    • Edited by RickSmit123 Wednesday, August 10, 2016 5:23 PM
    Wednesday, August 10, 2016 5:04 PM
  • Hi .im new in c++, i dnt want to code CMD application, i want to code Windows forms, with buttons and lists and label and all the other controls, which CMD cant display... 

    now since i cant find ANY books on how to code c++ in the windows form way,,,i decided to do it myself. learn as i do..

    but i found that the code in windows form c++ and CMD c++ are actually as different from eachother as c++ and c#...

    my question... in the windows forms c++, HOW DO I DECLARE VARIABLES

    in vb its:
    dim Mystring as String
    dim Myint as integer

    and CMDc++:
    int Myint
    #include <string>
    string MyString 

    BUT how do i declare variables in windowsforms c++ coz i tried 
    System::String
    Cstring
    String

    None of which wants to declare a simple string variable tobe used later... 

    can someone please refer a list or something that shows the declarations of variables in the windows forms c++...cz not even the f1 help can help me

    thank u

    You should note that

    (a) Windows forms in C++ actually uses a language called C++/CLI, which enables use of the .NET library

    (b) Windows Forms in C++ is actually deprecated by Microsoft, and the template for creating a WinForms project in C++ has been removed from recent versions of Visual Studio.

    Rather you might consider:

    1. WinForms in C#

    2. Win32 application using MFC using standard C++

    3. Win32 application using the raw Win32 API and standard C++

    4. Various 3rd party platforms such as QT, WTL and Wxwidgets using standard C++

    5. Universal Windows 10 application using C++/CX

    This said,  it is possible to write WinForms using C++/CLI, and when you do this you can use any of the C++ you learned (or should have learned) for writing console applications (except for console specific stuff like std::cout and std::cin). Variable declarations are done in exactly teh same way.

    But it is often better to use classes that are designed specifically for .NET, for example System::String instead of std::string or std::wstring.


    David Wilkinson | Visual C++ MVP

    • Proposed as answer by RickVBplayer Thursday, August 11, 2016 8:29 AM
    Wednesday, August 10, 2016 7:24 PM
  • Hi david thank you... i found out that VS 2010 can't successfully create a c++cli form,,,but VS2005/2008 can, so i see that u are right and it has been removed... 

    c# i can use but that is ONLY for .net,,,and at the moment i to practice a language that doen't need it, coz i have another project which needs me to code a non- .net language... 

    i have also seen that raw win32 c++ are writen in console c++ but made a form...so i think then that is best... so since u know... will u be able to tell me or show me how to go about starting such a project... 
    like:
    *which VS version? (probably all of them)
    *Which project type?
    *and how to code spicifics to make it debug and make a form like rnning an empty form,,,,  please... 

      thank you for the advice... though it doesnt answer my Q but it lead me searching for a different thing which did give me the answer,,,but only for "string",,,i will see if other things are the same..
    but u still lead me to it, so thanks :-)

    hope u can help with my newsest problem then also,,,the raw win32 


    Thursday, August 11, 2016 8:29 AM
  • Hi david thank you... i found out that VS 2010 can't successfully create a c++cli form,,,but VS2005/2008 can, so i see that u are right and it has been removed...

    c# i can use but that is ONLY for .net,,,and at the moment i to practice a language that doen't need it, coz i have another project which needs me to code a non- .net language...

    i have also seen that raw win32 c++ are writen in console c++ but made a form...so i think then that is best... so since u know... will u be able to tell me or show me how to go about starting such a project...
    like:
    *which VS version? (probably all of them)
    *Which project type?
    *and how to code spicifics to make it debug and make a form like rnning an empty form,,,,  please...

      thank you for the advice... though it doesnt answer my Q but it lead me searching for a different thing which did give me the answer,,,but only for "string",,,i will see if other things are the same..
    but u still lead me to it, so thanks :-)

    hope u can help with my newsest problem then also,,,the raw win32

    If you do not want to use .net then you cannot use WinForms, either in C# or C++/CLI.

    If you want to write a Windows app in C++ without using .net then unfortunately you are stuck with old Microsoft technologies -- raw Win32 API or MFC. For either of these you are best to use Visual Studio 2015 Community Edition, but you can use any other full edition of Visual Studio (the former Express versions do not include MFC).

    The project templates you want are

    Visual C++ -> Win32 -> Win32 Project

    Visual C++ -> MFC -> MFC Application

    Both of these project types use standard C++, so you can declare variables in exactly the same way as in a console application. In either type you can use std::string or (better) std::wstring, and MFC also has the type CString. It is possible to use CString in the raw Win32 API, but there is no real reason to do so.

    Note that both these project types should be compiled using the Unicode character set (this is the default setting in VS2015) which means that all string will be 16-bit wchar_t strings, using the UTF-16 encoding. You should use L"" strings, wchar_t and std::wstring everywhere, rather than "" strings, char and std::string. CString uses wchar_t strings automatically in a Unicode build. Do NOT make the mistake of starting a Windows application using 8-bit MBCS strings -- you will regret it later, believe me.

    Note that the term "form" is not used in the Win32 API or MFC, rather there are dialogs, controls and (more generally) windows.


    David Wilkinson | Visual C++ MVP


    • Proposed as answer by RickVBplayer Thursday, August 11, 2016 11:33 AM
    • Edited by davewilk Thursday, August 11, 2016 2:30 PM
    Thursday, August 11, 2016 10:22 AM
  • Actually, VS2010 was the last version of Visual Studio that included the templates/wizard to create a Windows Form application using C++/CLI.  If you cannot accomplish this then you are probably doing something wrong.

    There is no such thing as "console C++" or "CMD C++".  Refer back to David Wilkinson's concise summary from his earlier message.  From your comments it seems that you are referring to standard C++ by these misnomers.

    I understand your confusion about "string".  There are different contexts in which "string" denotes either an array of characters that are null terminated (i.e, a C string), or an object of type std::string that is from the Standard C++ Library, or an object of type System::String form the .Net framework.

    If you want to write code for the native (raw) Win32 API you will need to understand the syntax of standard C and/or standard C++.  To minimize confusion I suggest you focus your research and reading on those languages and avoid material about C++/CLI, C++/CLX and C#.  You can move on to those after you have a good baseline understanding of standard C and standard C++.

    Thursday, August 11, 2016 10:23 AM
  • oh cool. okay thank you... i will finish my current c++ CLI project then go to Win32 c++... then i will see what i can do... thank you so much :-)
    Thursday, August 11, 2016 11:33 AM
  • Yes thank you... i just didnt know the names,,,so i used the most logic name according to where they run  sorry... thank you for the input... 

    btw, the 2010 dosnt want to create the CLI form  it gives a linker error, even after i searched the right solution online,,, so i just made the c++ CLI on VS 2008 and opened it in VS 2010.... smart huh...hahahahahaha

    thank you for all the input guys. :-) i hope im one day as smart as u all

    Thursday, August 11, 2016 11:35 AM
  • Yes thank you... i just didnt know the names,,,so i used the most logic name according to where they run  sorry... thank you for the input... 

    btw, the 2010 dosnt want to create the CLI form  it gives a linker error, even after i searched the right solution online,,, so i just made the c++ CLI on VS 2008 and opened it in VS 2010.... smart huh...hahahahahaha

    thank you for all the input guys. :-) i hope im one day as smart as u all

    Are you using two different accounts in this thread? If so, please do not do that. It is confusing for the responders, and also changes the set of options available to you. For example, it is not possible for the original poster (OP) to Propose as Answer, only Mark as Answer, yet you appear to have done that by using a different account.

    If one or more responses answers your question, you should be using Mark as Answer.


    David Wilkinson | Visual C++ MVP

    Thursday, August 11, 2016 5:21 PM
  • wow i didnt even notice....sorry....i have two hotmail accounts and they both seem to log in here. coz i once made a different user account when i thought my email was closed... now it goes in here all topsy turby, im sorry,,,i will try and fix it... atleast "RICK"(myname) is present in both....but i will try....thank you.
    Friday, August 12, 2016 4:19 AM