c++ - Cannot access public static method -


i have class

#pragma once namespace cmt{ namespace sql=system::data::sqlclient; public ref class db { public:db(void){} public: static sql::sqlconnection sc(){             system::string cstring="data source=192.168.0.139\\cedfit; "+                 "initial catalog=cedfitdb; user id=client; password=cedfit";             sql::sqlconnection sc=new sql::sqlconnection(cstring);             return sc;         }  }; } 

now when go form 1 load event cannot access db's sc() method why?

i tried make in form1 load event code:

system::data::sqlconnection mycon=db::sc(); mycon.open();//i tried mycon->open() , mycon::open() 

why doesnt work? why can't program recognize "open()"? when put #include "db.h" on cmt.cpp says cannot covert system::data::sqlclient::sqlconnection int sure returning sqlconnection why?

you have many errors when trying use reference types in .net framework via c++/cli.

-- need use ^ when referring .net reference types in c++/cli. also, when allocating memory reference types, need use gcnew instead of new. see changes below:

static sql::sqlconnection^ sc() {     system::string^ cstring = "data source=" + "asdfasdf";     sql::sqlconnection^ sc = gcnew sql::sqlconnection(cstring);      return sc; } 

-- problem again when trying use method in code. additionally, didn't specify correct namespace sqlconnection cmt::db::sc.

int main(array<system::string ^> ^args) {     system::data::sqlclient::sqlconnection^ mycon = cmt::db::sc();     mycon->open();      return 0; } 

as side note, there particular reason need c++/cli instead of c#? there cases c++/cli beneficial, can unnecessarily complicated if not attempting interop native code. thought.

full code:

db.h

#pragma once  namespace cmt { namespace sql = system::data::sqlclient;  ref class db { public:      db(void)     {     }      static sql::sqlconnection^ sc()     {         system::string^ cstring = "whatever";         sql::sqlconnection^ sc = gcnew sql::sqlconnection(cstring);          return sc;     }  };  } 

main.cpp

// consoleapplication1.cpp : main project file.  #include "stdafx.h" #include "db.h"  using namespace system;  int main(array<system::string ^> ^args) {     system::data::sqlclient::sqlconnection^ mycon = cmt::db::sc();     mycon->open();      return 0; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -