开发者

'Class' is a 'type' but is used like a 'variable'

I'm coding an application which uses a fingerprint reader to capture images of someone's fingerprints and save it in the database.

I'm busy converting vb logic into c#, but I'm stuck here...

Within my main form I have 2 classes: fingerPrints and fingerImage respectively.

My issue is the following:

From my fingerPrints class, I needed to reference the fingerImage class. Here's some code for clarity:

This method is derived from the fingerPrint class, which references the fingerImage class:

public byte[] getImageFromFinger(string finger)
        {
            foreach (fingerImage fi in fingerImage)
            {
                if (fi.Finger == finger)
                    return fi.Image;
            }
            return null;
        }

My problem is the error which I can't seem to fix...fingerImage is a 'type' but is used like a 'variable'.

I need to loop through the class 10 times, once for each finger, but I'm doing something wrong and I think it's something small. Any help would be appreciated.

Furthermore, here's some code from my fingerImage class, just in case...

public class fingerImage : frmFingerprintsMain
        {
            private byte[] _image;
            private string _finger;

            public fingerImage(byte[] image, string finger)
            {
                Image = image;
                Finger = finger;
            }

            public byte[] Image
          开发者_开发问答  {
                get { return _image; }
                set { _image = value; }
            }

            public string Finger
            {
                get { return _finger; }
                set { _finger = value; }
            }
        }


You are getting the error because you have no variable named fingerImage. You do however have a type called fingerImage.

You probably need to get a collection of fingerImages from somewhere to iterate through.


A class is a type; you seem to be thinking of the word "class" as synonymous with "object" (you can't "loop through" a class; you can loop through an enumerable object, though).

Basically, what you need instead of this:

foreach (fingerImage fi in fingerImage)

...is something like this:

foreach (fingerImage fi in fingerImages)

...where fingerImages is an collection of instances of the fingerImage class.

Where exactly that collection is, I can't really say without seeing more code. But I'm assuming you have one somewhere (maybe a fingerImage[]? Or a List<fingerImage>?).


fingerImage fi in fingerImage

These two types are the same, this shouldn't happen. The latter fingerImage, should be a collection.


Try this:

foreach (FingerImage fi in fingerImages)
{
    ....
}

Note that:

  • The variable name that you are iterating over is probably some sort of collection and the variable name is probably plural (and if it isn't, it most likely should be).
  • The class should be in Pascal case to follow the Microsoft naming conventions. It looks like you need to fix your class definition too. Following the naming conventions can reduce confusion and make errors like this easier to spot.

I know that following naming conventions often seems like unnecessary work, but it will save you time in the long run, and especially when you have other people join your team.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜