Passing a array piece to a function

Jun 2, 2008
163
0
0
Hi, let's say I have an array and cellData[5], cellData[7], and cellData[15] need to be passed off to a function to format it right.

In this case, it's a date from excel, but it doesn't convert right, 10/27/2007 turns into 39545 or something.

So I have a piece of code that can convert it.

Here's the code. It only works for one cell obviously.

//Converts the date to proper format
string serialDateS;
serialDateS = cellData[7];
int serialDate;
serialDate = Convert.ToInt32(cellData[7]);
int l = serialDate + 68569 + 2415019;
int n = (int)((4 * l) / 146097);
l = l - (int)((146097 * n + 3) / 4);
int k = (int)((4000 * (l + 1)) / 1461001);
l = l - (int)((1461 * k) / 4) + 31;
int j = (int)((80 * l) / 2447);
int month = l - (int)((2447 * j) / 80);
l = (int)(j / 11);
int day = j + 2 - (12 * l);
int year = 100 * (n - 49) + k + l;
string dateConverted = (day + "/" + month + "/" + year);

//END
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
I had issues with /-separated dates being formatted wrong in PHP- in the course of passing through functions, the /s were interpreted as division, and broke things. Could that be your problem?
 
Jun 2, 2008
163
0
0
Originally posted by: Crusty
And your question is? :p

How do I pass a lot of variables to one function.

It's going to be about 15+ dates that need to be converted. Right now I can only do one.

presidenttender, My code works, I just don't understand how I can pass a bunch of variables in a array through it.

something like

datefunction(array[5])
datefunction(array[7])
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Well, yeah...you loop through the array...

for (int $i = 0; $i < count($array); $i++) {
datefunction($array);
}
 
Jun 2, 2008
163
0
0
That won't work out too well because only parts of the array need to be fixed. Should I make another array just for dates??

My code is here

*******************

using System;
using Microsoft.Office.Interop.Excel;
using System.Threading;
using System.IO;

namespace ExcelExample
{
/// <summary>
///
/// </summary>
class ExcelClass
{
/// <summary>
///
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Data Extractor");
//Open Excel
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); // Creates a new Excel Application
excelApp.Visible = false; // Makes Excel not visible to the user.

string path = "c:/temp/pmCards/";
string[] workbooks = Directory.GetFiles(path, "*.xls");
//Create a CSV file to write to
TextWriter tw = new StreamWriter("c:/temp/pm1.csv");
//Create the header
string cellName = "Proj_Num,ProjNA,Exec_Spon,Proj_Own,Proj_Mgr,Proj_Desc,Org,Effect_DT,Pyxis_No,Depend,ITD_capbudg,ITD_capact,ITD_capvar,YTD_capbudg,YTD_capact,YTD_capvar,YEF_capbudg,YEF_capact,YEF_capvar,ITD_expbudg,ITD_expact,ITD_expvar,YTD_expbudg,YTD_expact,YTD_expvar,YEF_expbudg,YEF_expact,YEF_expvar";
//Write it
tw.WriteLine(cellName);

//For each workbook
foreach (string s in workbooks)
{
//TEMP
Console.WriteLine("FOUND");

// The following code opens an existing workbook
string workbookPath = s; // Add your own path here
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);

// The following gets the Worksheets collection
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;

// The following gets Scorecard Sheet for editing
string currentSheet = "Scorecard";
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

// The following gets a cell
string cell;
cell = "K53";
Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(cell, cell);

//************ Extraction of Number from Filename Function ***********
FileInfo fileToRead = new FileInfo(workbookPath);

string fileNameWithOutSextension = fileToRead.FullName.Replace(fileToRead.Extension, "");

string numberInTheFileName = string.Join(null, System.Text.RegularExpressions.Regex.Split(fileNameWithOutSextension, "[^\\d]"));

int numberAsInteger = Int32.Parse(numberInTheFileName);
string projectID;
projectID = numberAsInteger.ToString();
//******** END Extraction of Number from Filename Function ***********

//Create cell array
string[] cellData = new string[] { "K53", "C3", "C5", "C7", "C6", "C9", "H6", "I3", "J6", "H9", "I17", "J17", "K17", "I20", "J20", "K20", "I23", "J23", "K23", "I18", "J18", "K18", "I21", "J21", "K21", "I24", "J24", "K24" };

for (int i = 0; i <= 27; i++)
{
cell = cellData;
excelCell = excelWorksheet.get_Range(cell, cell);
cellData = excelCell.Value2 + "";

//Take away commas in strings to have pure data for CSV
string commaClear = cellData;
commaClear = commaClear.Replace(",", "");
cellData = commaClear;

//Write Correct ProjNo to Array
cellData[0] = projectID;
Console.WriteLine(cellData);
}

//Converts the date to proper format
string serialDateS;
serialDateS = cellData[7];
int serialDate;
serialDate = Convert.ToInt32(cellData[7]);
int l = serialDate + 68569 + 2415019;
int n = (int)((4 * l) / 146097);
l = l - (int)((146097 * n + 3) / 4);
int k = (int)((4000 * (l + 1)) / 1461001);
l = l - (int)((1461 * k) / 4) + 31;
int j = (int)((80 * l) / 2447);
int month = l - (int)((2447 * j) / 80);
l = (int)(j / 11);
int day = j + 2 - (12 * l);
int year = 100 * (n - 49) + k + l;
string dateConverted = (day + "/" + month + "/" + year);
//END

//Convert date to correct format
cellData[7] = dateConverted;
Console.WriteLine("********* END Project *********");
//Takes the array and converts it to a comma spaced string
string cellDataStr = String.Join(",", cellData);

//Writes the string to a text file
tw.WriteLine(cellDataStr);
}
//Do not ask excel to save
excelApp.DisplayAlerts = false;
//Exit excel
excelApp.Quit();
//Close the file
tw.Close();
}
}
}


*******************
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: TheEarthWillShake
Originally posted by: Crusty
And your question is? :p

How do I pass a lot of variables to one function.

It's going to be about 15+ dates that need to be converted. Right now I can only do one.

presidenttender, My code works, I just don't understand how I can pass a bunch of variables in a array through it.

something like

datefunction(array[5])
datefunction(array[7])

It all depends on the context really. If you have to loop through your array to determine which indexes need formatting then you would just call the datefunction for each element you determine needs to be formatted, or if you really wanted to, you could create a 2nd array that stores only the values you want to format, and pass the whole array to the datefunction.
 
Jun 2, 2008
163
0
0
I see, this is what I want to do but I don't know how.


namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
domath(fibarray[4]); //Call domath function
domath(fibarray[6]); //Call domath function

}

static void domath()
{ int x;
System.Console.WriteLine(fibarray[x]);
int num = fibarray[x];
fibarray[x] = num * 5
return fibarray[x]
}
}
}
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Your domath function should have a different signature. It's not a void, since you're returning a value, and you really want to be passing in two parameters - the array and the index of the target.

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
domath(4, fibarray); //Call domath function
domath(6, fibarray); //Call domath function

}



static int domath(int target, int[] values)
{
System.Console.WriteLine(values[target]);
values[target] = values[target] * 5;
return values[target]
}


Hopefully I remembered the C# syntax right.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
What language are you using again? If this is c++ the best way to do it is to send the array and a size right through and declare your function input to be a pointer. In C++ arrays are pointers, it all works out just fine.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: Cogman
What language are you using again? If this is c++ the best way to do it is to send the array and a size right through and declare your function input to be a pointer. In C++ arrays are pointers, it all works out just fine.

C#!
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Sorry, i haven't ever dabbled with c# code so I really couldn't pick it out from python, perl, or VB (Yes, I know they are all vastly different)
 
Jun 2, 2008
163
0
0
Originally posted by: PhatoseAlpha
Your domath function should have a different signature. It's not a void, since you're returning a value, and you really want to be passing in two parameters - the array and the index of the target.

Thank you, I have done this but I'm stuck.

//**********

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int[] cellData = new int[] { 39945, 39965 };
domath(0, cellData); //Call domath function
domath(1, cellData); //Call domath function
}

static int domath(int target, int[] values)
{
//Converts the date to proper format
string serialDateS;
serialDateS = Convert.ToString(values[target]);
int serialDate;
serialDate = Convert.ToInt32(values[target]);
int l = serialDate + 68569 + 2415019;
int n = (int)((4 * l) / 146097);
l = l - (int)((146097 * n + 3) / 4);
int k = (int)((4000 * (l + 1)) / 1461001);
l = l - (int)((1461 * k) / 4) + 31;
int j = (int)((80 * l) / 2447);
int month = l - (int)((2447 * j) / 80);
l = (int)(j / 11);
int day = j + 2 - (12 * l);
int year = 100 * (n - 49) + k + l;
string dateConverted = (day + "/" + month + "/" + year);
Console.WriteLine(dateConverted + " What I want!");
Console.WriteLine("************");
Console.WriteLine(values[target] + " What I don't want!");
Console.WriteLine("************");
return values[target];
}

}
}

//**********

My issue right now is that I'm taking a int messing with it, then I want to return it back as a string. Is that even possible? Would I return it, then do another routine to convert it to a string?

Thanks
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Change your function to return a string then just do

return values[target]+"";