Object not getting assigned. [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm working on an asp.net MVC project and have the following code in my view model
public Division ToDivision()
{
Division d = new Division();
Name = this.Name;
Active = this.Active;
return d;
}
Then, in my controller, I have the following method:
public ActionResult Create(DivisionViewModel divisionViewModel)
{
if (ModelState.IsValid)
{
Division division;
division = divisionViewModel.ToDivision();
_divisionService.Create(division);
return RedirectToAction("Index");
}
return View(divisionViewModel);
}
Division is not getting assigned. Why is this? I have a feeling it's something very simple that I'm just not seeing

In your function ToDivision, Name = this.Name doesn't do anything because they are the same value. You need to do d.Name = this.Name. Similarly for Active. Try:
public Division ToDivision()
{
Division d = new Division();
d.Name = this.Name;
d.Active = this.Active;
return d;
}

From your code example seems that you want to use object initialization in a method with property member assignment, but assigned to the Name member instead of d.Name. Try using the following initializer:
public Division ToDivision()
{
Division d = new Division()
{
Name = this.Name;
Active = this.Active;
}
return d;
}

Related

IComparer does not implement interface member - Error CS0738 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to sort two objects by one of their properties (.Transaction.topLeftX, an integer) using the following code to create a comparer to use in a Sort method:
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public Kairos.Net.RecognizeImage Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
if (x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX) <= 0) return x;
else return y;
}
}
However, I get the error message Error CS0738 'RecogniseFacesKairos.RespComp' does not implement interface member 'IComparer.Compare(RecognizeImage, RecognizeImage)'. 'RecogniseFacesKairos.RespComp.Compare(RecognizeImage, RecognizeImage)' cannot implement 'IComparer.Compare(RecognizeImage, RecognizeImage)' because it does not have the matching return type of 'int'.
Does the comparer used in the Sort method need to have return type int?
The IComparer<T> interface is supposed to implement a method that returns an int comparison. -1 for less than, 0 for equal and 1 for greater than.
Look at your code, if you're just comparing the top left, you can probably just do the following:
public int Compare(FooImage x, FooImage y) {
return x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX);
}
The desired outcome of sorting objects by one of their parameters was achieved by the following code:
...
Kairos.Net.KairosClient client = new Kairos.Net.KairosClient();
client.ApplicationID = appId;
client.ApplicationKey = appKey;
Kairos.Net.RecognizeResponse resp = client.Recognize(...);
RespComp SortImages = new RespComp();
resp.Images.Sort(SortImages);
...
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public int Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
return x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX);
}
}

How can I CAST a variable that's passed into a method? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I had this code repeated many times:
if (db2.Query<int>("SELECT 1 FROM CARDCHOICE WHERE CC = ?", (int)CC.JFBP1).Count == 0)
{
var temp10 = Enumerable.Range(0, 10).Select(i => new CardChoice { Cc = (int)CC.JFBP1, Number = i });
db2.InsertAll(temp10);
}
I tried to put this into a method that I called like this
InsertCC(CC.JFBP1, 10);
Here is the method
private static void InsertCC(CC cc, int qty )
{
var choice = int(cc);
if (db2.Query<int>("SELECT 1 FROM CARDCHOICE WHERE CC = ?", choice).Count == 0)
{
var temp = Enumerable.Range(0, qty).Select(i => new CardChoice { Cc = choice, Number = i });
db2.InsertAll(temp);
}
}
However what happens is that it tells me I cannot CAST the cc in the method with (int) and gives me "Error expression term int"
Can someone give me some advice as to how I could cast the cc that's passed in? I realize I could do the casting in the method call but would prefer not to do that as I have a lot of those calls.
This code isn't cast var choice = int(cc);
use var choice = (int)cc;
but you don't using cc parameter inside the method, so just pass int
private static void InsertCC(int choice, int qty )
{
if (db2.Query<int>("SELECT 1 FROM CARDCHOICE WHERE CC = ?", choice).Count == 0)
{
var temp = Enumerable.Range(0, qty).Select(i => new CardChoice { Cc = choice, Number = i });
db2.InsertAll(temp);
}
}
then your call will work
InsertCC(CC.JFBP1, 10);

Creating overload methods [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to create an overload method in visual studio with the method name getPrice(), here is the first overload method I tried to create:
private double getPrice(double price)
{
int intQty = 1;
txtQty.Text = intQty.ToString();
double dblSalesTax = 0;
lblSalesTax.Text = dblSalesTax.ToString();
double dblPrice = double.Parse(txtPrice.Text);
txtPrice.Text = dblPrice.ToString("c");
}
However my naming of it is off or something it keeps giving me an error, not all code paths return a double.. so I'm not sure how to fix that and this first overload method is supposed to only take a single parameter called price and then it is supposed to default Qty to 1 and sales tax to 0, besides the error did I do any of that other stuff correct or is the whole thing wrong or how would I fix that? Once I get this first parameter set I think I can get the other 2 working.
EDIT
Ok I changed it a bit...
private void btnCalculate_Click(object sender, EventArgs e)
{
getPrice(double price);
}
private double getPrice(double price)
{
double dblQty = 1;
double dblSalesTax = 0;
double dblPrice = double.Parse(txtPrice.Text);
double dblTotal = (dblPrice * dblQty) *dblSalesTax;
lblTotal.Text = dblTotal.ToString("c");
return dblTotal;
//lblSalesTax.Text = dblSalesTax.ToString();
//double dblPrice = double.Parse(txtPrice.Text);
//txtPrice.Text = dblPrice.ToString("c");
}
There is what I have now, how can I use the parameter price with it and why does it error when i try to put it in the btnCalculate_Click method?
You dont require parameter. You are not using the passed value inside function. You can return double value as given below:
`
private double getPrice()
{
int intQty = 1;
txtQty.Text = intQty.ToString();
double dblSalesTax = 0;
lblSalesTax.Text = dblSalesTax.ToString();
double dblPrice = double.Parse(txtPrice.Text);
txtPrice.Text = dblPrice.ToString("c");
return Convert.ToDouble(txtPrice.Text);
}
`

trouble with if statement and the > operator [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to write an if statement and i'm having trouble with my variables. it states operator > can not be applied to type int and string. code located below. both variables are displaying a int.
if (e.CmsData.Skill.InQueueInRing > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
}
else if (e.CmsData.Skill.AgentsAvailable > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
}
else
{
callsWaitingData.Text = "0";
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
}
This error couldn't really get much more descriptive.
operator > can not be applied to type int and string
if (e.CmsData.Skill.InQueueInRing > "0")
int -----^ ^--- string
Change it to
if (e.CmsData.Skill.InQueueInRing > 0)
Then both sides of the boolean logic is an int.

C# Enums with string properties [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Let's say I have enum with some strings in it like this:
enum MyEnum
{
stringA = "String a",
stringB = "String b"
}
Then, I try to access a string property of a string in this enum:
MyEnum.stringA.Length
And it doesn't let me. I can't use any of the string properties with the strings in the enum. Is it possible to access these string properties? or am I doing something wrong.
Thanks in advance.
You can use the DescriptionAttribute from the System.ComponentModel namespace.
enum MyEnum
{
[Description("String a")]
stringA,
[Description("String b")]
stringB
}
And then use this method to get description:
public static string GetDescription(Enum Enumeration)
{
string Value = Enumeration.ToString();
Type EnumType = Enumeration.GetType();
var DescAttribute = (DescriptionAttribute[])EnumType
.GetField(Value)
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return DescAttribute.Length > 0 ? DescAttribute[0].Description : Value;
}
And you can get the value:
var result = GetDescription(MyEnum.stringA).Length;

Categories