Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:03:05+00:00 2026-05-28T03:03:05+00:00

I’m trying to find a struct I created earlier that has a specific value.

  • 0

I’m trying to find a struct I created earlier that has a specific value. Once I found it, I want to set variables on that struct. I don’t know how to do this. Is there a better way of doing this? Maybe classes? Or should structs work?

For example, my struct:

public struct MyTest
{
    public string device;
    public string status;
    public string revision;
    public string number;
    public string ledmo;        
}

My Test Code:

MyTest thisTest=new MyTest();
thisTest.device=blah;
thisTest.number=blah2;

MyTest thisTest2=new MyTest();
thisTest2.device=blah5;
thisTest2.number=blah6;

//Another Part in my code.
//Need to find the MyTest Structure that 'device' variable = the string 'blah'
var Foundit=MyTest.find(device==blah);
Foundit.revision=blah9999;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:03:06+00:00Added an answer on May 28, 2026 at 3:03 am

    I’d use a class, because Mutable structs are evil

    Basically, because every struct is copied, even if you do find the right struct, you’ll only ever change one copy. Lets say MyTest.find finds thisTest2 what happens is this

    var Foundit = MyTest.Find(device==blah); 
    // The line above has made a copy of thisTest2, that copy is in FoundIt
    
    Foundit.revision = "blah9999";
    // You've changed revision in the copy of thisTest2, 
    // therefore the contents of thisTest2 remain unchanged
    

    To do this with a class you’ll need to keep every instance of the class you create in a list or other data structure, so you know you can look it up.

    If you do this you also need to tell the list when you’re finished with each object, otherwise they’ll hang around forever and never get garbage collected.

    Before I go any further, are you sure this is the best way to solve this problem?

    Anyway, say your class is MyData, you can put a static factory method on this called Create, which will put each new MyData object into a list.

    public class MyData
    {
        private static List<MyData> allMyDatas = new List<MyData>();
        public static IEnumerable<MyData> AllInstances
        {
            get {return allMyDatas;}
        }
    
        public string Device {get; set;}
        public string Status {get; set;}
        public string Revision {get; set;}
        public string Number {get; set;}
        public string Ledmo {get; set;}
    
        private MyData() // Private ctor ensures only a member 
        {                // function can create a new MyData
        }
        public static MyData Create()
        {
            var newData = new MyData();
            allMyDatas.Add(newData);
            return newData;
        }
    
        public static void Delete(MyData itemToRemove)
        {
            allMyDatas.Remove(itemToRemove);
        }
    }
    

    Everywhere you use a MyData you’ll need to Delete it when you’re finished with it.

    Your code becomes

    var thisTest = MyData.Create();
    thisTest.Device = "blah";
    thisTest.Number = "blah2";
    
    var  thisTest2 = MyData.Create();
    thisTest2.Device = "blah5";
    thisTest2.Number = "blah6";
    
    //Another Part in my code.
    //Need to find the MyData Structure that 'device' variable = the string 'blah'
    var Foundit = MyData.AllInstances.FirstOrDefault(md => md.Device == "blah");
    if(Foundit != null)
        Foundit.Revision = "blah9999";    
    

    Changing FoundIt now also changes thisTest

    P.S.: It’s important that nothing outside MyData can new an instance of MyData. If it could, then there would be an instance of MyData that you couldn’t find in AllInstances. Declaring the constructor private means a compiler error will be generated if code outside MyData tries something like var someData = new MyData

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.