Jump to content

Cranial Gas

- - - - -

  • Please log in to reply
5 replies to this topic

#1
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
Ok trying to recall how to do this with a class.

Example I've got my class Widgits


public class Widgits

{

    public string Type { get; set; }

    public string Color { get; set; }

    public string Weight { get; set; }

    public string TotalHeight { get; set; }

    public string TotalWidth { get; set; }

    public string TotalLength { get; set; }

    public string Material { get; set; }

}


And Via this I can assign the values as
Widgits myWidgit = new Widgits();
and the values then as
myWidgit.Color = "Blue";
mywidgit.Material = "Unobatanium";
ect...
But isn't their a way to have it also return
string VarableName = mywidgit.TotalHeight .???? to Get VarableName = "Total Height" ? Or what ever I code it to?

Reason is I'm trying to go through all assigned values of a Class, and get a descriptor of the variable along with the value itself. The Descriptors would be fixed, the value can be adjusted as needed.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I don't think so. I'm not real strong on C#, but as I recall Widgets.TotalHeight is a reference to a string object. As a result, the string object doesn't know which of several references may be used to access it at any given moment.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Professor Green

Professor Green

    Newbie

  • Members
  • Pip
  • 5 posts
Hello,

I think you want a behavior for the TotalHeight in your class SO make a TotalHeight method in the Widgits class :

public class Widgits

    {

        private string totalHeight ;

        public string TotalHeight

        {

            get { return totalHeight ; }

            set { totalHeight = value; }

        //OR set { totalHeight = 123; } or any other condition you want


        // now a constructor for your class

        public Widgits (); //Default Constructor

        public Widgits (string theight)

        {

            totalHeight = theight;

        }


        // HERE'S THE BEHAVIOR I THINK YOU WANT, a 'METHOD' will handle the behavior of TotalHeight:

        public string TotalHeight (string height)

        {

             CODE HERE WHAT DO YOU WANT FROM METHODE TO RETURN FOR YOU

             example : string sum = height+xxx+!!!+;

             

             return sum;

        }

        }


now use the methode :
Finally string VarableName = mywidgit.TotalHeight(any string you want to work with ithere);

I don't know if my post was helpful , I wish it will be helpful

thank you ...

#4
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
Supplemental Question.
1) This class is being used by two applications. I'd like to not have to have my custom class be it's own DLL so How do I share the File so I only have one copy of it that is used by both applications during development.
2) I'd like to be able to (I think it's Override?) the Binary Reader / Binary Writer so I can use something like BinaryFile.ReadWidgits() and BinaryFile.Write(MyWidgit)
Right now for Number 2 I can break it out in both different applications and read / write each type but as this class expands I might forget to add something or mix up the order I'm reading / writhing.

#5
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
I though it would be something like this (add it to my Widgits class)


public class Widgits

{

    public virtual void Write(Widgits value)

    {

        // Now Use This To Write Each value?

    }

}

But when I try to add the command
SaveFileWriter.Write(mywidgit); I get the error

Error 13 The best overloaded method match for 'System.IO.BinaryWriter.Write(bool)' has some invalid arguments

#6
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts

PGP_Protector said:

Supplemental Question.
1) This class is being used by two applications. I'd like to not have to have my custom class be it's own DLL so How do I share the File so I only have one copy of it that is used by both applications during development.
Solved: Don't add existing, but Add Existing AS LINK

PGP_Protector said:

2) I'd like to be able to (I think it's Override?) the Binary Reader / Binary Writer so I can use something like BinaryFile.ReadWidgits() and BinaryFile.Write(MyWidgit)
Right now for Number 2 I can break it out in both different applications and read / write each type but as this class expands I might forget to add something or mix up the order I'm reading / writhing.
Workaround For Now, though I'd like to find a way to modify System.IO.BinaryWrite so I can have it write / read the Widgits class.
Added a Write/Read command to the Widgits Class.
so I just go Widgits.Write(BinaryWriter) / Widgits.Read(BinaryReader). Not quite the solution I wanted, but it does work.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users