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.Globalization;
41 namespace Microsoft.VisualBasic
43 using CompilerServices;
45 [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute]
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 (((Red & 0xFF) << 16) | ((Green & 0xFF) << 8) | ((Blue & 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 [System.Runtime.InteropServices.Optional]
234 [System.ComponentModel.DefaultValue(1)]
240 e = new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", "Array"));
241 throw (ArgumentNullException) ExceptionUtils.VbMakeException(e, 9);
244 e = ExceptionUtils.VbMakeException((Exception) new ArgumentNullException(VBUtils.GetResourceString(9)), 9);
248 throw new ArgumentException("Not array arrived to UBound method of Information class");
251 if(Rank < 1 || Rank > Array.Rank)
252 throw new RankException(Utils.GetResourceString("Argument_InvalidRank1", "Rank"));
254 return Array.GetUpperBound(Rank-1);
257 public static System.Int32 LBound (System.Array Array,
258 [System.Runtime.InteropServices.Optional]
259 [System.ComponentModel.DefaultValue(1)]
265 e = new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", "Array"));
266 throw (ArgumentNullException)ExceptionUtils.VbMakeException(e, 9);
269 if(!IsArray(Array)) {
270 throw new ArgumentException("Not array arrived to LBound method of Information class");
273 if(Rank < 1 || Rank > Array.Rank)
274 throw new RankException(Utils.GetResourceString("Argument_InvalidRank1", "Rank"));
276 return Array.GetLowerBound(Rank-1);
279 public static Microsoft.VisualBasic.ErrObject Err()
281 return ProjectData.Err;
284 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
285 public static System.Int32 Erl ()
287 return ProjectData.Err.Erl;
290 public static System.String TypeName (System.Object VarName)
293 Boolean isArray = false;
298 Type type = VarName.GetType();
301 type = type.GetElementType();
306 switch(Type.GetTypeCode(type)) {
307 case TypeCode.DBNull:
310 case TypeCode.Boolean:
328 case TypeCode.Single:
331 case TypeCode.Double:
334 case TypeCode.Decimal:
337 case TypeCode.DateTime:
340 case TypeCode.String:
348 // the following is commented even with mainsoft
350 // if(type.get_IsCOMObject() && name.equals("__ComObject"))
352 // throw new NotImplementedException("COM Objects unsupported in TypeName of Information");
355 int index = name.IndexOf('+');
357 name = name.Substring(index + 1);
359 if(isArray == true) {
360 int rank = (VarName as Array).Rank;
365 for (int i = 0; i < rank - 1; i++)
369 name = Utils.VBFriendlyNameOfTypeName(name);
374 public static System.String SystemTypeName (System.String VbName)
376 String tmpStr = VbName.Trim().ToLower(CultureInfo.InvariantCulture);
377 if(tmpStr.Equals("object"))
378 return "System.Object";
379 if(tmpStr.Equals("short"))
380 return "System.Int16";
381 if(tmpStr.Equals("integer"))
382 return "System.Int32";
383 if(tmpStr.Equals("single"))
384 return "System.Single";
385 if(tmpStr.Equals("double"))
386 return "System.Double";
387 if(tmpStr.Equals("date"))
388 return "System.DateTime";
389 if(tmpStr.Equals("string"))
390 return "System.String";
391 if(tmpStr.Equals("boolean"))
392 return "System.Boolean";
393 if(tmpStr.Equals("decimal"))
394 return "System.Decimal";
395 if(tmpStr.Equals("byte"))
396 return "System.Byte";
397 if(tmpStr.Equals("char"))
398 return "System.Char";
399 if(tmpStr.Equals("long"))
400 return "System.Int64";
406 public static VariantType VarType(Object VarName)
409 return VariantType.Object;
411 if(VarName is Exception)
412 return VariantType.Error;
414 return varType(VarName.GetType());
417 private static VariantType varType(Type varType)
420 return VariantType.Object;
422 if(varType.IsArray) {
424 Type type = varType.GetElementType();
426 return VariantType.ObjectArray;
428 int elemVal = (int) Information.varType(type);
429 return (VariantType)((int)VariantType.Array | elemVal);
433 varType = Enum.GetUnderlyingType(varType);
436 return VariantType.Empty;
438 switch(Type.GetTypeCode(varType)) {
440 return VariantType.Empty;
441 case TypeCode.DBNull:
442 return VariantType.Null;
443 case TypeCode.Boolean:
444 return VariantType.Boolean;
446 return VariantType.Char;
448 return VariantType.Byte;
450 return VariantType.Short;
452 return VariantType.Integer;
454 return VariantType.Long;
455 case TypeCode.Single:
456 return VariantType.Single;
457 case TypeCode.Double:
458 return VariantType.Double;
459 case TypeCode.Decimal:
460 return VariantType.Decimal;
461 case TypeCode.DateTime:
462 return VariantType.Date;
463 case TypeCode.String:
464 return VariantType.String;
467 if(varType.IsSubclassOf(typeof(System.Exception)))
468 return VariantType.Error;
470 if(varType.IsValueType)
471 return VariantType.UserDefinedType;
473 return VariantType.Object;