variant in c#

PoweRoyPoweRoy Member Posts: 43
edited 2006-02-10 in Navision Attain
recently i made a topic cause of a problem to use the GetFieldData, i got a reply on a dutch forum to use the stringbuilder :) now it works BUT

cause its a string, decimal/dates/etc are not showns properly,
strings got a ÿ in front it(code field). numbers got a square in front of it (char 255)(codefield, value's in % are shown as a @(decimal field), and some other bugs
[DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] 
		 private static extern int DBL_GetFieldData(System.Text.StringBuilder dst, int dstsize, int hTable, int record, int fieldnr);
case "lr" : //list records new style
					dTable = new DataTable("List Records");
					int hrecord = DBL_AllocRec(htable);
					
					//Fields(columns) aanmaken
					int afield = 0;
					int i = DBL_NextField(htable,0); //first field
					string type = "";
					while ( i != 0 )
					{
						switch (DBL_FieldType(htable,i))  
						{
							case (35072) : type = "System.String" ; break; //code
							case (11520) : type = "System.String";  break; //text
							case (34048) : type = "System.Boolean"; break; //boolean
							case (34560) : type = "System.Int32"; break; //integer
							case (12800) : type = "System.String"; break; //decimal
							case (35584) : type = "System.String"; break; //option
							case (11798) : type = "System.String"; break; //dateformula
							case (11776) : type = "System.DateTime"; break; //date

							default: type = "System.String"; Console.WriteLine("fieldtype " + DBL_FieldType(htable,i).ToString()  + " niet gevonden."); break;

						}
						dColumn = new DataColumn(Marshal.PtrToStringAnsi(DBL_FieldName(htable,i)),System.Type.GetType(type));
						dTable.Columns.Add(dColumn);
						afield++;
						i = DBL_NextField(htable,i);
					}

					//data erin
					if (DBL_FindRec(htable, hrecord ,"-") ) //eerste record
					{
						System.Text.StringBuilder sbuilder = new System.Text.StringBuilder(100);
						dRow = dTable.NewRow();					 
						int dteller = DBL_NextField(htable,0); //nr van field
						for (int k = 0 ; k < afield ; k++) //van field 0 tot laatste field
						{
							DBL_GetFieldData(sbuilder, sbuilder.MaxCapacity, htable,hrecord,dteller);
							string tdata = sbuilder.ToString();
							if (tdata != "") 
							{
								switch (DBL_FieldType(htable,dteller))  
								{
									case (35072) : dRow[k] = tdata.Substring(1,tdata.Length-1); break; //code
									case (11520) : dRow[k] = tdata; break; //text
									case (34048) : dRow[k] = true;	break; //boolean //nog uitzoeken
									case (34560) : dRow[k] = Convert.ToInt32(tdata,16); break; //integer 													
									case (12800) : dRow[k] = tdata; break;//Convert.ToDecimal(tdata); break; //decimal
									case (35584) : dRow[k] = tdata; break; //option
									case (11798) : dRow[k] = tdata; break; //dateformula
									case (11776) : dRow[k] = Convert.ToDateTime(tdata); break; //date

								}
							}	
							dteller = DBL_NextField(htable,dteller);
						}
						dTable.Rows.Add(dRow);

						while ( DBL_NextRec(htable, hrecord, 1) != 0 ) //elke volgende record
						{
							dRow = dTable.NewRow();					 
							dteller = DBL_NextField(htable,0); 
							for (int k = 0 ; k < afield ; k++)
							{
								DBL_GetFieldData(sbuilder, sbuilder.MaxCapacity, htable,hrecord,dteller);
								string tdata = sbuilder.ToString();
								if (tdata != "")
								{
									switch (DBL_FieldType(htable,dteller))  
									{
										case (35072) : dRow[k] = tdata.Substring(1,tdata.Length-1); break; //code
										case (11520) : dRow[k] = tdata;  break; //text
										case (34048) : dRow[k] = true; break; //boolean //nog uitzoeken
										case (34560) : dRow[k] = Convert.ToInt32(tdata,16); break; //integer 
										case (12800) : dRow[k] = tdata; break;//Convert.ToDecimal(tdata); break; //decimal
										case (35584) : dRow[k] = tdata; break; //option
										case (11798) : dRow[k] = tdata; break; //dateformula
										case (11776) : dRow[k] = Convert.ToDateTime(tdata); break; //date

									}
								}
								dteller = DBL_NextField(htable,dteller);
							}
							dTable.Rows.Add(dRow);
						}										
					}
					DBL_FreeRec(hrecord);
					break;
dont go like huh on the case statements, those were changed each time i debugged :P

the GetFieldData function returns and variant. but if i specify de dllimport so it returns an object. crate the object like "object blaat = new object();
it gives a exception.

u gusy got some ideas/examples how to catch the variant and transform it into an string/date etc?

thanks.
Sign In or Register to comment.