Click here to Skip to main content
15,902,863 members
Home / Discussions / C#
   

C#

 
QuestionIs there a better way to call Same Methods on different objects ? Pin
Emanuele Bonin23-Dec-15 0:03
Emanuele Bonin23-Dec-15 0:03 
AnswerRe: Is there a better way to call Same Methods on different objects ? Pin
Richard MacCutchan23-Dec-15 0:30
mveRichard MacCutchan23-Dec-15 0:30 
AnswerRe: Is there a better way to call Same Methods on different objects ? Pin
Eddy Vluggen23-Dec-15 1:49
professionalEddy Vluggen23-Dec-15 1:49 
AnswerRe: Is there a better way to call Same Methods on different objects ? Pin
BillWoodruff23-Dec-15 5:04
professionalBillWoodruff23-Dec-15 5:04 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Eddy Vluggen23-Dec-15 6:34
professionalEddy Vluggen23-Dec-15 6:34 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
BillWoodruff23-Dec-15 19:44
professionalBillWoodruff23-Dec-15 19:44 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Eddy Vluggen24-Dec-15 1:30
professionalEddy Vluggen24-Dec-15 1:30 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Emanuele Bonin24-Dec-15 9:51
Emanuele Bonin24-Dec-15 9:51 
The "Type" variable in my piece of code is symply an enumerator that assume two values observing the extension of a filename (not present in the code posted) used in fs that is a filestream.
Sorry for unusable code but when i posted the question i was in the first approach to the problem.
Thanx to all for suggestions but in the end i choiced a simply (much simply??) way to use the switch.
In the Visual Fox Pro (the my previous language) i can call a generic method from an object that can be anytype ... there is no strong type checking... the more similar way is use the Pinvoke ... but i don't like so much .. even if in some cases i used it. Initially i tryed to use a simple object of type 'object' ... but the use of switch was necessary with many cast ... so the partial initial (reaing only), but useful, result of my class for managing xls or xlsx with NPOI is :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO; 

using NPOI.XSSF.UserModel; // XSSFWorkbook, XSSFSheet
using NPOI.HSSF.UserModel; // HSSFWorkbook, HSSFSheet XLS
using NPOI.SS.UserModel;

namespace XLSManagement
{
	// Classe per la gestione dei file excel con NPOI
	public class NPOIWrapper {

		enum XLSType {XLS, XLSX}

		public string FileName { get; set;}
		private int _CurrentSheetIndex = 0;
		public int CurrentSheetIndex { get {return _CurrentSheetIndex; } set {_CurrentSheetIndex = value; } }

		public NPOIWrapper() { }
		public NPOIWrapper(string cFileName) {
			this.FileName = cFileName;
			ReadXLS();
		}

		private string[] _Sheet;
		public string[] Sheet {get { return _Sheet;} }

		private int _SheetCount = 0;
		public int SheetCount {get { return _SheetCount;} }

		XLSType Type;

        XSSFWorkbook	Xwb;
        HSSFWorkbook	Hwb;
        ISheet[]		sh;

		public bool ReadXLS(string XLSFileName = "") {
			bool Ret = true;
			string XLSName, Extension;
			StringBuilder Sheets = new StringBuilder();

			FileStream fs;
			XLSName = String.IsNullOrEmpty(XLSFileName)?this.FileName:XLSFileName; 
			Extension = Path.GetExtension(XLSName).Substring(1).ToUpper();
			switch (Extension) {
				case "XLS":
					Type = XLSType.XLS;
					break;
				case "XLSX":
					Type = XLSType.XLSX;
					break;
				default:
					Ret = false;
					break;
			}
			if(Ret) {
				fs = new FileStream(XLSName, FileMode.Open, FileAccess.Read);
				if (fs==null) Ret = false;
				else {
					switch (Type) {
						case XLSType.XLS:
							try {
								Hwb = new HSSFWorkbook(fs);
							} catch (Exception ex){ MessageBox.Show("Lettura File XLSX: \n" + ex.Message);}
							_SheetCount = Hwb.Count;
							sh = new ISheet[_SheetCount];
							for (int i = 0; i < _SheetCount; i++) {
								sh[i] = Hwb.GetSheetAt(i);
								Sheets.Append("\n" + sh[i].SheetName);
							}											
							break;
						case XLSType.XLSX:
							try {
								Xwb = new XSSFWorkbook(fs);
							} catch (Exception ex){ MessageBox.Show("Lettura File XLSX: \n" + ex.Message);}
							_SheetCount = Xwb.Count;
							sh = new ISheet[_SheetCount];
							for (int i = 0; i < _SheetCount; i++) {
								sh[i] = Xwb.GetSheetAt(i);
								Sheets.Append("\n" + sh[i].SheetName);
							}											
							break;
						default:
							break;
					}
					this._Sheet = Sheets.ToString().Substring(1).Split('\n');

				}
			}
			return Ret;
		}

		public ICell GetCell(int Row, int Col) {
			ICell Ret;
			Ret = GetCell(Row, Col, this.CurrentSheetIndex);
			return Ret;
		}

		public ICell GetCell(int Row, int Col, int Sheet) {
			ICell Ret;
			Ret = sh[Sheet].GetRow(Row).GetCell(Col);
			return Ret;
		}

		public String GetCellValue(int Row, int Col) {
			String Ret;
			Ret = GetCellValue(Row, Col, this.CurrentSheetIndex);
			return Ret;
		}

		public string GetCellValue(int Row, int Col, int Sheet) {
			string Ret;
			Ret = sh[Sheet].GetRow(Row).GetCell(Col).ToString();
			return Ret;
		}

		public IRow GetRow(int Row, int Sheet) {
			IRow Ret;
			Ret = sh[Sheet].GetRow(Row);
			return Ret;
		}

		public IRow GetRow(int Row) {
			IRow Ret;
			Ret = GetRow(Row, CurrentSheetIndex);
			return Ret;
		}

	}

}

GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Emanuele Bonin24-Dec-15 9:58
Emanuele Bonin24-Dec-15 9:58 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Emanuele Bonin24-Dec-15 9:55
Emanuele Bonin24-Dec-15 9:55 
AnswerRe: Is there a better way to call Same Methods on different objects ? Pin
Gerry Schmitz24-Dec-15 8:11
mveGerry Schmitz24-Dec-15 8:11 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Emanuele Bonin26-Dec-15 5:28
Emanuele Bonin26-Dec-15 5:28 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Gerry Schmitz26-Dec-15 7:43
mveGerry Schmitz26-Dec-15 7:43 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Emanuele Bonin26-Dec-15 8:40
Emanuele Bonin26-Dec-15 8:40 
GeneralRe: Is there a better way to call Same Methods on different objects ? Pin
Gerry Schmitz26-Dec-15 16:16
mveGerry Schmitz26-Dec-15 16:16 
QuestionFill DataGrid ODBC Postgres Pin
Andre Dieme22-Dec-15 23:04
Andre Dieme22-Dec-15 23:04 
AnswerRe: Fill DataGrid ODBC Postgres Pin
Eddy Vluggen23-Dec-15 1:51
professionalEddy Vluggen23-Dec-15 1:51 
AnswerRe: Fill DataGrid ODBC Postgres Pin
Mycroft Holmes23-Dec-15 11:53
professionalMycroft Holmes23-Dec-15 11:53 
GeneralRe: Fill DataGrid ODBC Postgres Pin
Andre Dieme25-Dec-15 10:31
Andre Dieme25-Dec-15 10:31 
QuestionWhy i can't change my startup Form in VS2013. Pin
Kashaf Murtaza22-Dec-15 22:18
Kashaf Murtaza22-Dec-15 22:18 
AnswerRe: Why i can't change my startup Form in VS2013. Pin
Pete O'Hanlon22-Dec-15 23:35
mvePete O'Hanlon22-Dec-15 23:35 
AnswerRe: Why i can't change my startup Form in VS2013. Pin
OriginalGriff22-Dec-15 23:37
mveOriginalGriff22-Dec-15 23:37 
AnswerRe: Why i can't change my startup Form in VS2013. Pin
John Torjo28-Dec-15 0:14
professionalJohn Torjo28-Dec-15 0:14 
Questioncompression Pin
jackie.398121-Dec-15 9:18
jackie.398121-Dec-15 9:18 
AnswerRe: compression Pin
Kevin Marois21-Dec-15 9:28
professionalKevin Marois21-Dec-15 9:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.