❔ How to call a method that uses a struct in it's definition?

I program in C# with Unity most-often, I am branching out to 'normal' C# and am stuck on connecting a method to main in a practice project.
public struct sInformation
public struct sInformation
is used to define this method:
sInformation oIncome(int[] aoItemList, float[] aoItemCosts)
sInformation oIncome(int[] aoItemList, float[] aoItemCosts)
and I cannot find a way to run this in Main to actually begin working out the results. Any help would be greatly appreciated!
46 Replies
Becquerel
Becquerel2y ago
where does oIncome live, inside sInformation?
Angius
Angius2y ago
Holy goddamn Hungarian notation lmao
Becquerel
Becquerel2y ago
lol yeah, was going to mention that later...
Angius
Angius2y ago
If it's not a static method, you'll need an instance of this struct
dankmememachine
dankmememachineOP2y ago
Haha yeah the formatting on this is weird, oIncome is it's own method
sInformation oIncome(int[] aoItemList, float[] aoItemCosts)
{
etc...
}
sInformation oIncome(int[] aoItemList, float[] aoItemCosts)
{
etc...
}
Angius
Angius2y ago
Wym "it's own"?
Becquerel
Becquerel2y ago
right, but does it live within the definition for sInformation?
Angius
Angius2y ago
Methods are defined inside of classes or structs Unless we're talking Program.cs with top-level statements
dankmememachine
dankmememachineOP2y ago
we are, this is in
class Program
{
class Program
{
my bad
Becquerel
Becquerel2y ago
ah, that's totally fine
Angius
Angius2y ago
It's not a static method
dankmememachine
dankmememachineOP2y ago
only thing outside of class is the struct
Angius
Angius2y ago
So you'll need to instantiate Program Or make it static Or make a non-static wrapper for Main
Becquerel
Becquerel2y ago
changing the method definition to static sInformation oIncome... will be the simplest of those options
Angius
Angius2y ago
$nonstaticmain
MODiX
MODiX2y ago
class Program
{
// that's all there is in this class
static void Main(string[] args) => new App().Run(args);
}
class Program
{
// that's all there is in this class
static void Main(string[] args) => new App().Run(args);
}
class App
{
public void Run(string[] args)
{
// code that would go into main, goes here
}
}
class App
{
public void Run(string[] args)
{
// code that would go into main, goes here
}
}
Angius
Angius2y ago
Or, yeah, make this method static (and, for all that is holy, get rid of those weird prefixes lol)
Becquerel
Becquerel2y ago
i mainly don't want to overload them with information when they probably don't know what static means
dankmememachine
dankmememachineOP2y ago
I mean, in my previous experience static just means it cannot be a value that can be changed, unless it means something else in this context
Angius
Angius2y ago
static methods exist on the type itself Not on an instance of that type
Becquerel
Becquerel2y ago
static does not mean 'readonly' it has a very specific meaning in c# that is generally counterintuitive for beginners are you familiar with basic OOP concepts of creating instances of classes?
dankmememachine
dankmememachineOP2y ago
I think so? In essence, is there a minimally invasive way to be able to run this oIncome method? I can post the whole script if that helps
Becquerel
Becquerel2y ago
sure, you can post your whole Program.cs the simplest way is likely to add the static keyword to the definition of your method
dankmememachine
dankmememachineOP2y ago
ok i will get that up in a minute, thank you
Becquerel
Becquerel2y ago
$paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Becquerel
Becquerel2y ago
might be useful
dankmememachine
dankmememachineOP2y ago
ill mention the purpose of this app is to be open to improvement, so most of this code came as is
Becquerel
Becquerel2y ago
of course
dankmememachine
dankmememachineOP2y ago
class Program
{
static void Main(string[] args)
{
}

static sInformation oIncome(int[] aoIngredientList, float[] aoIngredientCosts)
{
sInformation returnInfo = new sInformation();
float fBaseChargePricePerItem = 2.34f;

bool bIsSaleHappening = false;

int iterWIthLowestIngredient = 0;
for(int i = 0; i < aoIngredientList.Length; ++i)
{
if(aoIngredientList[i] < aoIngredientList[iterWIthLowestIngredient])
{
iterWIthLowestIngredient = i;
}
}

while (aoIngredientList[iterWIthLowestIngredient] > 0)
{
for (int i = 0; i < aoIngredientList.Length; ++i)
{
aoIngredientList[i]--;
}

if (DateTime.Now.DayOfWeek.ToString() == "Tuesday")
{
bIsSaleHappening = true;
}

returnInfo.fTotalAvailableTacos++;

var randomGen = new System.Random();
returnInfo.fTotalChargeForItems += fBaseChargePricePerItems + randomGen.Next(8, 16);

if(bIsSaleHappening)
{
returnInfo.fTotalChargeForItems -= fBaseChargePricePerItems / 2;
}

for (int i = 0; i < aoIngredientCosts.Length; ++i)
{
returnInfo.fOurTotalIngredientCosts += aoIngredientCosts[i];
}
}

return returnInfo;
}
}

public struct sInformation
{
public float fTotalAvailableItems;
public float fTotalChargeForItems;
public float fOurTotalIngredientCosts;
}
}
class Program
{
static void Main(string[] args)
{
}

static sInformation oIncome(int[] aoIngredientList, float[] aoIngredientCosts)
{
sInformation returnInfo = new sInformation();
float fBaseChargePricePerItem = 2.34f;

bool bIsSaleHappening = false;

int iterWIthLowestIngredient = 0;
for(int i = 0; i < aoIngredientList.Length; ++i)
{
if(aoIngredientList[i] < aoIngredientList[iterWIthLowestIngredient])
{
iterWIthLowestIngredient = i;
}
}

while (aoIngredientList[iterWIthLowestIngredient] > 0)
{
for (int i = 0; i < aoIngredientList.Length; ++i)
{
aoIngredientList[i]--;
}

if (DateTime.Now.DayOfWeek.ToString() == "Tuesday")
{
bIsSaleHappening = true;
}

returnInfo.fTotalAvailableTacos++;

var randomGen = new System.Random();
returnInfo.fTotalChargeForItems += fBaseChargePricePerItems + randomGen.Next(8, 16);

if(bIsSaleHappening)
{
returnInfo.fTotalChargeForItems -= fBaseChargePricePerItems / 2;
}

for (int i = 0; i < aoIngredientCosts.Length; ++i)
{
returnInfo.fOurTotalIngredientCosts += aoIngredientCosts[i];
}
}

return returnInfo;
}
}

public struct sInformation
{
public float fTotalAvailableItems;
public float fTotalChargeForItems;
public float fOurTotalIngredientCosts;
}
}
there might be discrepency in naming, im in the middle of renaming some variables here
Angius
Angius2y ago
Yeah, just call oIncome in Main as is
dankmememachine
dankmememachineOP2y ago
ok, but what about the parameters? this is everything in the code, did i need to create a list for the actual method params?
Angius
Angius2y ago
Well, yeah
Becquerel
Becquerel2y ago
yes, you will need to create arrays for the values you want to pass to the method
Angius
Angius2y ago
You need the params the method needs
dankmememachine
dankmememachineOP2y ago
would it be best practice to define that before main?
Angius
Angius2y ago
Why? Do you need them everywhere in the class? Or do you need them in the main?
dankmememachine
dankmememachineOP2y ago
well, we have
int[] aoIngredientList
int[] aoIngredientList
required, but I don't have anything to put into the method. This would be a int[] containing random food items like burger or soda etc for the script to run through
Angius
Angius2y ago
So create a local variable
dankmememachine
dankmememachineOP2y ago
I guess i just mean, with the way this code is presented, what the best practice would be, so a local variable in main to call the method?
Angius
Angius2y ago
And this array seems to be holding integers, not ingredients, btw Yeah
dankmememachine
dankmememachineOP2y ago
right
Angius
Angius2y ago
Just local variables
dankmememachine
dankmememachineOP2y ago
the ints would represent food items my bad So this doesn't seem to like the bracketing in this setup :
static void Main(string[] args)
{
private int[] Ingredients = { 1, 2 };
private float[] Costs = { 2.20f, 3.40f };
oIncome(Ingredients, Costs);
}
static void Main(string[] args)
{
private int[] Ingredients = { 1, 2 };
private float[] Costs = { 2.20f, 3.40f };
oIncome(Ingredients, Costs);
}
it says } expected for the main function
Angius
Angius2y ago
Local variables cannot be public or private Local variables are... local
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server