|
More than one box can be selected. Guess I'll look into the multi-select list box because I'm about to go crazy dealing with this issue.
|
|
|
|
|
If it's just a matter of knowing at any given time if any checkbox is checked, you can do the following:
bool ok = cb1.Checked || cb2.Checked || cb3.Checked || etc.
if ok is true, then at least 1 checkbox was checked; you don't have to repeat the "if's" elsewhere.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
How about using jQuery's .is() filter[^]:
var atLeastOneChecked = $('#Booker, #OA, #Hauler, #DA, #BAO, #OAO, #DAO').is(':checked');
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming,
That was EXACTLY what I needed! Now, all is well!! Thank you so much, Sir!
|
|
|
|
|
I'm trying to convert an old vb.net program into CSharp but I'm having some trouble accessing an unmanaged DLL - the DLL itself is for a NFS reader (CR95HF Demo Kit) and the application note is here:
[^] (a PDF file)
Just as an example I'm trying to access the gethardwareVersion function.
The problem I have is that the example says the declaration should look like
CR95HFDLL_getHardwareVersion(char* StringReply)
and the source that calls this is
char strAnswer[50]="";
int iresult
iresult = CR95HFDLL_GetHardwareVersion(strAnswer)
So far I have:
<pre>namespace RWH_DLL
{
public class RWH
{
[DllImport("CR95HF.dll")]
public static extern UInt32 CR95HFDLL_USBconnect();
[DllImport("CR95HF.dll")]
public static unsafe extern UInt32 CR95HFDLL_getHardwareVersion(char* returnString);
}
}
namespace ReadWriteHardware
{
public partial class RWHardware
{
static RWHardware singleInstance = null;
static readonly object padlock = new object();
RWHardware()
{ }
public static RWHardware Instance
{
get
{
lock (padlock)
{
if (singleInstance == null)
{
singleInstance = new RWHardware();
}
return singleInstance;
}
}
}
}
public partial class RWHardware
{
}
public partial class RWHardware
{
public Boolean GetRWBoard()
{
UInt32 blob = 0;
blob = RWH_DLL.RWH.CR95HFDLL_USBconnect();
String cmdString = "260100";
String rtnString = new string(Convert.ToChar(" "), 256);
char[] strAnswer = new char [50];
blob = RWH_DLL.RWH.CR95HFDLL_getHardwareVersion(strAnswer);
return false;
}
}
}
I'm struggling swapping between the pointers and the char arrays. I have had simpler functions from the dll working (those that just return a value and have no input). - the stuff in comments is all the functions I am converting and my vb versions of those functions.
|
|
|
|
|
You need to convert your managed char array into a pointer that is safe for the unmanaged code to use. To do this, you need to use the fixed keywords when you call the function
char[] strAnswer = new char [50];
fixed(char* pArray = strAnswer) {
blob = RWH_DLL.RWH.CR95HFDLL_getHardwareVersion(pArray);
}
return false;
you will have to also declare the GetRWBoard method as unsafe
Having said this, I prefer to use unmanaged memory when calling into native functions, and utilise the Marshal class for copying the data into managed memory. As such, I would write the routine as:
public static extern UInt32 CR95HFDLL_getHardwareVersion(IntPtr returnString);
IntPtr returnString = Marshal.AllocHGlobal(50);
blob = RWH_DLL.RWH.CR95HFDLL_getHardwareVersion(returnString);
string strAnswer = Marshal.PtrToStringANSI(returnString);
char[] arryAnswer = new char[50];
Marshal.Copy(returnString, arryAnswer, 0, 50);
Marshal.FreeHGlobal(returnString);
return false;
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
Absolutely perfect, works like a charm! (I'm using the marshal version)- the only tiny thing is that
PtrToStringANSI should be
PtrToStringAnsi .
for anyone else reading this solution, I used code like
string commandString = "260100";
IntPtr cmdString = Marshal.StringToHGlobalAnsi(commandString);
blob = RWH_DLL.RWH.CR95HFDll_SendReceive(cmdString, returnString);
to enter commands into the dll.
|
|
|
|
|
Also take a look at http://www.pinvoke.net - they show many examples for common Windows functions. Some of them may have similar signatures like the functions you require, so you can use their way of invoking them.
|
|
|
|
|
Hello there, I need to do the same thing, I'm trying to import the C or VB DLL into C#, but it's the first time that I try to do a similar thing.. Any suggestions for where to start?
My task is really simple, I just have to Detect another NFC tag and then to Write and Read one message.
Did you finish successfully your task? Or it's better to use another NFC tag reader with available C# DLL?
Greetings Davide.
|
|
|
|
|
Hello forum,
actually i'm trying to build my first C# GUI and first C# program ever.
I used Glade to design the GUI. But after compiling and debug-run i'm getting:
/usr/bin/mono --debug --debugger-agent=transport=dt_socket,server=y,address=127.0.0.1:55555,setpgid=y /home/sascha/RiderProjects/PublicanCreators/PublicanCreators/bin/Debug/PublicanCreators.exe
Gtk-Message: (for origin information, set GTK_DEBUG): failed to retrieve property `gtk-primary-button-warps-slider' of type `gboolean' from rc file value "((GString*) 0x20b82e0)" of type `gboolean'
(PublicanCreators:7404): libglade-WARNING **: Expected <glade-interface>. Got <interface>.
(PublicanCreators:7404): libglade-WARNING **: did not finish in PARSER_FINISH state
(PublicanCreators:7404): libglade-CRITICAL **: glade_xml_get_widget: assertion 'self != NULL' failed
(PublicanCreators:7404): libglade-CRITICAL **: glade_xml_get_widget: assertion 'self != NULL' failed
(PublicanCreators:7404): libglade-CRITICAL **: glade_xml_get_widget: assertion 'self != NULL' failed
(PublicanCreators:7404): libglade-CRITICAL **: glade_xml_get_widget: assertion 'self != NULL' failed
(PublicanCreators:7404): libglade-CRITICAL **: glade_xml_signal_autoconnect_full: assertion 'self != NULL' failed
The gui.glade can be found there: PublicanCreators/gui.glade at master · saigkill/PublicanCreators · GitHub[^]
Can anyone help me to get the GUI running, please?
Greetings
Sascha
|
|
|
|
|
I found it out, why it doesn't work. The Glade file was produced with Glade 3.x. My implementation was for using with the old libglade library. But since some time this library was replaced by Gtk.Builder. This one can now handle the Glade 3.x output.
If anyone has such problem again, so you can start there with your journey: GtkSharp - Part 3 - Basic Example with VS and Glade - The Grbd Blog[^]
|
|
|
|
|
I'm trying to calculate the area of different shapes using Strategy pattern and below are my code thus far.
public class Shape
{ public string Name{get;set;}
}
public class Circle:Shape
{ public double Radius{get;set;}
public Circle (string name, double radius)
{Name = name; Radius = radius;}
}
public class Square:Shape
{ public double Side {get; set;}
public Square(string name, double side)
{ Name = name; Side = side;}
}
public interface ICalculateAreaStrategy
{ double Calculate(Shape shape);
}
public class CalculateCircleAreaStrategy: ICalculateAreaStrategy
{ double Calculate(Shape shape)
{
return Math.Pow(shape.Radius,2)*3.14;
}
}
public class CalculateSquareAreaStrategy: ICalculateAreaStrategy
{ double Calculate(Shape shape)
{
return shape.Side * shape.Side
}
}
public class CalculateAreaService
{ readonly ICalculateAreaStrategy calculateAreaStrategy;
public CalculateAreaService(ICalculateAreaStrategy strategy)
{
calculateAreaStrategy = strategy;
}
double CalculateArea(Shape shape)
{
return calculateAreaStrategy.Calculate(shape);
}
}
The reason I'm stuck is I cannot get access to the Circle or Square class from the Shape object passed to the Calculate() method in the CalculateCircleAreaStrategy and CalculateSquareAreaStrategy classes.
modified 17-Jan-17 4:37am.
|
|
|
|
|
The reason you can't access it is because you are passing in a Shape which does not have the relevant parameters because they are defined in the subclasses. If you know that you are passing in a certain type, then you just need to cast them before you try to access them. For instance, in the Circle strategy, your Calculate method could be converted to this:
double Calculate(Shape shape)
{
Circle circle = shape as Circle;
if (circle == null)
{
throw new InvalidOperationException("You didn't pass in a circle");
}
return Math.Pow(circle.Radius, 2)* 3.14;
}
This space for rent
|
|
|
|
|
Thanks, that's exactly what I needed.
|
|
|
|
|
This isn't what the strategy pattern is for anyway.
You could just define these methods on their respective classes, it doesn't make sense to calculate the area of a circle as if it was a square, and vice versa. So changing the strategy doesn't even apply to this situation. Even if you do apply it, a strategy is supposed is not supposed to work only conditionally, they're supposed to be usable interchangeably - that's their whole point.
|
|
|
|
|
Thanks for replying. I'll look into this topic more.
|
|
|
|
|
 To answer your specific question you need to up caste to the type you need
public class Shape
{
public string Name { get; set; }
}
public class Circle : Shape
{
public double Radius { get; set; }
public Circle(string name, double radius)
{ Name = name; Radius = radius; }
}
public class Square : Shape
{
public double Side { get; set; }
public Square(string name, double side)
{ Name = name; Side = side; }
}
public interface ICalculateAreaStrategy
{
double Calculate(Shape shape);
}
public class CalculateCircleAreaStrategy : ICalculateAreaStrategy
{
public double Calculate(Shape shape)
{
Circle c = shape as Circle;
if (c == null)
{
return 0;
}
return 3.14 * c.Radius * c.Radius;
}
}
public class CalculateSquareAreaStrategy : ICalculateAreaStrategy
{
public double Calculate(Shape shape)
{
Square s = shape as Square;
if (s == null)
{
return 0;
}
return s.Side * s.Side;
}
}
public class CalculateAreaService
{
readonly ICalculateAreaStrategy calculateAreaStrategy;
public CalculateAreaService(ICalculateAreaStrategy strategy)
{
calculateAreaStrategy = strategy;
}
public double CalculateArea(Shape shape)
{
return calculateAreaStrategy.Calculate(shape);
}
}
Usage
CalculateAreaService cas = new CalculateAreaService(new CalculateSquareAreaStrategy());
Console.WriteLine(cas.CalculateArea(new Square("Square", 3)));
cas = new CalculateAreaService(new CalculateCircleAreaStrategy());
Console.WriteLine(cas.CalculateArea(new Circle("Circle", 3)));
Console.ReadLine();
However the strategy pattern isn't that great for things that require different parameters such as you have with Square, Circle etc. I appreciate you're doing this to learn the strategy pattern and this might not be your ultimate use for it, but if you wanted to do something like you're doing then you could do it more simply like this
public interface ICalculateArea
{
double Calculate();
}
public class CalculateCircleArea : ICalculateArea
{
private Circle circle;
public CalculateCircleArea(Circle circle)
{
this.circle = circle;
}
public double Calculate()
{
return 3.14 * circle.Radius * circle.Radius;
}
}
public class CalculateSquareArea : ICalculateArea
{
private Square square;
public CalculateSquareArea(Square square)
{
this.square = square;
}
public double Calculate()
{
return square.Side * square.Side;
}
}
Usage
List<ICalculateArea> calcs = new List<ICalculateArea>();
calcs.Add(new CalculateSquareArea(new Square("Square", 3)));
calcs.Add(new CalculateCircleArea(new Circle("Circle", 3)));
foreach (var calc in calcs)
{
Console.WriteLine(calc.Calculate());
}
Or even simpler still have Square, Circle etc implement ICalculateArea and return their own area.
|
|
|
|
|
Thank you for your time, I would not have known that the Strategy pattern isn't suited for my situation if you hadn't shared your thoughts.
Can you please explain why "the strategy pattern isn't that great for things that require different parameters". I've checked the definition of the Strategy pattern and there wasn't anything that says it shouldn't be used on situations that are similar to mine.
The following are part of the definition of the Strategy pattern I've found online.
* "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it." (Gang of Four)
* "Specifies a set of classes, each representing a potential behaviour. Switching between those classes changes the application behaviour. (The Strategy)
From my perspective all of my classes which inherit from the ICalculateAreaStrategy interface have the same exact method and it takes the same exact number and type of parameter no matter which class it's used in.
modified 17-Jan-17 13:40pm.
|
|
|
|
|
Liagapi wrote: vary independently from clients That's the point: in your case, a Circle requires the algorithm for a circle, not for a rectangle, etc.: the algorithm absolutely depends on the client.
Think of something different, say a chess game. How to find out the next move? You could use "brute force" calculating a few moves ahead, or you could implement some "artificial intelligence strategy". There could be external constraints, e.g. how much time you may use for calculating the next move, and hence apply different algorithms with different trade-offs.
Select the algorithm applicable for the current scenario - and with the next scenario use an algorithm for that scenario.
|
|
|
|
|
My first impulse would have been to use polymorphism:
Polymorphism (C# Programming Guide)
(The example looks very familiar).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have a SQL table with a Started column that is of type DateTimeOffset.
In a Linq To Entities query I'm trying to query against a DateTime value:
.
.
.
where t.Started.Value.DateTime >= startDT
.
.
.
I get "The specified type member 'DateTime' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
I've never used a DateTimeOffset. What's the right way to query this field using Linq To Entities?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
There are many things you can't do with L2E - sadly, this is one of them. What you need to do is materialize the data into something like a list first, and then apply the filtering to the list.
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: What you need to do is materialize the data into something like a list first
I thought of that, but what this means is first querying the ENTIRE table into a list, then applying the date check. This obviously won't work.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Turns out that it works with Linq To SQL, but not Linq To Entities.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I know - that's why I said this is one of the things you can't do with L2E.
This space for rent
|
|
|
|