1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how do u call this counstruction

Discussion in 'C, C++, C#' started by Mutikasa, Dec 15, 2012.

  1. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    Code:
    ProfileEntry profile = (ProfileEntry) service.Get(feedUrl);
    whay is the (ProfileEntry) in parenthesis and space between service.Get?
     
  2. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    (ProfileEntry) is casting the returned value of service.Get() into a class/struct of type ProfileEntry. You can see something similar in this C++ code:

    Code:
    #include <iostream>
    
    
    class test
    {
    	public:
    	
    	void set_m_int_1( int args ) { m_int_1 = args; };
    	void set_m_char_3( char args ) { m_char_3 = args; };
    	
    	int get_m_int_1( void ) { return m_int_1; };
    	char get_m_char_3( void ) { return m_char_3; };
    	
    	int m_int_1;
    	char m_char_3;
    };
    
    int main( int argc, char* argv[] )
    {
    	test new_test;
    	new_test.set_m_int_1( 68 );
    	new_test.set_m_char_3( 'a' );
    
    	/*here is where they are casted*/	
    	char local_char	= (char) new_test.get_m_int_1();
    	int local_int	= (int) new_test.get_m_char_3();
    	
    	std::cout << "local_char:\t" << local_char << std::endl << "local_int:\t" << local_int << std::endl;
    
    	return 0;
    }
    
    Output:
    Code:
    soap@hydra:~/tmp$ ./a.out
    local_char:	D
    local_int:	97
    
     
    • Thanks Thanks x 3
  3. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    574
    Likes Received:
    207
    Is space between
    Code:
    (ProfileEntry)
    and
    Code:
    service.Get
    syntax mistake, in c#?
     
  4. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    Nope, that's not syntax mistake and space is optional. That's how the casting is done to cast one type to another in C# & C++.

     
  5. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    The space is optional in C and C++, I assume it is the same in C#. Personally I don't use it in my code as I think it makes the intention more explicit with regards to readability, but that is your call.