In Visual Studio I get this error
Argument 1: cannot convert from 'Sintra.frmK1s.ProductStockUpdate' to
'Sintra.ScannetWebService.ProductUpdate'
on this line
int[] updatedResults = Client.Product_Update(updateProduct);
I can't figure it out how I sove this.
Can anyone help?
My code on frmK1s.cs:
using System;
using Sintra.ScannetWebService;
class ProductStockUpdate
{
public string Id;
public int Stock;
}
ProductStockUpdate updateProduct = new ProductStockUpdate();
updateProduct.Id = 5242;
updateProduct.Stock = 100;
int[] updatedResults = Client.Product_Update(updateProduct);
On Sintra.ScannetWebService
public int[] Product_Update(Sintra.ScannetWebService.ProductUpdate ProductData) {
Sintra.ScannetWebService.Product_UpdateRequest inValue = new Sintra.ScannetWebService.Product_UpdateRequest();
inValue.ProductData = ProductData;
Sintra.ScannetWebService.Product_UpdateResponse retVal = ((Sintra.ScannetWebService.WebServicePort)(this)).Product_Update(inValue);
return retVal.Product_UpdateResult;
}
Look at the web service:
public int[] Product_Update(Sintra.ScannetWebService.ProductUpdate ProductData) {
Sintra.ScannetWebService.Product_UpdateRequest inValue = new Sintra.ScannetWebService.Product_UpdateRequest();
inValue.ProductData = ProductData;
Sintra.ScannetWebService.Product_UpdateResponse retVal = ((Sintra.ScannetWebService.WebServicePort)(this)).Product_Update(inValue);
return retVal.Product_UpdateResult;
}
The method is expecting Sintra.ScannetWebService.ProductUpdate object type
You are passing ProductStockUpdate object type:
int[] updatedResults = Client.Product_Update(updateProduct);
class ProductStockUpdate
{
public string Id;
public int Stock;
}
Please pass the correct object to the given method: Sintra.ScannetWebService.ProductUpdate object type:
Sintra.ScannetWebService.ProductUpdate = new Sintra.ScannetWebService.ProductUpdate();
Related
I don't really know if this is the correct method, but I wanna return an array of arrays in order to call the element of it in another function later.
static Values RetrieveXML(string XMLFile)
{
using var reader = XmlReader.Create(XMLFile);
reader.ReadToFollowing("ticket");
reader.ReadToFollowing("lignes");
string nombre = reader.GetAttribute("nombre");
int ligne = int.Parse(nombre);
Values[] _Values = new Values[ligne];
for(int i=0; i<ligne; i++){
_Values[i] = new Values();
reader.ReadToFollowing("article");
_Values[i].code = reader.GetAttribute("code");
_Values[i].qty = reader.GetAttribute("quantite");
_Values[i].net_price = reader.GetAttribute("net");
_Values[i].net_ht_price = reader.GetAttribute("net_ht");
_Values[i].tva = reader.GetAttribute("taxes");
_Values[i].num = reader.GetAttribute("numero");
_Values[i].valeur = reader.GetAttribute("base_ht");
_Values[i].remise = reader.GetAttribute("valeur_remise");
reader.ReadToFollowing("libelle");
_Values[i].libelle = reader.ReadElementContentAsString();
Console.WriteLine("-----------Using XMLToOrderLine--------------");
Console.WriteLine(_Values[i].code);
Console.WriteLine(_Values[i].valeur);
}
return _Values[];
}
public class Values
{
public string code;
public string qty;
public string remise;
public string valeur;
public string tva;
public string libelle;
public string num;
public string net_ht_price;
public string net_price;
}
And then after that I wanna call by example: Values[1].code in another function, how can I do that?
change your code like this:
static Values[] RetrieveXML(string XMLFile)
{
using var reader = XmlReader.Create(XMLFile);
reader.ReadToFollowing("ticket");
reader.ReadToFollowing("lignes");
string nombre = reader.GetAttribute("nombre");
int ligne = int.Parse(nombre);
Values[] _Values = new Values[ligne];
for(int i=0; i<ligne; i++){
_Values[i] = new Values();
reader.ReadToFollowing("article");
_Values[i].code = reader.GetAttribute("code");
_Values[i].qty = reader.GetAttribute("quantite");
_Values[i].net_price = reader.GetAttribute("net");
_Values[i].net_ht_price = reader.GetAttribute("net_ht");
_Values[i].tva = reader.GetAttribute("taxes");
_Values[i].num = reader.GetAttribute("numero");
_Values[i].valeur = reader.GetAttribute("base_ht");
_Values[i].remise = reader.GetAttribute("valeur_remise");
reader.ReadToFollowing("libelle");
_Values[i].libelle = reader.ReadElementContentAsString();
Console.WriteLine("-----------Using XMLToOrderLine--------------");
Console.WriteLine(_Values[i].code);
Console.WriteLine(_Values[i].valeur);
}
return _Values;
}
just try it.
I am making an "error code to String" converter that would display the name of an error code from its value, for example 0x000000c3 would give "Class not found", but using MY OWN error codes!
Here's how it actually looks like:
#region errcodes
public int NORMAL_STOP = 0x00000000;
public int LIB_BROKEN = 0x000000a1;
public int RESOURCE_MISSING = 0x000000a2;
public int METHOD_NOT_FOUND = 0x000000a3;
public int FRAMEWORK_ERROR = 0x000000b1;
public int UNKNOWN = 0x000000ff;
#endregion
public string getName(int CODE)
{
}
I would like to get a string value from parameter CODE, in function getName.
How can I do that ?
A good C# practice is using of an enum:
public enum ErrorCode
{
NORMAL_STOP = 0x00000000,
LIB_BROKEN = 0x000000a1,
RESOURCE_MISSING = 0x000000a2,
METHOD_NOT_FOUND = 0x000000a3,
FRAMEWORK_ERROR = 0x000000b1,
UNKNOWN = 0x000000ff
}
public const string InvalidErrorCodeMessage = "Class not found";
public static string GetName(ErrorCode code)
{
var isExist = Enum.IsDefined(typeof(ErrorCode), code);
return isExist ? code.ToString() : InvalidErrorCodeMessage;
}
public static string GetName(int code)
{
return GetName((ErrorCode)code);
}
Another good advice: it would be great to use the C# naming convention for error codes:
public enum ErrorCode
{
NormalStop = 0x00000000,
LibBroken = 0x000000a1,
ResourceMissing = 0x000000a2,
MethodNotFound = 0x000000a3,
FrameworkError = 0x000000b1,
Unknown = 0x000000ff
}
Usage example:
void Main()
{
Console.WriteLine(GetName(0)); // NormalStop
Console.WriteLine(GetName(1)); // Class not found
Console.WriteLine(GetName(ErrorCode.Unknown)); // Unknown
}
Here is the code snippet from my LinqPad:
public class Elephant{
public int Size;
public Elephant()
{
Size = 1;
}
}
public struct Ant{
public int Size;
}
private T[] Transform2AnotherType<T>(Elephant[] elephantList)
where T:new()
{
dynamic tArray = new T[elephantList.Length];
for (int i = 0; i < elephantList.Length; i++)
{
tArray[i] = new T();
tArray[i].Size = 100;
//tArray[i].Dump();
}
return tArray;
}
void Main()
{
var elephantList = new Elephant[2];
var elephant1 = new Elephant();
var elephant2 = new Elephant();
elephantList[0] = elephant1;
elephantList[1] = elephant2;
elephantList.Dump();
var r = Transform2AnotherType<Ant>(elephantList);
r.Dump();
}
I want to change one object array of known type,Elephant,to another object array of type T. T is not a class,but limited to struct which
provided by the already existed API.And every instance of type T shares some common property,says Size,but also has their own particular property which
I have omitted in my example code.So I put dynamic keyword inside the Transform2AnotherType<T>.
And I could not even to use Dump to make sure if the assignment has made effect,thus will throw RuntimeBinderException.
My question is: how to correctly make the assignment in such a struct array and return it back properly?
I suggest change your code like this:
public class Elephant
{
public Elephant()
{
Size = 1;
}
public int Size { get; set; }
}
public struct Ant
{
public int Size { get; set; }
}
private static T[] Transform2AnotherType<T>(Elephant[] elephantList)
where T : new()
{
T[] tArray = new T[elephantList.Length];
for (int i = 0; i < elephantList.Length; i++)
{
dynamic arrayElement = new T();
arrayElement.Size = 100;
tArray[i] = arrayElement;
//tArray[i].Dump();
}
return tArray;
}
static void Main()
{
var elephantList = new Elephant[2];
var elephant1 = new Elephant();
var elephant2 = new Elephant();
elephantList[0] = elephant1;
elephantList[1] = elephant2;
//elephantList.Dump();
var r = Transform2AnotherType<Ant>(elephantList);
//r.Dump();
}
I have the following class:
public class TestQuestionHeader
{
public int Id { get; set; }
public int QId { get; set; }
}
and JSON methods:
public static string ToJSONString(this object obj)
{
using (var stream = new MemoryStream())
{
var ser = new DataContractJsonSerializer(obj.GetType());
ser.WriteObject(stream, obj);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
public static T FromJSONString<T>(this string obj)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(obj)))
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
T ret = (T)ser.ReadObject(stream);
return ret;
}
}
I am using these as follows:
IList<TestQuestionHeaders> testQuestionHeaders = xxx;
string test.Questions = JSON.ToJSONString(testQuestionHeaders);
var a = JSON.FromJSONString<TestQuestionHeader>(test.Questions);
Can someone help me to explain why the variable a contains TestQuestionHeader
instead of IList<TestQuestionHeader>
Your FromJSONString method returns an instance of type T. Your call in your example passes TestQuestionHeader as T. You just need to supply the correct type for T.
You'll need to change that line to:
var a = JSON.FromJSONString<List<TestQuestionHeader>>(test.Questions);
That's because your passing TestQuestionHeader as generic type parameter. Try following:
IList<TestQuestionHeader> a = JSON.FromJSONString<List<TestQuestionHeader>>(test.Questions);
Use JSON.NET, the best JSON library for .NET.
See: http://james.newtonking.com/json/help/index.html
I saw an example on MSDN where it would let you specify the default value if nothing is returned. See below:
List<int> months = new List<int> { };
int firstMonth2 = months.DefaultIfEmpty(1).First();
Is it possible to use this functionality with an object? Example:
class object
{
int id;
string name;
}
code:
List<myObjec> objs = new List<myObjec> {};
string defaultName = objs.DefaultIfEmpty(/*something to define object in here*/).name;
UPDATE:
I was thinking I could do something like this:
List<myObjec> objs = new List<myObjec> {};
string defaultName = objs.DefaultIfEmpty(new myObjec(-1,"test")).name;
But haven't been able to. It should be noted that I am actually trying to use this method on an object defined in my DBML using LINQ-To-SQL. Not sure if that makes a difference in this case or not.
You need to pass an instantiated class as a parameter of the DefaultIfEmpty.
class Program
{
static void Main(string[] args)
{
var lTest = new List<Test>();
var s = lTest.DefaultIfEmpty(new Test() { i = 1, name = "testing" }).First().name;
Console.WriteLine(s);
Console.ReadLine();
}
}
public class Test
{
public int i { get; set; }
public string name { get; set; }
}
To add to it and make it a bit more elegant (IMO) add a default constructor:
class Program
{
static void Main(string[] args)
{
var lTest = new List<Test>();
var s = lTest.DefaultIfEmpty(new Test()).First().name;
Console.WriteLine(s);
Console.ReadLine();
}
}
public class Test
{
public int i { get; set; }
public string name { get; set; }
public Test() { i = 2; name = "testing2"; }
}
As per the MSDN page on this Extension Method you can do what you want:
http://msdn.microsoft.com/en-us/library/bb355419.aspx
Check the sample on this page for an example on how to use this with an object.
i must admit i am not too sure i understand your question, but i'll try to suggest using double question mark if the returned object might be null. Like so:
myList.FirstOrDefault() ?? new myObject();
You can create a default Object Like this:
Object o_Obj_Default = new Object();
o_Obj_Default.id = 3;
o_Obj_Default.name = "C";
And add it to your default value :
string defaultName = objs.DefaultIfEmpty(o_Obj_Default).First().name;
If your list "objs" is empty, the result will be "C"