steven preadly
steven preadly
Explore posts from servers
CC#
Created by steven preadly on 7/13/2024 in #help
✅ positional parameters
dose this sentence You can use positional parameters in a primary constructor to create and instantiate a type with immutable properties. form docs means that the record create a primary constractor that contains the parameter , or it means that i can create a primary constractor inside the record type with the parameter , as i read the record creates by default a primary constructor and a proparites that is inilized using this constractor
31 replies
CC#
Created by steven preadly on 7/11/2024 in #help
why the Clist.Contains(customer) is not getting true in that case ?
why the Clist.Contains(customer) is not getting true while the new object that i created is already on the list dose that means that i have to override the Equals method in the customer class
c#

List<Customer> Clist = new List<Customer>(2)
{
new Customer() { Id = 1, Name = "mina" },
new Customer() { Id = 2, Name = "shaker" },
new Customer() { Id = 3, Name = "Kirollos" },
new Customer() { Id = 4, Name = "georget" }
};

Customer customer = new Customer() { Id = 1, Name = "mina" };


if (Clist.Contains(customer))
{
Console.WriteLine("Customer Exits");
}
else
{
Console.WriteLine("customer is not in the list");
}
c#

List<Customer> Clist = new List<Customer>(2)
{
new Customer() { Id = 1, Name = "mina" },
new Customer() { Id = 2, Name = "shaker" },
new Customer() { Id = 3, Name = "Kirollos" },
new Customer() { Id = 4, Name = "georget" }
};

Customer customer = new Customer() { Id = 1, Name = "mina" };


if (Clist.Contains(customer))
{
Console.WriteLine("Customer Exits");
}
else
{
Console.WriteLine("customer is not in the list");
}
i have tried to override the Equals() method in the customer class but, what was intersting is that it works
c#
public class Customer
{
public int Id { get; set; }
public string? Name { get; set; }

public override bool Equals(object? obj)
{
if (obj == null) return false;
if (obj.GetType() != typeof(Customer)) return false;
return (this.Name == ((Customer)obj).Name && this.Id == ((Customer)obj).Id);
}

public override int GetHashCode()
{
return this.Id.GetHashCode();
}
}
c#
public class Customer
{
public int Id { get; set; }
public string? Name { get; set; }

public override bool Equals(object? obj)
{
if (obj == null) return false;
if (obj.GetType() != typeof(Customer)) return false;
return (this.Name == ((Customer)obj).Name && this.Id == ((Customer)obj).Id);
}

public override int GetHashCode()
{
return this.Id.GetHashCode();
}
}
32 replies
CC#
Created by steven preadly on 7/9/2024 in #help
✅ I have been hired in a company that is still using c# with webforms asp.net
I have been hired in a company that is still using c# with webforms asp.net , they dont have the intension to migrate to aspCore i am one of the juniors in my team what should i do can any one give me opinion? do i have to study asp.net webforms fully because i have only the basics of it and them as a update for my self i study asp.net core ? or what should i do
21 replies
CC#
Created by steven preadly on 7/7/2024 in #help
✅ what will be the default value of the `params int[] value` if i did not pass it while calling M
what will be the default value of the params int[] value if i did not pass it while calling method , do this will be trated as an empty array of ints
c#

public static void Sum(int number , int number2,params int[] values)
{
Console.WriteLine(number);
Console.WriteLine(number2);
Console.WriteLine(values is int[]);
}
c#

public static void Sum(int number , int number2,params int[] values)
{
Console.WriteLine(number);
Console.WriteLine(number2);
Console.WriteLine(values is int[]);
}
22 replies
CC#
Created by steven preadly on 7/7/2024 in #help
i dont know why i am still got `CS8602 - Dereference of a possibly null reference.` in that indexer
here is an indexer i still got this warning in the setter i dont know why
c#
public string? this[int employee_id]
{
get => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id)?.Name ?? "No Name Found";

set => listEmployees.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value;
}
c#
public string? this[int employee_id]
{
get => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id)?.Name ?? "No Name Found";

set => listEmployees.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value;
}
34 replies
CC#
Created by steven preadly on 7/4/2024 in #help
✅ how can i improve this code ?
c#
c#
78 replies
KPCKevin Powell - Community
Created by steven preadly on 6/29/2024 in #front-end
what are the ways that i can follow to mange the height in card created using bootstrap
No description
18 replies
KPCKevin Powell - Community
Created by steven preadly on 6/20/2024 in #front-end
Why is the <div> with position-absolute and start-100 positioned outside the container?
No description
17 replies
CC#
Created by steven preadly on 5/28/2024 in #help
can I ask about your opinion for this description that prove that structs is value type
c#
/*
* in this example we have a structure and a class
* and those are done to prove the statment above
* that the struct is a value type while the class is
* refrence type
*/
public struct Point
{
public int x;
public int y;
}


class MyClass
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
/*
* in here we used the struct we inilize it with a value x = 10
* and y = 20
*/
Point p1 = new Point { x = 10, y = 20 };

// then we write the value of x to the console
// it gets (10)
Console.WriteLine(p1.x);

//we used the ModifayStruct(Point P1) and we passed the P1 struct to it
// where this method modifayes the p1.x value to 12 other than 10
ModifayStruct(p1);

// the result when writting to the console is 10
// this is happiesn becuse the struct is a value type when we passed the struct to
// a method we are passing a copy of the struct not an adress to it in the memory
Console.WriteLine(p1.x);


/*
* in here we instatiated a mew Myclass object with x feild is 10
*/
MyClass @class = new MyClass { x = 10 };

// we used the ModifayClass(MyClass obj) and passed the above object to it
// where this method modifies x feild inside of it
ModifayClass(@class);

// the result is that x got modified that becuse when we passed a refrence varible to the
// method it got passed by refrence whicj means we are passng the address of that object
// to the method
Console.WriteLine(@class.x);

}

// modifays the point structs feild x to 12
public static void ModifayStruct(Point p1)
{
p1.x = 12;
}

// modifays the point class feild x to 12
public static void ModifayClass(MyClass obj)
{
obj.x = 12;
}
}
c#
/*
* in this example we have a structure and a class
* and those are done to prove the statment above
* that the struct is a value type while the class is
* refrence type
*/
public struct Point
{
public int x;
public int y;
}


class MyClass
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
/*
* in here we used the struct we inilize it with a value x = 10
* and y = 20
*/
Point p1 = new Point { x = 10, y = 20 };

// then we write the value of x to the console
// it gets (10)
Console.WriteLine(p1.x);

//we used the ModifayStruct(Point P1) and we passed the P1 struct to it
// where this method modifayes the p1.x value to 12 other than 10
ModifayStruct(p1);

// the result when writting to the console is 10
// this is happiesn becuse the struct is a value type when we passed the struct to
// a method we are passing a copy of the struct not an adress to it in the memory
Console.WriteLine(p1.x);


/*
* in here we instatiated a mew Myclass object with x feild is 10
*/
MyClass @class = new MyClass { x = 10 };

// we used the ModifayClass(MyClass obj) and passed the above object to it
// where this method modifies x feild inside of it
ModifayClass(@class);

// the result is that x got modified that becuse when we passed a refrence varible to the
// method it got passed by refrence whicj means we are passng the address of that object
// to the method
Console.WriteLine(@class.x);

}

// modifays the point structs feild x to 12
public static void ModifayStruct(Point p1)
{
p1.x = 12;
}

// modifays the point class feild x to 12
public static void ModifayClass(MyClass obj)
{
obj.x = 12;
}
}
3 replies
CC#
Created by steven preadly on 5/28/2024 in #help
When should i use Private constractor with factory method?
here included example
c#
public class Student
{

private string name;
private Student(string name)
{
this.name = name;
}

public static Student CreateObject(string name)
{
return new Student(name);
}

public string Name
{
get { return name; }
}


}
c#
public class Student
{

private string name;
private Student(string name)
{
this.name = name;
}

public static Student CreateObject(string name)
{
return new Student(name);
}

public string Name
{
get { return name; }
}


}
12 replies
CC#
Created by steven preadly on 5/27/2024 in #help
Calculating Student Grade Book Average (GBA) in Percent - Encountering Zero Value Issue
I'm working on a student class with properties mark and totalMark. My goal is to define the totalMark property to compute the GBA as a percentage. However, every time I implement the calculation, the totalMark value consistently returns 0.
c#
public class Student
{

private int totalmarks;

public string? Name { get; set; }

public int Marks { get; set; }

public int TotalMarks
{
get
{
return (this.Marks / 250) * 100;
}
}

}

Student student = new Student();

student.Marks = 120;

int gba = student.TotalMarks;

Console.WriteLine(gba);
c#
public class Student
{

private int totalmarks;

public string? Name { get; set; }

public int Marks { get; set; }

public int TotalMarks
{
get
{
return (this.Marks / 250) * 100;
}
}

}

Student student = new Student();

student.Marks = 120;

int gba = student.TotalMarks;

Console.WriteLine(gba);
63 replies
CC#
Created by steven preadly on 5/26/2024 in #help
is the method hiding considered a type of polymorphisim?
is the method hiding considered a type of polymorphisim?
2 replies
CC#
Created by steven preadly on 5/24/2024 in #help
Polymorphism
dose the below sentence simply describes the meaning of Polymorphism
polymorphism is a Opp pillar that treats the objects created from derived class as a base class at runtime
polymorphism is a Opp pillar that treats the objects created from derived class as a base class at runtime
as an example in here the reference variable is pointing to the Employee base class while at run time the method is called on the PartTimeEmployee() type correct ?
c#
public class Employee
{
public string? firstName = "FN";
public string? lastName = "LN";

public virtual void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName);
}
}

public class PartTimeEmployee: Employee
{
public override void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName + " - PartTime");
}
}
Employee employee = new PartTimeEmployee();

employee.PrintFullName();
c#
public class Employee
{
public string? firstName = "FN";
public string? lastName = "LN";

public virtual void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName);
}
}

public class PartTimeEmployee: Employee
{
public override void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName + " - PartTime");
}
}
Employee employee = new PartTimeEmployee();

employee.PrintFullName();
and that is polymorphisim we have a base class method in one class(type) that can have many forms in drived class and the type that the method are called from is defined at runtime
2 replies
CC#
Created by steven preadly on 5/23/2024 in #help
Why does calling the PrintFullName() method require casting it as ((Employee)PTE).PrintFullName() ?
Hi, I have two classes where one inherits from the other. Here's the code. My question is: why do I need to cast the method PrintFullName() when calling it as ((Employee)PTE).PrintFullName()? Why can't it be cast directly like (Employee)PTE.PrintFullName()?
c#
public class Employee
{
public string? firstName;
public string? lastName;

public virtual string PrintFullName()
{
//Console.WriteLine(firstName + " " + lastName);

return firstName + " " + lastName;
}
}

public class PartTimeEmployee: Employee
{
public new string PrintFullName()
{
return firstName + " " + lastName + " " + "contractor";
}
}
c#
public class Employee
{
public string? firstName;
public string? lastName;

public virtual string PrintFullName()
{
//Console.WriteLine(firstName + " " + lastName);

return firstName + " " + lastName;
}
}

public class PartTimeEmployee: Employee
{
public new string PrintFullName()
{
return firstName + " " + lastName + " " + "contractor";
}
}
c#
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.firstName = "parttime";
PTE.lastName = "Employee";
((Employee)PTE).PrintFullName();
c#
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.firstName = "parttime";
PTE.lastName = "Employee";
((Employee)PTE).PrintFullName();
19 replies
CC#
Created by steven preadly on 5/21/2024 in #help
How can I get the memory address of a feilds in C#?
created an object from a class in C#. Is it possible to retrieve its memory location and address using C# code? i have tried this code but i got error at this line IntPtr address = GCHandle.AddrOfPinnedObject(handle); of the code
c#
Person person_1 = new Person("mina remon shaker");
FieldInfo? fieldInfo = typeof(Person).GetField("Name");
GCHandle handle = GCHandle.Alloc(person_1, GCHandleType.Pinned);
IntPtr address = GCHandle.AddrOfPinnedObject(handle);
handle.Free();
Console.WriteLine("Object address: 0x{0:X}", address.ToInt64());
c#
Person person_1 = new Person("mina remon shaker");
FieldInfo? fieldInfo = typeof(Person).GetField("Name");
GCHandle handle = GCHandle.Alloc(person_1, GCHandleType.Pinned);
IntPtr address = GCHandle.AddrOfPinnedObject(handle);
handle.Free();
Console.WriteLine("Object address: 0x{0:X}", address.ToInt64());
`
37 replies