C
C#8mo ago
Dinny

I am unaware on how to fix this conversion error

it's saying i can't convert from string to int, though I am unsure why when i used the string.format method to represent my int variable ID as a string and display it in my output file. I have three separte files for a regular user logging in, an administrator, and thena file to put the whole program together. here is admin and the full program since that is where the issue lies
using System;
using System.Globalization;
using System.IO;


namespace LogIn
{
public class Administrator : RegularUser
{
//data fields
private int ID;


public Administrator(int ID = 0, string userName = " ") : base(userName)
{
//data fields
this.ID = ID;
}

//GETTER
/**
Returns the ID of the admin
@return: admin's ID
*/
public int GetID() { return ID; }


//SETTERS
/**
Returns an updated ID number
@return: admin's updated ID number
*/
public void SetID(int ID) { this.ID = ID; }

//sign-in method
public new static void SignIn(string dateAndTime, StreamWriter writer)
{
writer.WriteLine("signed in @ {1}", dateAndTime);
}

//sign out method
public new static void SignOut(string dateAndTime, StreamWriter writer)
{
writer.WriteLine("signed out @ {1}", dateAndTime);
}


/** Returns the user's username and ID in string format and whether they've signed in/out
@return: a string representing who signed in/out along with ther ID #
*/
public override string Stringify()
{
return string.Format("({1}) ", base.Stringify(), GetID());
}

public override string Stringify2()
{
return string.Format("({1}) ", base.Stringify2(), GetID());
}


}


}
using System;
using System.Globalization;
using System.IO;


namespace LogIn
{
public class Administrator : RegularUser
{
//data fields
private int ID;


public Administrator(int ID = 0, string userName = " ") : base(userName)
{
//data fields
this.ID = ID;
}

//GETTER
/**
Returns the ID of the admin
@return: admin's ID
*/
public int GetID() { return ID; }


//SETTERS
/**
Returns an updated ID number
@return: admin's updated ID number
*/
public void SetID(int ID) { this.ID = ID; }

//sign-in method
public new static void SignIn(string dateAndTime, StreamWriter writer)
{
writer.WriteLine("signed in @ {1}", dateAndTime);
}

//sign out method
public new static void SignOut(string dateAndTime, StreamWriter writer)
{
writer.WriteLine("signed out @ {1}", dateAndTime);
}


/** Returns the user's username and ID in string format and whether they've signed in/out
@return: a string representing who signed in/out along with ther ID #
*/
public override string Stringify()
{
return string.Format("({1}) ", base.Stringify(), GetID());
}

public override string Stringify2()
{
return string.Format("({1}) ", base.Stringify2(), GetID());
}


}


}
39 Replies
Dinny
Dinny8mo ago
now part of the file to put it all together
while (!reader.EndOfStream)
{

string? nextLine = reader.ReadLine();
if (nextLine != null)
{
string[] nextItem = nextLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (nextItem.Length == 0) { continue; }


if (nextItem[0].Equals("IN"))
{
if (nextItem[1].Equals("REG"))
{
writer.Write("Regular User ");
reg = new(nextItem[2]);

//get date and time
/*write info to output file in format
adm/reg [username] date and time*/

}

if (nextLine[1].Equals("ADM"))
{
writer.Write("Administrator ");
admin = new(nextItem[2]);
//get the username
//get ID #
//get date and time
/*write info to output file in format
adm/reg [username] (ID) date and time*/

}
while (!reader.EndOfStream)
{

string? nextLine = reader.ReadLine();
if (nextLine != null)
{
string[] nextItem = nextLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (nextItem.Length == 0) { continue; }


if (nextItem[0].Equals("IN"))
{
if (nextItem[1].Equals("REG"))
{
writer.Write("Regular User ");
reg = new(nextItem[2]);

//get date and time
/*write info to output file in format
adm/reg [username] date and time*/

}

if (nextLine[1].Equals("ADM"))
{
writer.Write("Administrator ");
admin = new(nextItem[2]);
//get the username
//get ID #
//get date and time
/*write info to output file in format
adm/reg [username] (ID) date and time*/

}
i get the error where it says "admin = new(nextItem[2]);"
Angius
Angius8mo ago
Because... it's a string nextItem is an array of strings string[], it says so right there So, every element of that array will be a string If you want to parse a string to an int, use int.TryParse() Also, why not just use Json instead of fiddling around with stringifying the data manually?
Dinny
Dinny8mo ago
bc that’s the way my prof wants it done we are learning overriding and i want it to stay a string that’s why i was confused
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius8mo ago
Or better yet, use string interpolation instead
Dinny
Dinny8mo ago
this is the way my prof is teaching it and the way he expects us to do it as well he hasn’t taught us that yet so idk what that is he requires doc strings and said we will use a large chunk of points if we don’t use them too i get this seems impractical but i just think my profs teaching style is outdated
Angius
Angius8mo ago
var num = 69;
Console.WriteLine($"The number is {num}");
var num = 69;
Console.WriteLine($"The number is {num}");
that's interpolation
Dinny
Dinny8mo ago
would we be able to work around that ? alright thank u
Angius
Angius8mo ago
And yes, it does seem like yout teacher is another one of the fossils that stopped developing their skills in 1999
Dinny
Dinny8mo ago
HAHAHAHAHAHAHAHAA i’ve gotten help before here and ppl were so confused why i did code the way i did. i must always explain my prof’s teaching style Aside from that tho, my goal is to turn the ID number into a strong but it keeps trying to turn it into an int but idk how to make that go away
Angius
Angius8mo ago
Administrator ctor takes an int You must give it an int
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Dinny
Dinny8mo ago
i did give an int, but at the end, i wanted to display it all as a string like my prof did lemme show u what he did plz hold
public virtual string Stringify() { return string.Format("Name: {0}\nQuantity: {1:D}", GetName(), GetQuantity()); }
public virtual string Stringify() { return string.Format("Name: {0}\nQuantity: {1:D}", GetName(), GetQuantity()); }
here's his entire code for the first part of his example code
namespace Module_8 {
public class GenericItem {
// Data fields
private string? name; // Stores the name of the item.
private uint quantity; // Stores the quantity of the item.

// Constructor
public GenericItem(string name = "", uint quantity = 0) {
SetName(name);
SetQuantity(quantity);
}

// Getters

/** Returns the name of the item.
@return: name of the item
*/
public string? GetName() { return name; }

/** Returns the quantity of the item.
@return: quantity of the item
*/
public uint GetQuantity() { return quantity; }

// Setters

/** Updates the name of the item.
@param name: updated name of the item
*/
public void SetName(string name) { this.name = name; }

/** Updates the quantity of the item.
@param quantity: updated quantity of the item
*/
public void SetQuantity(uint quantity) { this.quantity = quantity; }

// Method

/** Converts the item to a string.
@return: a string representing the item
*/
public virtual string Stringify() { return string.Format("Name: {0}\nQuantity: {1:D}", GetName(), GetQuantity()); }
}
}
namespace Module_8 {
public class GenericItem {
// Data fields
private string? name; // Stores the name of the item.
private uint quantity; // Stores the quantity of the item.

// Constructor
public GenericItem(string name = "", uint quantity = 0) {
SetName(name);
SetQuantity(quantity);
}

// Getters

/** Returns the name of the item.
@return: name of the item
*/
public string? GetName() { return name; }

/** Returns the quantity of the item.
@return: quantity of the item
*/
public uint GetQuantity() { return quantity; }

// Setters

/** Updates the name of the item.
@param name: updated name of the item
*/
public void SetName(string name) { this.name = name; }

/** Updates the quantity of the item.
@param quantity: updated quantity of the item
*/
public void SetQuantity(uint quantity) { this.quantity = quantity; }

// Method

/** Converts the item to a string.
@return: a string representing the item
*/
public virtual string Stringify() { return string.Format("Name: {0}\nQuantity: {1:D}", GetName(), GetQuantity()); }
}
}
Angius
Angius8mo ago
Jesus
Dinny
Dinny8mo ago
yeah i was NOT joking lmao
Angius
Angius8mo ago
Well If you have something that inherits from GenericItem, and the point is to learn about overrides, you can override the Stringify() method
Dinny
Dinny8mo ago
I DID idk y that caps sorry i did one momebt
Angius
Angius8mo ago
So... what's the problem?
Dinny
Dinny8mo ago
public override string Stringify()
{
return string.Format("({1}) ", base.Stringify(), GetID());
}
public override string Stringify()
{
return string.Format("({1}) ", base.Stringify(), GetID());
}
whenever i try calling it
Angius
Angius8mo ago
The format expects one parameter With a wrong ID to boot
Dinny
Dinny8mo ago
oops hang on i did it wong
Angius
Angius8mo ago
But you pass it two parameters
Dinny
Dinny8mo ago
how if i am just referencing to the original stringify() it should have everything in the base including what i am adding like what he did
Angius
Angius8mo ago
If you override a method, you provide a whole entire new implementation of it
Dinny
Dinny8mo ago
public override string Stringify() {
return string.Format("{0}\nExpiration Date: {1}", base.Stringify(), GetExpirationDate());
}
public override string Stringify() {
return string.Format("{0}\nExpiration Date: {1}", base.Stringify(), GetExpirationDate());
}
his override
Angius
Angius8mo ago
Yeah Two params in the format string
Dinny
Dinny8mo ago
which is pretty much what i did
Angius
Angius8mo ago
Two params given Works Your example has one param in the format string With ID 1 instead of 0 And you give it two values Does not compute
Dinny
Dinny8mo ago
dude u wanna know what's crazy i had something completely different there before that required a {0} and a {1} and deleted the wrong one im gonna throw up no way it was that simple of an error ugh plz hold
MODiX
MODiX8mo ago
Angius
REPL Result: Success
string.Format("{1}, {2}", 1, 2, 3)
string.Format("{1}, {2}", 1, 2, 3)
Result: string
2, 3
2, 3
Compile: 464.220ms | Execution: 29.099ms | React with ❌ to remove this embed.
Dinny
Dinny8mo ago
no i get how that works ijust, accidentally put the wrong number and didn't realize ugh thank u so much im glad that part is fixed but the error is still showing in the main program "cannot convert string to int"
Angius
Angius8mo ago
Probably the same line as at the beginning Your constructor takes an int You're giving it a string
Angius
Angius8mo ago
Square peg does not fit round hole
Dinny
Dinny8mo ago
how would i work around it like he did ? cause he nearly did the same thing
Angius
Angius8mo ago
You... wouldn't? You'd convert that string to an integer That's that
Dinny
Dinny8mo ago
okay plz hold ok i have no mor eof those errors per say but now it's saying i have no static main method whent= i do i have this
static void main(string[] args)
static void main(string[] args)
Angius
Angius8mo ago
Main C# is case-sensitive
Dinny
Dinny8mo ago
im still thinking in terms of c++ ughhhhh thank you it's always the tiniest things