Declaring & Variable already declared.. Message

Just starting out? Need help? Post your questions and find answers here.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Declaring & Variable already declared.. Message

Post by Thunder93 »

Hi.

I'm hitting my head up against a wall trying to figure this out. I feel that I should at least know how to-do this, but I'm stumped.

My dilemma..
I make 32App, but to have it support 64bit OS, I have to include 64bit supported structure.

My ignorance is to declaring. If the 32App is running on 64bit OS, use Item.StructB, otherwise use Item.StructA. The Variable name is depended on so It needs to remain the same but declared to either structure based on criteria.

And I read the structure from variable name such as Item. The name must not change but a way to declare differently with alternative structure and both in an If and else cases. Currently I would be receiving already declared message.

Concept code:

Code: Select all

   CompilerIf #PB_Compiler_Processor = #PB_Processor_x86

      If Is64bitOS() : Protected Item.StructB : Else : Protected Item.StructA : EndIf
  
   CompilerElse
     Protected Item.StructA
   CompilerEndIf
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Declaring & Variable already declared.. Message

Post by JHPJHP »

Not sure about the following, but I'm interested to see where this thread leads:

Code: Select all

Structure Struct32
  test.l
EndStructure

Structure Struct64
  test.q
EndStructure

Procedure GetStructure32()
  Protected Item.Struct32
  ProcedureReturn Item
EndProcedure

Procedure GetStructure64()
  Protected Item.Struct64
  ProcedureReturn Item
EndProcedure

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  *Item.Struct32 = GetStructure32()
CompilerElse
  *Item.Struct64 = GetStructure64()
CompilerEndIf

*Item = AllocateMemory(SizeOf(*Item))
*Item\test = SizeOf(*Item\test)
Debug *Item\test
FreeMemory(*Item)
Last edited by JHPJHP on Thu Feb 13, 2014 12:36 am, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Declaring & Variable already declared.. Message

Post by luis »

@JHP

I think the problem is he wants a single variable being one of two different types selected at runtime, so conditional compilation doesn't help.

I'm not sure I have understood well, but something like this would be acceptable ?

Code: Select all

Procedure Is64bitOS()
 ProcedureReturn 0 ; change this
EndProcedure

Structure Struct32 ; 32 bit structure
 field.l
EndStructure

Structure Struct64 ; 64 bit structure
 field.q
EndStructure

Structure Universal ; union
 StructureUnion 
  S32.Struct32
  S64.Struct64
 EndStructureUnion
EndStructure 

Global Item.Universal ; "Item" is always the same so it's simple declare-wise

; but you have to differentiate it somewhere, maybe it's not a problem ? looks clean enough

If Is64bitOS()
 Debug SizeOf(Item\S64) 
 Item\S64\field = 1  
Else
 Debug SizeOf(Item\S32)
 Item\S32\field = 1
EndIf
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Hey JHPJHP. :)

Thanks. However the code you provided has limits, one stated by luis.


@luis: Thanks. The problem still exists I'd have to duplicate bunch of code in-order to still be supporting 64bit OS from my 32bit App. I'm still hoping for a convenient method, and avoid lot of extra footprint.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Really appreciate you guys helping me, trying to figure this out. I really hope there is a way to-do this with PureBasic.

Currently I could, simply use;

Protected Item2.StructB ;My included x64 Structure to be using when running my 32bit App on x64 OS.
Protected Item1.StructA ;[Pointing to native Structure]

But still comes back to duplicating code and making it twice as big in PB. I should be-able declare Item1 or Item2 based on criteria ..as Item and have my later code dependant on that.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: Declaring & Variable already declared.. Message

Post by jack »

how about

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
      Protected Item.StructB
   CompilerElse
     Protected Item.StructA
CompilerEndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Hi jack.
That won't work for my needs. See my original posted code. Needing single variable being one of two different types selected at runtime.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Harry0
User
User
Posts: 13
Joined: Sun Mar 02, 2008 4:28 pm
Location: Palatine

Re: Declaring & Variable already declared.. Message

Post by Harry0 »

Would something like this work for you:

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86

      If Is64bitOS() : Protected *Item = @ItemB.StructB : Else : Protected *Item = @ItemA.StructA : EndIf
  
   CompilerElse
     Protected *Item = @ItemA.StructA
   CompilerEndIf

If it is memory space that you want to minimize then try this:

Code: Select all


Structure StructA
  Thing1.i
EndStructure

Structure StructB
  Thing1.q
EndStructure
  
Structure items2
  StructureUnion
    ItemA.StructA
    ItemB.StructB
  EndStructureUnion
EndStructure

Procedure test()
  
  Items.items2
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  
    If Is64bitOS() : Protected *Item = @Items\ItemB : Else : Protected *Item = @Items\ItemA : EndIf
  
  CompilerElse
   Protected *Item = @Items\ItemA
  CompilerEndIf

EndProcedure

And then you just need to reference/use *Item.

NOTE: Have not tested this code but it does pass a syntax check.

Hope that helps!

Harry0
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Hi Harry0.

Still faced with the variable already declared message ... Error: Line 26 - Local variable already declared: *Item.

... you only going to receive this message if you testing with the PB x86 compiler :wink:

And trying with PB x64, I receive...

Line ???: The following variable doesn't have a 'Structure': *Item ... *Item\MyField1 = "Test"
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Declaring & Variable already declared.. Message

Post by Danilo »

@Thunder93:
Could you please first explain why you need a different structure when the 32bit
program is running on a 64bit OS? It is still a 32bit program and can't access 64bit
stuff anyway, so why would you need a special 64bit structure in a 32bit program?
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Hi Danilo.

32Bit App isn't as limited on x64 OS as you might think. I already know that using 64bit structure in my 32bit App running on x64 OS ... will work.

I actually demonstrated the need w/my last post on 'how enumerate all desktop icons (name/label) ?' - http://www.purebasic.fr/english/viewtop ... 15#p437640
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Is the verdict in? Does this need to be a feature request? No other avenues to explore? :lol:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Declaring & Variable already declared.. Message

Post by Demivec »

This should make the code Harry0 posted compile:

Code: Select all

Structure StructA
  Thing1.i
EndStructure

Structure StructB
  Thing1.q
EndStructure
  
Structure items2
  StructureUnion
    ItemA.StructA
    ItemB.StructB
  EndStructureUnion
EndStructure

Procedure test()
  
  Items.items2
  
  Protected *Item
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    
    
    If Is64bitOS() :  *Item = @Items\ItemB : Else : *Item = @Items\ItemA : EndIf
  
  CompilerElse
    *Item = @Items\ItemA
  CompilerEndIf

EndProcedure

The code still has other problems. One of these is that it can't tell which memory layout to use, ItemA or ItemB, for any of the references to the substructure.


You could use an interface and a newItem() function. The newItem() function would reserve the memory for the correct structure (depending on the OS) and then the interfaces functions would set and retrieve the items values. You would also have to destroy the 'object' through another interface call (i.e. before exiting a procedure).

This would limit any duplication to basically the small amount of code in the interface without having to spread it throughout your program if items are referenced from many different areas. It does some add additional statements to create and free the variable's memory and require using the interface to interact with the values.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Declaring & Variable already declared.. Message

Post by Thunder93 »

Thanks Demivec.

You are pointing into an direction I have not explored before... I'll look towards the angels and pray that my wish comes true. :wink:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Declaring & Variable already declared.. Message

Post by Demivec »

Thunder93 wrote:Thanks Demivec.

You are pointing into an direction I have not explored before... I'll look towards the angels and pray that my wish comes true. :wink:
You're welcome. Will you post the structure's details? I can share a more meaningful code example if you do.
Post Reply