Thread: Arrays in Visual Studio.Net 2003

  1. #1
    Registered User
    Join Date
    Aug 2005
    Location
    Pasadena, CA
    Posts
    6

    Arrays in Visual Studio.Net 2003

    Hello everyone

    I am a beginner C++ programmer and I have a difficult problem that came up while using Visual Studio 2003. I wrote a simple blackjack program in console mode with Visual Studio and I finished it with no problem. Being that I wish to make my program graphical, I borrowed the playing cards from the cards.dll that microsoft uses for windows so that I may learn how to write such a program. It became apparent after some investigation to use a windows forms application to port my console program over. It seems that the imagelist object would allow me to draw the bitmaps of the cards to the form with no problem. I wrote a Card class that displays itself in graphics mode with no problem and I was very elated until I started having problems with my code. Using the IDE I generated a windows forms application project and added my code to it. I was having problems when I tried to declare an array that I was using to represent the card deck. In my code I wrote:

    Card deck[52];

    The compiler kept telling me that only _gc or _nogc arrays are allowed, but whenever I used the syntax of the _nogc keyword I got errors. Finnally, I declared my array dynamically, but then I found that my array consisted of only one element. In debugger mode I can only see one array element, is that because is declared dynamically and I only have access to the address of the first element? Please enlighten me!

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh .net arrays oh gosh THE PAIN lol

    i personally used oh classic C style arrays and let it give me warnings

    .net has this weird array __gc set up mumbo jumbo

    though yea i thought that using an array like that for a class or struct would work but guess not i never tried it

    seems like it would work just use a C-style struct/class i found it easier to work with it gives me warnings but it manages to work better?

    or did you make this class .net style or old style that helps too they both seem to handle two different ways?

    .net style the you have that using namespace System non .net you have using namespace std
    hooch

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Pasadena, CA
    Posts
    6

    Smile

    I created the class using two files. Card.h and Card.cpp as far as I know the classes are standard c++. Here is the source for the class.

    The Card.h file contains:

    Code:
    #pragma once
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    enum Suit { clubs, diamonds, spades, hearts };
    class Card
    {
    public:
    	Card(void);
    
    	~Card(void);
    private:
    	int number;  // 2 to 10, jack, queen, king, ace
    	
    	Suit suit;  // clubs, diamonds, hearts, spades
    public:
    	// initialize card
    	void init(int n, Suit s);
    	int GetCardNumber(void);
    	// Return card suit
    	Suit GetCardSuit(void);
    	// Display card
    	void display(Graphics* gobj, ImageList* imageList1, int XPos, int YPos);
    };
    The Card.cpp file contains:

    Code:
    #include "StdAfx.h"
    
    Card::Card(void)
    : number(0)
    {
    }
    
    Card::~Card(void)
    {
    }
    
    
    // initialize card
    void Card::init(int n, Suit s)
    {
    suit = s; 
    number=n;
    }
    
    // Return card number
    int Card::GetCardNumber(void)
    {
    	return number;
    }
    
    // Return card suit
    Suit Card::GetCardSuit(void)
    {
    	return suit;
    }
    
    // // Display card
    void Card::display(Graphics* gobj, ImageList* imageList1, int XPos, int YPos)
    {
    int index = number - 2 + suit * 13;
    
    imageList1->Draw(gobj, XPos, YPos, index);
    
    }
    The problem seems to be when declaring arrays added to the forms.h code that gets generated by Visual Studio.

    When I am using the Card class in the paint method of the forms.h class this way:


    Code:
    Graphics* gr = CreateGraphics();
    
    Card mycard;
    mycard.init(11,hearts);
    mycard.display(gr,imageList1,500,500);
    it works beautifully. But when I try to use an array for the class nothing seems to work as expected.

    the following declaration only creates an array of one element:

    Code:
    static Card* deck = new Card[CardsInDeck];
    where CardsInDeck is 52.

    I have no clue as to what is going on. Does anyone have an idea that I can try?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok that looks like C++.net so just keep in mind its NOT standard unless your only going to use that .net compiler its not going to work elsewhere with that .net framework stuff....since that using namespace System is a .net thing so and so is the ->

    and where you posted you cpp file that looks like it should get in the .h file underneath everything else cause your telling it with that ok what are these objects in card class supposed to do? otherwise to it it has no clue it sees them but with nothing for them to do.

    that should probably fix some of the errors if not at least get the card class working properly

    though im not sure on classes in C++.net never learned that version so could be wrong but thatd be my guess.
    hooch

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Pasadena, CA
    Posts
    6

    Smile

    Hello

    Thanks for your input. I realized that when I am using dot net features I will not be able to recompile with a different compiler.

    I tried to make my code as simple as possible since I am just a beginner, but as you say that dot net thing is not simple. I was just trying to get a shortcut since I did not want to have to deal with the complexities of the device context interface, but obviously, it may be the only way to go. I still believe that there must be a way to accomplish what I am trying to do with dot net, but I am stumped at the moment. I recompiled my code using the bloodshed compiler and I did not have any difficulty by creating a win32 application with it. I know that I can do that with the Visual Studio compiler also, but there seems to be some differences in the code that it generates for the windows main function. I would have to study it before I make a decision on which way to go.

    Still I wish to learn the dot net thing since there seems to be a high demand for programmers that can do dot net. I will try your suggestions, and if I am still not making any progress I will switch to a win32 application and try the device context code. I have some tutorials that may be able to help me with the complexities involved.

    Thanks very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. visual studio 6 and visual studio.net difference
    By gemini_shooter in forum Tech Board
    Replies: 5
    Last Post: 02-04-2006, 01:32 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. visual studio 2003 academic...
    By revelation437 in forum Tech Board
    Replies: 4
    Last Post: 01-01-2004, 08:29 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. Microsoft Visual Studio.net Professional
    By nbo10 in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2003, 12:52 PM