C
C#15mo ago
u41c

Instantiating a new object failed?

Hi, I have a very simple class called InvItem that takes three parameters: itemNo, description, and price. When I try to instantiate a new object using it, Visual Studio tells me that I haven't passed it any correpsonding arguments? I feel like the answer should be obvious but my brain just ain't doing the braining today. InvItem.cs
using System;

public class InvItem
{
public int ItemNo { get; set; }
public string Description { get; set; }
public int Price { get; set; }

public InvItem(int itemNo, string description, int price)
{
ItemNo = itemNo;
Description = description;
Price = price;
}

public string GetDisplayText()
{
return ItemNo.ToString() + " " + Description + " ($" + Price.ToString() + ")";
}
}
using System;

public class InvItem
{
public int ItemNo { get; set; }
public string Description { get; set; }
public int Price { get; set; }

public InvItem(int itemNo, string description, int price)
{
ItemNo = itemNo;
Description = description;
Price = price;
}

public string GetDisplayText()
{
return ItemNo.ToString() + " " + Description + " ($" + Price.ToString() + ")";
}
}
frmNewItem.cs
// Create new inventory item
InvItem invItem = null;

// Take values from fields and instantiate a new inventory item object using those values
public InvItem getInvItem()
{
int newItemNo = int.Parse(txtItemNo.Text);
string newDescription = txtDescription.Text;
int newPrice = int.Parse(txtPrice.Text);

InvItem newInvItem = new InvItem(newItemNo, newDescription, newPrice);
MessageBox.Show(newInvItem.GetDisplayText());
return newInvItem;
}
// Create new inventory item
InvItem invItem = null;

// Take values from fields and instantiate a new inventory item object using those values
public InvItem getInvItem()
{
int newItemNo = int.Parse(txtItemNo.Text);
string newDescription = txtDescription.Text;
int newPrice = int.Parse(txtPrice.Text);

InvItem newInvItem = new InvItem(newItemNo, newDescription, newPrice);
MessageBox.Show(newInvItem.GetDisplayText());
return newInvItem;
}
No description
4 Replies
ParaLogia
ParaLogia15mo ago
The error you posted says that it's in "InvItemDB.cs". Can you post the code from that file?
u41c
u41cOP15mo ago
yeah absolutely
u41c
u41cOP15mo ago
Pastebin
InvItemDB.cs - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
u41c
u41cOP15mo ago
I just realised what I did wrong. I had price as int, not decimal.
Want results from more Discord servers?
Add your server