5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Francesco Delfino (pluto@tipic.com)
7 // Rafael Teixeira (rafaelteixeirabr@hotmail.com)
9 // (C) 2002 Chris J Breisch
10 // 2003 Tipic, Inc. (http://www.tipic.com)
11 // 2004 Rafael Teixeira
15 // Copyright (c) 2002-2003 Mainsoft Corporation.
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Runtime.InteropServices;
40 using System.Globalization;
41 using Microsoft.VisualBasic.CompilerServices;
43 namespace Microsoft.VisualBasic
46 sealed public class Information {
48 private Information ()
50 //Nothing to do here...
53 private static int [] QBColorTable = { 0, 8388608, 32768, 8421376,
54 128, 8388736, 32896, 12632256,
55 8421504, 16711680, 65280, 16776960,
56 255, 16711935, 65535, 16777215 };
60 public static System.Boolean IsArray (Object VarName)
65 return VarName is Array;
68 public static System.Boolean IsError (Object Expression)
70 if(Expression == null )
73 return Expression is Exception;
76 public static System.Boolean IsReference (Object Expression)
78 return (false == Expression is ValueType);
83 public static System.Boolean IsDate (System.Object Expression)
85 if(Expression == null)
87 else if(Expression is DateTime)
89 else if(Expression is String) {
91 DateTime.Parse((String) Expression);
104 public static System.Boolean IsDBNull (System.Object Expression)
106 if(Expression == null)
109 return Expression is DBNull;
112 public static System.Boolean IsNothing (System.Object Expression)
114 return (Expression == null);
117 public static System.Int32 RGB (int Red, int Green, int Blue)
120 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Red"));
122 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Green"));
124 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Blue"));
133 return (((Blue & 0xFF) << 16) | ((Green & 0xFF) << 8) | ((Red & 0xFF) << 0));
139 public static System.Boolean IsNumeric (System.Object Expression)
142 if(Expression == null || Expression is DateTime)
145 if(Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal ||
146 Expression is Single || Expression is Double || Expression is Boolean)
152 if(Expression is string)
153 Double.Parse(Expression as string);
155 Double.Parse(Expression.ToString());
157 } catch {} // just dismiss errors but return false
162 public static System.Int32 QBColor (System.Int32 Color)
164 if(Color < 0 || Color > 15)
165 throw new ArgumentException(
166 Utils.GetResourceString("Argument_InvalidValue1", "Color"));
167 return QBColorTable[Color];
170 public static String VbTypeName(String UrtName)
176 String tmpName = UrtName.Trim().ToLower(CultureInfo.InvariantCulture);
178 if(tmpName.StartsWith("system."))
179 tmpName = tmpName.Substring(7);
181 if(tmpName.Equals("object"))
183 if(tmpName.Equals("int16"))
185 if(tmpName.Equals("clrint16"))
187 if(tmpName.Equals("int32"))
189 if(tmpName.Equals("clrint32"))
191 if(tmpName.Equals("int64"))
193 if(tmpName.Equals("clrint64"))
195 if(tmpName.Equals("clrsingle"))
197 if(tmpName.Equals("single"))
199 if(tmpName.Equals("double"))
201 if(tmpName.Equals("clrdouble"))
203 if(tmpName.Equals("boolean"))
205 if(tmpName.Equals("clrboolean"))
207 if(tmpName.Equals("char"))
209 if(tmpName.Equals("clrchar"))
211 if(tmpName.Equals("string"))
213 if(tmpName.Equals("java.lang.string"))
215 if(tmpName.Equals("byte"))
217 if(tmpName.Equals("clrbyte"))
219 if(tmpName.Equals("decimal"))
221 if(tmpName.Equals("datetime"))
227 internal static String TypeNameOfCOMObject (Object obj, Boolean flag)
229 throw new NotImplementedException("Method Microsoft.VisualBasic.Information.TypeNameOfCOMObject() is not supported");
232 public static System.Int32 UBound (System.Array Array,
233 [Optional, __DefaultArgumentValue(1)]
239 e = new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", "Array"));
240 throw (ArgumentNullException) ExceptionUtils.VbMakeException(e, 9);
243 e = ExceptionUtils.VbMakeException((Exception) new ArgumentNullException(VBUtils.GetResourceString(9)), 9);
247 throw new ArgumentException("Not array arrived to UBound method of Information class");
250 if(Rank < 1 || Rank > Array.Rank)
251 throw new RankException(Utils.GetResourceString("Argument_InvalidRank1", "Rank"));
253 return Array.GetUpperBound(Rank-1);
256 public static System.Int32 LBound (System.Array Array,
257 [Optional, __DefaultArgumentValue(1)]
263 e = new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", "Array"));
264 throw (ArgumentNullException)ExceptionUtils.VbMakeException(e, 9);
267 if(!IsArray(Array)) {
268 throw new ArgumentException("Not array arrived to LBound method of Information class");
271 if(Rank < 1 || Rank > Array.Rank)
272 throw new RankException(Utils.GetResourceString("Argument_InvalidRank1", "Rank"));
274 return Array.GetLowerBound(Rank-1);
277 public static Microsoft.VisualBasic.ErrObject Err()
279 return ProjectData.Err;
282 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
283 public static System.Int32 Erl ()
285 return ProjectData.Err.Erl;
288 public static System.String TypeName (System.Object VarName)
291 Boolean isArray = false;
296 Type type = VarName.GetType();
299 type = type.GetElementType();
304 switch(Type.GetTypeCode(type)) {
305 case TypeCode.DBNull:
308 case TypeCode.Boolean:
326 case TypeCode.Single:
329 case TypeCode.Double:
332 case TypeCode.Decimal:
335 case TypeCode.DateTime:
338 case TypeCode.String:
346 // the following is commented even with mainsoft
348 // if(type.get_IsCOMObject() && name.equals("__ComObject"))
350 // throw new NotImplementedException("COM Objects unsupported in TypeName of Information");
353 int index = name.IndexOf('+');
355 name = name.Substring(index + 1);
357 if(isArray == true) {
358 int rank = (VarName as Array).Rank;
363 for (int i = 0; i < rank - 1; i++)
367 name = Utils.VBFriendlyNameOfTypeName(name);
372 public static System.String SystemTypeName (System.String VbName)
374 String tmpStr = VbName.Trim().ToLower(CultureInfo.InvariantCulture);
375 if(tmpStr.Equals("object"))
376 return "System.Object";
377 if(tmpStr.Equals("short"))
378 return "System.Int16";
379 if(tmpStr.Equals("integer"))
380 return "System.Int32";
381 if(tmpStr.Equals("single"))
382 return "System.Single";
383 if(tmpStr.Equals("double"))
384 return "System.Double";
385 if(tmpStr.Equals("date"))
386 return "System.DateTime";
387 if(tmpStr.Equals("string"))
388 return "System.String";
389 if(tmpStr.Equals("boolean"))
390 return "System.Boolean";
391 if(tmpStr.Equals("decimal"))
392 return "System.Decimal";
393 if(tmpStr.Equals("byte"))
394 return "System.Byte";
395 if(tmpStr.Equals("char"))
396 return "System.Char";
397 if(tmpStr.Equals("long"))
398 return "System.Int64";
404 public static VariantType VarType(Object VarName)
407 return VariantType.Object;
409 if(VarName is Exception)
410 return VariantType.Error;
412 return varType(VarName.GetType());
415 private static VariantType varType(Type varType)
418 return VariantType.Object;
420 if(varType.IsArray) {
422 Type type = varType.GetElementType();
424 return VariantType.ObjectArray;
426 int elemVal = (int) Information.varType(type);
427 return (VariantType)((int)VariantType.Array | elemVal);
431 varType = Enum.GetUnderlyingType(varType);
434 return VariantType.Empty;
436 switch(Type.GetTypeCode(varType)) {
438 return VariantType.Empty;
439 case TypeCode.DBNull:
440 return VariantType.Null;
441 case TypeCode.Boolean:
442 return VariantType.Boolean;
444 return VariantType.Char;
446 return VariantType.Byte;
448 return VariantType.Short;
450 return VariantType.Integer;
452 return VariantType.Long;
453 case TypeCode.Single:
454 return VariantType.Single;
455 case TypeCode.Double:
456 return VariantType.Double;
457 case TypeCode.Decimal:
458 return VariantType.Decimal;
459 case TypeCode.DateTime:
460 return VariantType.Date;
461 case TypeCode.String:
462 return VariantType.String;
465 if(varType.IsSubclassOf(typeof(System.Exception)))
466 return VariantType.Error;
468 if(varType.IsValueType)
469 return VariantType.UserDefinedType;
471 return VariantType.Object;