|
Hi All,
I have a struct that contains data such as ID, Name etc.
I have a List of instances of that struct.
I then have a method which returns a struct from the list based on an ID value pased as a parameter.
But my problem is if the id does not match any of the structs in the list I would like to return a Null value. Any ideas how to get around this?
I have thought to have a bool property in the struct that states if null or not null, but i would ideally prefer to return a null value rather then create an empty struct with only IsNull = true set and then returning that.
Thanks for any advice offered
|
|
|
|
|
Make your method return an instance of Nullable<struct> (reference[^]).
It automatically adds an "HasValue" property and can be also compared with null.
|
|
|
|
|
Well I will mark as answer as it does answer the question I posted , but appears this way means the struct values are read-only.
I think I will stick with the IsNull property for ease of use, thanks thou
|
|
|
|
|
While it's true that the "Value" property is read-only, you can always assign your nullable variable to an instance of the struct using the = operator. See the following code snipper
public struct MyStruct
{
public int Field1 = 0;
public int Field2 = 1;
};
class Program
{
static void Main(string[] args)
{
MyStruct str = new MyStruct();
str.Field1 = 1;
str.Field2 = 2;
Nullable<MyStruct> nul = null;
if (nul.HasValue)
Console.WriteLine("VALUE IS {0},{1}", nul.Value.Field1, nul.Value.Field2);
else
Console.WriteLine("VALUE IS EMPTY");
nul = str;
if (nul.HasValue)
Console.WriteLine("VALUE IS {0},{1}", nul.Value.Field1, nul.Value.Field2);
else
Console.WriteLine("VALUE IS EMPTY");
}
}
|
|
|
|
|
Thanks, but you cant assign nullable struct to struct i.e. str = nul;
which means I wouldnt be able to save the str back to the list, without copying the values from null to str first.
|
|
|
|
|
Yes, you can't do that because you don't know if nul has value or not.
You can do str = nul.Value, or, you can use a list of nullable structs from the beginning.
|
|
|
|
|
Hi,
if you need to hold some data and the ability to indicate there is no data, use a class instead
of a struct, and null for "no such thing".
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:54 AM
|
|
|
|
|
Or define an Empty member to return for those cases:
if ( x != thing.Empty ) ...
|
|
|
|
|
hi,
When I click one button.I called One webservice that webservice have one method.it will return one Image byte Array.I want to display that Image via Javascript.ie.Displaying Image From Server with the help of Webservice and Javascript.
Is it Possible?Or Give Me another solution.
Please Help Me.
With Regards,
Justin Dhas. Y
|
|
|
|
|
justindhas wrote: Give Me another solution
Write an active X control.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hi
Thanks for ur reply.Other than ActiveX any possible.
Thanks & Regards,
Justin Dhas.
|
|
|
|
|
helu evry one...
i need help in starting a windows service from a windows form.. the windows servics is manualy sarted and i need to start from the windows form. the windows service is windows service2. thans..
|
|
|
|
|
System.ServiceProcess.ServiceController can be used to start a service by providing a service name ...
I think it looks like the following way...
<br />
ServiceController service = new ServiceController(serviceName);
<br />
service.start()
Use Google for ServiceController implementation.
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Thank you very uch for the code...
|
|
|
|
|
I have an array list of serial port configuration. i want to call these configuration later in the program. So how can i call the configuration from array ?
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
using System.Data;
using System.Drawing;
public class Class1
{
public Class1()
{
}
public ArrayList SerialPortContainer = new ArrayList();
private string config_file = "C:/achu/config.txt";
private string PhoneNumber;
private string ServiceCentre;
public bool test()
{
try
{
string line;
TextReader tr = new StreamReader(config_file);
System.IO.StreamReader objReader;
objReader = new System.IO.StreamReader(config_file);
int NumberOfLines = 0;
while ((line = tr.ReadLine()) != null)
{
NumberOfLines++;
}
string[] ListLines = new string[NumberOfLines];
for (int i = 0; i < NumberOfLines; i++)
{
ListLines[i] = objReader.ReadLine();
string[] values = ListLines[i].Split(new char[] { '$', '#' });
string[] PortSplit = values[0].Split(new char[] { ':', '#' });
string PortName = PortSplit[1];
string[] BaudSplit = values[1].Split(new char[] { ':', '#' });
int BaudRate = Convert.ToInt32(BaudSplit[1]);
string[] BitsSplit = values[2].Split(new char[] { ':', '#' });
int DataBits = Convert.ToInt32(BitsSplit[1]);
string[] NumberSplit = values[3].Split(new char[] { ':', '#' });
PhoneNumber = NumberSplit[1];
string[] SerNumberSplit = values[4].Split(new char[] { ':', '#' });
ServiceCentre = SerNumberSplit[1];
SerialPort serialPort1 = new SerialPort();
serialPort1.PortName = PortName;
serialPort1.Handshake = Handshake.RequestToSend;
serialPort1.BaudRate = BaudRate;
serialPort1.DataBits = DataBits;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DtrEnable = true;
serialPort1.Open();
SerialPortContainer.Add(serialPort1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
public void ATTest()
{
string val = Convert.ToString(SerialPortContainer[1]);
MessageBox.Show(val);
}
}
|
|
|
|
|
[Cannot understand the question clearly]...hmmm..Ok if you want to access those array values in the ATTest() method,declare those array variables as global variables i mean at class level not inside the method Test().
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Using a field in the class, like myConfigArray? If you have to use it in another class think about using static fields that are public.
Regards
Sebastian
|
|
|
|
|
hiii
First Option:
Pass The Array to the function...
Second Option:
Declare The Arryas as "Class" Private Members.....
|
|
|
|
|
Hello there.
I'm developing a C# application that interracts with a contactless smart card readers ( MiFare 1k and MiFare 2k readers), but since those kind of card readers dont support PS/SC the WinScard API won't help.
(For a start ... i need a list of all connected contactless smart card readers )
So i suppose i should loop through all connected usb devices and send some APDU command so they can give their status ( identify them if they are contactless smart card readers )
Is that the right way to do that ? If someone has any information about this issue ... i'm opened to any suggestions
I've been searching around the web for about 3 whole days and i'm nowhere .... Please help me
|
|
|
|
|
You'd normally go to the manufacturer and get the Software Development Kit to do this. I can't believe a manufacturer put out a piece of hardware and didn't supply an SDK so you can use it in your own code.
|
|
|
|
|
Thank you for your response.
Unfortunatly my task is to develope an access management system that interracts with all card readers in a specific building. And unfortunately the card readers in that building are not from the same manifacturer. ( The only thing in common between them is that they are conctactless card readers and reader MiFare 1k and MiFare 2k cards. )
Yes, i have a sdk which works fine with the specific reader provided to me, but ... i dont think it will "work" with the other devices.
So I was thinking of a universal low level usb communication with the card readers, hoping they "work the same way behind the scenes". And folowing that logic i got stuck here http://www.codeproject.com/script/Forums/View.aspx?fid=1649&select=2906409&tid=2904097[^]
modified on Monday, February 2, 2009 3:37 AM
|
|
|
|
|
Well, unfortunately for you, the commands, responses, and communication protocol between the different card readers are, more-than-likely, not the same. You're going to end up writing a seperate communication module for each card reader you have. The SDK for each of them is the only thing that's going to simplify this process.
|
|
|
|
|
Thank you for your response!
I was afraid , but not surpsised by your answer.
Since those devices don't support PS/SC, there is no universal way to achieve my goal.
However when i pass this hell, i'm going to write an article about contactless cards and contactless card readers i codeproject (actually ... as surprising as it sounds ... there is not even one GOOD article about that in the web - i've been researching like a whole week )
Thanks again for the information !
Best regards, Hristiyan ....
|
|
|
|
|
I have been through this hell, and it is possible to write a program that will process a couple of different cards. However, I have done it with C++ and not C#. I will try to give you an idea of how to do this in C#.
What I did, was that I had a Mifare and a 15693 contactless mix. The readers were made by entirely different companies. Each reader has its own dll and there is unmanaged code to and from the reader, so in C# you must use delegates for you methods.
You must also add the references (dlls to the project). Then you create a reader object for every kind of reader that you have dlls for.
I had a dll which was able to read and code to differentiate the various RFID technologies, and using that bit of code snippet, I was able to determine what dlls and reader objects to use.
The SDKs were fairly expensive, but I can give you a C++ code snippet on how to determine the various technologies: (you will notice that this can determine 1so15693, 14443A, 14443B, iso8000, felica
CIso_benchDlg::CIso_benchDlg(CWnd* pParent )
: CDialog(CIso_benchDlg::IDD, pParent)
{
m_tagid = _T("");
m_type = _T("");
m_iso15693 = FALSE;
m_iso14443A = FALSE;
m_iso14443B = FALSE;
m_iso18000 = FALSE;
m_felica = FALSE;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CIso_benchDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_DETECT, m_button);
DDX_Text(pDX, IDC_TAGID, m_tagid);
DDX_Text(pDX, IDC_TYPE, m_type);
DDX_Check(pDX, IDC_CHECK1, m_iso15693);
DDX_Check(pDX, IDC_CHECK2, m_iso14443A);
DDX_Check(pDX, IDC_CHECK3, m_iso14443B);
DDX_Check(pDX, IDC_CHECK4, m_iso18000);
DDX_Check(pDX, IDC_CHECK5, m_felica);
}
BEGIN_MESSAGE_MAP(CIso_benchDlg, CDialog)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_DETECT, OnDetect)
END_MESSAGE_MAP()
BOOL CIso_benchDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CenterWindow(GetDesktopWindow());
SetTimer(IDC_TIMER, 20, NULL);
UpdateData(FALSE);
return TRUE;
}
void CIso_benchDlg::OnTimer(UINT nIDEvent)
{
if (detect){
char tag_id[600];
tag_id[0]=0;
int temp=0;
int success=0;
m_iso15693=false;
m_iso14443A=false;
m_iso14443B=false;
m_iso18000=false;
m_felica=false;
C1_disable(); C1_enable();
if(success=get_14443A(&my_tag_14443A)) {
if(my_tag_14443A.type==MIFARE_CL) m_type="MiFare Classic";
else if(my_tag_14443A.type==MIFARE_UL) m_type="MiFare UltraLight";
else if(my_tag_14443A.type==MIFARE_DF) m_type="MiFare DESFire";
else m_type="14443A";
m_iso14443A=true;
C1_disable(); C1_enable();
}
if(temp) success=true;
if(temp=get_15693(&my_tag_15693,NULL)) {m_type="ISO 15693"; m_iso15693=true; C1_disable(); C1_enable();}
if(temp) success=true;
if(m_type!="ISO 15693" && (temp=get_18000(&my_tag_18000))) {m_type="ISO 18000"; m_iso18000=true; C1_disable(); C1_enable();}
if(temp) success=true;
if(temp=get_IC(&my_tag_IC, IC_15693, false, false)) {m_type="INSIDE"; m_iso15693=true; C1_disable(); C1_enable();}
if(temp) success=true;
if(temp=get_IC(&my_tag_IC, ISO_14443B3, false, false)) {m_type="INSIDE"; m_iso14443B=true; C1_disable(); C1_enable();}
if(temp) success=true;
if(temp=get_IC(&my_tag_IC, IC_14443B, false, false)) {m_type="INSIDE"; m_iso14443B=true; C1_disable(); C1_enable();}
if(temp) success=true;
if(temp=get_14443B(&my_tag_14443B)) {m_type="14443B"; m_iso14443B=true; C1_disable(); C1_enable();}
if(temp) success=true;
C1_disable(); C1_enable();
if(temp=get_14443B_SRI(&my_tag_14443B)) {m_type="14443B"; m_iso14443B=true; C1_disable(); C1_enable();}
if(temp) success=true;
C1_disable(); C1_enable();
if(temp=get_14443B_SR176(&my_tag_14443B)) {m_type="14443B"; m_iso14443B=true; C1_disable(); C1_enable();}
if(temp) success=true;
if(temp=get_FeliCa(&my_tag_FeliCa)) {m_type="FeliCa"; m_felica=true; C1_disable(); C1_enable();}
if(temp) success=true;
if (success){
success=0;
if(m_type=="ISO 15693") {
m_tagid="";
for(int i=0;i<my_tag_15693.id_length;i++) sprintf(tag_id,"%s%02X",tag_id,my_tag_15693.tag_id[i]);
m_tagid=tag_id;
}
else if(m_type=="MiFare Classic" || m_type=="MiFare UltraLight" || m_type=="MiFare DESFire" || m_type=="14443A") {
m_tagid="";
for(int i=0;i<7;i++) sprintf(tag_id,"%s%02X",tag_id,my_tag_14443A.tag_id[i]);
m_tagid=tag_id;
}
else if(m_type=="INSIDE") {
m_tagid="";
for(int i=0;i<8;i++) sprintf(tag_id,"%s%02X",tag_id,my_tag_IC.tag_id[i]);
m_tagid=tag_id;
}
else if(m_type=="ISO 18000") {
m_tagid="";
for(int i=0;i<my_tag_18000.data_length;i++) sprintf(tag_id,"%s%02X",tag_id,my_tag_18000.data[i]);
m_tagid=tag_id;
}
else if(m_type=="FeliCa") {
m_tagid="";
for(int i=0;i<my_tag_FeliCa.id_length;i++) sprintf(tag_id,"%s%02X",tag_id,my_tag_FeliCa.tag_id[i]);
m_tagid=tag_id;
}
else if(m_type=="14443B") {
m_tagid="";
for(int i=0;i<my_tag_14443B.id_length;i++) sprintf(tag_id,"%s%02X",tag_id,my_tag_14443B.tag_id[i]);
m_tagid=tag_id;
}
UpdateData(FALSE);
}
else{
if (m_tagid == "SEARCHING...") {
m_tagid = "SEARCHING.";
m_type = "SEARCHING.";
}
else {
m_tagid = "SEARCHING...";
m_type = "SEARCHING...";
}
UpdateData(FALSE);
}
}
CDialog::OnTimer(nIDEvent);
}
void CIso_benchDlg::OnDetect()
{
if(detect==FALSE) {
if(!C1_open_comm())
{
m_tagid = "Failed To Connect..";
m_type = "";
UpdateData(false);
C1_close_comm();
return;
}
if(!C1_enable())
{
m_tagid = "Failed To Enable..";
m_type = "";
UpdateData(false);
C1_close_comm();
return;
}
detect = TRUE;
m_button.SetWindowText(L"STOP DETECTION");
m_tagid = "";
m_type = "";
m_iso15693=false;
m_iso14443A=false;
m_iso14443B=false;
m_iso18000=false;
m_felica=false;
UpdateData(false);
} else {
C1_disable();
C1_close_comm();
detect = FALSE;
m_button.SetWindowText(L"DETECT!");
m_tagid = "";
m_type = "";
m_iso15693=false;
m_iso14443A=false;
m_iso14443B=false;
m_iso18000=false;
m_felica=false;
UpdateData(false);
}
}
Hope this helps.
|
|
|
|
|
we can populate Web Browser Control using DataFilePath and set the xml file path to DataFilePath property. i want to know how to populate web browser control with xml instead of using DataFilePath property of Web Browser Control. if possible the please let me know with sample code snippet.
tbhattacharjee
|
|
|
|
|