strange bug - object scope?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CannonFodder

    strange bug - object scope?

    Hi all, i have a bit of a weird problem i'll try and explain what
    happens:

    I have a class called "Rules" which contains lots of objects of type
    "RuleLocato r" or "RuleFormat ter" (both inheriting from "aRule").

    I also have a class called "WMIScanner " which appies "Rules" to data.
    When i define my "Rules" object within the constructor of "WMIScanner "
    everything runs fine, however if i try and pass a "Rules" object into
    "WMIScanner " from another class - called Scanner i get errors.
    "Specified Cast is not valid"

    this happens even though i am using the exact same code - just
    defining the obejct outside of the "WMIScanner " class instead of
    inside. The "Rules" object does not contain any references to
    anything outside itself.

    Does anyone know what might be happening here?

    Thanks in advance for any help

    Code follows:

    This causes errors:
    public WMIScanner(stri ng ComputerName, Rules r)
    {
    strComputerName = ComputerName;
    myRules = r;

    Connect();
    }

    This doesnt error - but i dont want to leave it this way:

    public WMIScanner(stri ng ComputerName)
    {
    strComputerName = ComputerName;
    Connect();
    ...
    declare individual rules (ruleCapacity etc..)
    ...
    myRules = new Rules();
    myRules.addRule (ruleCapacity);
    myRules.addRule (ruleSpeed);
    myRules.addRule (ruleFreeSpace) ;

    myRules.addRule (myLocatorRule) ;
    myRules.addRule (XPRule);
    myRules.addRule (DellRule);
    myRules.addRule (CompaqRule);
    myRules.addRule (FreeSpaceRule) ;

    myRules.addRule (myFlat);
    myRules.addRule (myFlat2);
    myRules.addRule (myFlat3);
    }
  • Jon Skeet [C# MVP]

    #2
    Re: strange bug - object scope?

    CannonFodder <gavinj@sol-tec.com> wrote:[color=blue]
    > Hi all, i have a bit of a weird problem i'll try and explain what
    > happens:
    >
    > I have a class called "Rules" which contains lots of objects of type
    > "RuleLocato r" or "RuleFormat ter" (both inheriting from "aRule").
    >
    > I also have a class called "WMIScanner " which appies "Rules" to data.
    > When i define my "Rules" object within the constructor of "WMIScanner "
    > everything runs fine, however if i try and pass a "Rules" object into
    > "WMIScanner " from another class - called Scanner i get errors.
    > "Specified Cast is not valid"
    >
    > this happens even though i am using the exact same code - just
    > defining the obejct outside of the "WMIScanner " class instead of
    > inside. The "Rules" object does not contain any references to
    > anything outside itself.
    >
    > Does anyone know what might be happening here?[/color]

    Could you post a short but complete program which demonstrates the
    problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for details of
    what I mean by that.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Ignacio Machin \( .NET/ C#  MVP \)

      #3
      Re: strange bug - object scope?

      Hi,

      Are all the classes in the same project ( assembly) ?

      Both the WMIScanner, Rules , aRule and the class that use them?

      Maybe you are defining the same class in two different places.

      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "CannonFodd er" <gavinj@sol-tec.com> wrote in message
      news:7f7e8031.0 410040014.75277 675@posting.goo gle.com...[color=blue]
      > Hi all, i have a bit of a weird problem i'll try and explain what
      > happens:
      >
      > I have a class called "Rules" which contains lots of objects of type
      > "RuleLocato r" or "RuleFormat ter" (both inheriting from "aRule").
      >
      > I also have a class called "WMIScanner " which appies "Rules" to data.
      > When i define my "Rules" object within the constructor of "WMIScanner "
      > everything runs fine, however if i try and pass a "Rules" object into
      > "WMIScanner " from another class - called Scanner i get errors.
      > "Specified Cast is not valid"
      >
      > this happens even though i am using the exact same code - just
      > defining the obejct outside of the "WMIScanner " class instead of
      > inside. The "Rules" object does not contain any references to
      > anything outside itself.
      >
      > Does anyone know what might be happening here?
      >
      > Thanks in advance for any help
      >
      > Code follows:
      >
      > This causes errors:
      > public WMIScanner(stri ng ComputerName, Rules r)
      > {
      > strComputerName = ComputerName;
      > myRules = r;
      >
      > Connect();
      > }
      >
      > This doesnt error - but i dont want to leave it this way:
      >
      > public WMIScanner(stri ng ComputerName)
      > {
      > strComputerName = ComputerName;
      > Connect();
      > ...
      > declare individual rules (ruleCapacity etc..)
      > ...
      > myRules = new Rules();
      > myRules.addRule (ruleCapacity);
      > myRules.addRule (ruleSpeed);
      > myRules.addRule (ruleFreeSpace) ;
      >
      > myRules.addRule (myLocatorRule) ;
      > myRules.addRule (XPRule);
      > myRules.addRule (DellRule);
      > myRules.addRule (CompaqRule);
      > myRules.addRule (FreeSpaceRule) ;
      >
      > myRules.addRule (myFlat);
      > myRules.addRule (myFlat2);
      > myRules.addRule (myFlat3);
      > }[/color]


      Comment

      Working...